From b6ba29770eba760f025e677c2a943e89dd8a86bf Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Wed, 24 Jan 2024 21:22:41 +0900 Subject: [PATCH] Make it compatible with ESM on NodeJS --- package.json | 20 +++++++++++++ sources/advanced/Cli.ts | 4 +-- tests/specs/e2e.test.ts | 63 +++++++++++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 5272e21..5a164c0 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/sources/advanced/Cli.ts b/sources/advanced/Cli.ts index f84ea96..ab99a15 100644 --- a/sources/advanced/Cli.ts +++ b/sources/advanced/Cli.ts @@ -1,10 +1,10 @@ -import {Readable, Writable} from 'stream'; +import {Readable, Writable} from 'node:stream'; +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'; diff --git a/tests/specs/e2e.test.ts b/tests/specs/e2e.test.ts index 4176b48..da1b66c 100644 --- a/tests/specs/e2e.test.ts +++ b/tests/specs/e2e.test.ts @@ -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}); @@ -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); @@ -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}); @@ -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); });