Przeglądaj źródła

Use commonjs package type for JS client (#36)

After many hours of research, this appears to be the most versatile and interoperable way of publishing dual package libraries.
Loris Leiva 1 rok temu
rodzic
commit
900ed38281

+ 5 - 0
.changeset/clever-wombats-grin.md

@@ -0,0 +1,5 @@
+---
+"create-solana-program": patch
+---
+
+Use commonjs package type for JS client

+ 6 - 0
template/clients/js/clients/js/_.eslintrc.cjs

@@ -1,5 +1,11 @@
 module.exports = {
   extends: ['@solana/eslint-config-solana'],
+  ignorePatterns: ['.eslintrc.cjs', 'tsup.config.ts', 'env-shim.ts'],
+  parserOptions: {
+    project: 'tsconfig.json',
+    tsconfigRootDir: __dirname,
+    sourceType: 'module',
+  },
   rules: {
     '@typescript-eslint/ban-types': 'off',
     '@typescript-eslint/sort-type-constituents': 'off',

+ 7 - 7
template/clients/js/clients/js/package.json.njk

@@ -3,15 +3,15 @@
   "version": "0.0.0",
   "description": "JavaScript client for the {{ programName | titleCase }} program",
   "sideEffects": false,
-  "module": "./dist/src/index.js",
-  "main": "./dist/src/index.cjs",
-  "types": "./dist/types/src/index.d.ts",
-  "type": "module",
+  "module": "./dist/src/index.mjs",
+  "main": "./dist/src/index.js",
+  "types": "./dist/types/index.d.ts",
+  "type": "commonjs",
   "exports": {
     ".": {
-      "types": "./dist/types/src/index.d.ts",
-      "import": "./dist/src/index.js",
-      "require": "./dist/src/index.cjs"
+      "types": "./dist/types/index.d.ts",
+      "import": "./dist/src/index.mjs",
+      "require": "./dist/src/index.js"
     }
   },
   "files": [

+ 1 - 1
template/clients/js/clients/js/test/_setup.ts

@@ -23,7 +23,7 @@ import {
   setTransactionMessageLifetimeUsingBlockhash,
   signTransactionMessageWithSigners,
 } from '@solana/web3.js';
-import { findCounterPda, getCreateInstructionAsync } from '../src/index.js';
+import { findCounterPda, getCreateInstructionAsync } from '../src';
 
 type Client = {
   rpc: Rpc<SolanaRpcApi>;

+ 2 - 2
template/clients/js/clients/js/test/create.test.ts

@@ -8,13 +8,13 @@ import {
   Counter,
   fetchCounterFromSeeds,
   getCreateInstructionAsync,
-} from '../src/index.js';
+} from '../src';
 import {
   createDefaultSolanaClient,
   createDefaultTransaction,
   generateKeyPairSignerWithSol,
   signAndSendTransaction,
-} from './_setup.js';
+} from './_setup';
 
 test('it creates a new counter account', async (t) => {
   // Given an authority key pair with some SOL.

+ 2 - 2
template/clients/js/clients/js/test/increment.test.ts.njk

@@ -15,7 +15,7 @@ import {
   findCounterPda,
   getIncrementInstruction,
   getIncrementInstructionAsync,
-} from '../src/index.js';
+} from '../src';
 import {
   createCounterForAuthority,
   createDefaultSolanaClient,
@@ -23,7 +23,7 @@ import {
   generateKeyPairSignerWithSol,
   getBalance,
   signAndSendTransaction,
-} from './_setup.js';
+} from './_setup';
 
 test('it increments an existing counter by 1 by default', async (t) => {
   // Given an authority key pair with an associated counter account of value 0.

+ 6 - 5
template/clients/js/clients/js/tsconfig.declarations.json

@@ -1,9 +1,10 @@
 {
   "compilerOptions": {
-      "declaration": true,
-      "declarationMap": true,
-      "emitDeclarationOnly": true,
-      "outDir": "./dist/types",
+    "declaration": true,
+    "declarationMap": true,
+    "emitDeclarationOnly": true,
+    "outDir": "./dist/types",
   },
-  "extends": "./tsconfig.json"
+  "extends": "./tsconfig.json",
+  "include": ["src"]
 }

+ 17 - 17
template/clients/js/clients/js/tsconfig.json

@@ -1,23 +1,23 @@
 {
   "$schema": "https://json.schemastore.org/tsconfig",
   "compilerOptions": {
-      "composite": false,
-      "declaration": true,
-      "declarationMap": true,
-      "esModuleInterop": true,
-      "forceConsistentCasingInFileNames": true,
-      "inlineSources": false,
-      "isolatedModules": true,
-      "module": "ESNext",
-      "moduleResolution": "node",
-      "noFallthroughCasesInSwitch": true,
-      "noUnusedLocals": true,
-      "noUnusedParameters": true,
-      "outDir": "./dist",
-      "preserveWatchOutput": true,
-      "skipLibCheck": true,
-      "strict": true,
-      "target": "ESNext"
+    "composite": false,
+    "declaration": true,
+    "declarationMap": true,
+    "esModuleInterop": true,
+    "forceConsistentCasingInFileNames": true,
+    "inlineSources": false,
+    "isolatedModules": true,
+    "module": "ESNext",
+    "moduleResolution": "node",
+    "noFallthroughCasesInSwitch": true,
+    "noUnusedLocals": true,
+    "noUnusedParameters": true,
+    "outDir": "./dist",
+    "preserveWatchOutput": true,
+    "skipLibCheck": true,
+    "strict": true,
+    "target": "ESNext"
   },
   "exclude": ["node_modules"],
   "include": ["src", "test"]

+ 2 - 2
template/clients/js/clients/js/tsup.config.ts

@@ -7,7 +7,7 @@ const SHARED_OPTIONS: Options = {
   entry: ['./src/index.ts'],
   inject: [path.resolve(__dirname, 'env-shim.ts')],
   outDir: './dist/src',
-  outExtension: ({ format }) => ({ js: format === 'cjs' ? '.cjs' : '.js' }),
+  outExtension: ({ format }) => ({ js: format === 'cjs' ? '.js' : '.mjs' }),
   sourcemap: true,
   treeshake: true,
 };
@@ -22,7 +22,7 @@ export default defineConfig(() => [
     ...SHARED_OPTIONS,
     bundle: false,
     entry: ['./test/*.ts'],
-    format: 'esm',
+    format: 'cjs',
     outDir: './dist/test',
   },
 ]);