Skip to content

Commit

Permalink
Use commonjs package type for JS client (#36)
Browse files Browse the repository at this point in the history
After many hours of research, this appears to be the most versatile and interoperable way of publishing dual package libraries.
  • Loading branch information
lorisleiva authored May 2, 2024
1 parent 41d47d0 commit 900ed38
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-wombats-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-solana-program": patch
---

Use commonjs package type for JS client
6 changes: 6 additions & 0 deletions template/clients/js/clients/js/_.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
14 changes: 7 additions & 7 deletions template/clients/js/clients/js/package.json.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion template/clients/js/clients/js/test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down
4 changes: 2 additions & 2 deletions template/clients/js/clients/js/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions template/clients/js/clients/js/test/increment.test.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
findCounterPda,
getIncrementInstruction,
getIncrementInstructionAsync,
} from '../src/index.js';
} from '../src';
import {
createCounterForAuthority,
createDefaultSolanaClient,
createDefaultTransaction,
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.
Expand Down
11 changes: 6 additions & 5 deletions template/clients/js/clients/js/tsconfig.declarations.json
Original file line number Diff line number Diff line change
@@ -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"]
}
34 changes: 17 additions & 17 deletions template/clients/js/clients/js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions template/clients/js/clients/js/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -22,7 +22,7 @@ export default defineConfig(() => [
...SHARED_OPTIONS,
bundle: false,
entry: ['./test/*.ts'],
format: 'esm',
format: 'cjs',
outDir: './dist/test',
},
]);

0 comments on commit 900ed38

Please sign in to comment.