Skip to content

Commit

Permalink
Make it compatible with ESM on NodeJS
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Jan 24, 2024
1 parent f65d64a commit b6ba297
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@
"browser": {
"./lib/platform/node.js": "./lib/platform/browser.js",
"./lib/platform/node.mjs": "./lib/platform/browser.mjs"
},
"exports": {
".": {
"types": "./lib/advanced/index.d.ts",
"require": "./lib/advanced/index.js",
"import": "./lib/advanced/index.mjs"
},
"./platform": {
"node": {
"types": "./lib/platform/node.d.ts",
"require": "./lib/platform/node.js",
"import": "./lib/platform/node.mjs"
},
"browser": {
"types": "./lib/platform/browser.d.ts",
"require": "./lib/platform/browser.js",
"import": "./lib/platform/browser.mjs"
}
},
"./package.json": "./package.json"
}
},
"files": [
Expand Down
4 changes: 2 additions & 2 deletions sources/advanced/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Readable, Writable} from 'stream';
import {Readable, Writable} from 'node:stream';

Check failure on line 1 in sources/advanced/Cli.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'node:stream' or its corresponding type declarations.

Check failure on line 1 in sources/advanced/Cli.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Cannot find module 'clipanion/platform' or its corresponding type declarations.

Check failure on line 1 in sources/advanced/Cli.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'node:stream' or its corresponding type declarations.

Check failure on line 1 in sources/advanced/Cli.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Cannot find module 'clipanion/platform' or its corresponding type declarations.
import * as platform from 'clipanion/platform';

import {HELP_COMMAND_INDEX} from '../constants';
import {CliBuilder, CommandBuilder} from '../core';
import {ErrorMeta} from '../errors';
import {formatMarkdownish, ColorFormat, richFormat, textFormat} from '../format';
import * as platform from '../platform/node';

import {CommandClass, Command, Definition} from './Command';
import {HelpCommand} from './HelpCommand';
Expand Down
63 changes: 55 additions & 8 deletions tests/specs/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ describe(`E2E`, () => {
const packed = await execUtils.execvp(`yarn`, [`pack`, `--out`, npath.fromPortablePath(`${tempDir}/${rndName}.tgz`)], {
cwd: npath.toPortablePath(__dirname),
});

expect(packed.code).toEqual(0);

await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-treeshake`});
await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-commonjs`});
await xfs.writeFilePromise(`${tempDir}/yarn.lock` as PortablePath, ``);

const added = await execUtils.execvp(`yarn`, [`add`, `./${rndName}.tgz`], {cwd: tempDir});
Expand All @@ -31,8 +30,11 @@ runExit(class MainCommand extends Command {
);

const result = await execUtils.execvp(`node`, [`${tempDir}/index.cjs`, 'World'], {cwd: tempDir});
expect(result.code).toEqual(0);
expect(result.stdout).toEqual('Hello World!\n');
expect(result).toEqual({
code: 0,
stderr: ``,
stdout: `Hello World!\n`,
});
});
}, 20000);

Expand All @@ -43,10 +45,9 @@ runExit(class MainCommand extends Command {
const packed = await execUtils.execvp(`yarn`, [`pack`, `--out`, npath.fromPortablePath(`${tempDir}/${rndName}.tgz`)], {
cwd: npath.toPortablePath(__dirname),
});

expect(packed.code).toEqual(0);

await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-treeshake`});
await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-esmodule`});
await xfs.writeFilePromise(`${tempDir}/yarn.lock` as PortablePath, ``);

const added = await execUtils.execvp(`yarn`, [`add`, `./${rndName}.tgz`], {cwd: tempDir});
Expand All @@ -65,8 +66,54 @@ runExit(class MainCommand extends Command {
);

const result = await execUtils.execvp(`node`, [`${tempDir}/index.mjs`, 'World'], {cwd: tempDir});
expect(result.code).toEqual(0);
expect(result.stdout).toEqual('Hello World!\n');
expect(result).toEqual({
code: 0,
stderr: ``,
stdout: `Hello World!\n`,
});
});
}, 20000);

it(`works with TypeScript's Node16 resolution`, async () => {
await xfs.mktempPromise(async tempDir => {
const rndName = Math.floor(Math.random() * 100000000).toString(16).padStart(8, `0`);

const packed = await execUtils.execvp(`yarn`, [`pack`, `--out`, npath.fromPortablePath(`${tempDir}/${rndName}.tgz`)], {
cwd: npath.toPortablePath(__dirname),
});
expect(packed.code).toEqual(0);

await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-ts-node16`});
await xfs.writeFilePromise(`${tempDir}/yarn.lock` as PortablePath, ``);

const added = await execUtils.execvp(`yarn`, [`add`, `./${rndName}.tgz`], {cwd: tempDir});
expect(added.code).toEqual(0);

await execUtils.execvp(`yarn`, [`add`, `-D`, `typescript`], {cwd: tempDir});

const tsconfig = {
compilerOptions: {
target: 'esnext',
module: 'node16',
moduleResolution: 'node16',
skipLibCheck: true,
noEmit: true,
},
};
await xfs.writeFilePromise(`${tempDir}/tsconfig.json` as PortablePath, JSON.stringify(tsconfig, null, 2));

await xfs.writeFilePromise(`${tempDir}/index.ts` as PortablePath,
`import {Command, Option, runExit} from 'clipanion';
import * as P from 'clipanion/platform';
`,
);

const result = await execUtils.execvp(`yarn`, [`tsc`], {cwd: tempDir});
expect(result).toEqual({
code: 0,
stderr: ``,
stdout: ``,
});
});
}, 20000);
});

0 comments on commit b6ba297

Please sign in to comment.