Skip to content

Commit

Permalink
Support Node16 ESM resolution (#140)
Browse files Browse the repository at this point in the history
* add e2e test for CommonJS/ESM

* Make it compatible with ESM on NodeJS

* fix module resolution on project

* fix a test case

* update eslint packages
  • Loading branch information
cometkim authored Sep 6, 2024
1 parent 6ab54a3 commit df01de5
Show file tree
Hide file tree
Showing 8 changed files with 1,381 additions and 420 deletions.
44 changes: 34 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
],
"version": "4.0.0-rc.3",
"main": "sources/advanced/index.ts",
"exports": {
".": "./sources/advanced/index.ts",
"./platform": "./sources/platform/node.ts"
},
"license": "MIT",
"sideEffects": false,
"repository": {
Expand All @@ -30,28 +34,28 @@
"typanion": "*"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^10.0.0",
"@rollup/plugin-typescript": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/jest": "^29.5.9",
"@types/lodash": "^4.14.179",
"@types/node": "^14.0.13",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"@types/rollup": "^0.54.0",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@yarnpkg/core": "^2.3.1",
"@yarnpkg/eslint-config": "yarnpkg/berry#workspace=@yarnpkg/eslint-config",
"@yarnpkg/fslib": "^3.0.1",
"eslint": "^8.54.0",
"eslint-plugin-arca": "^0.10.0",
"eslint-plugin-react": "^7.21.5",
"eslint": "^8.56.0",
"eslint-plugin-arca": "^0.16.0",
"eslint-plugin-react": "^7.33.0",
"get-stream": "^5.1.0",
"jest": "^29.7.0",
"lodash": "^4.17.21",
"rollup": "^2.16.1",
"rollup": "^2.78.0",
"rollup-plugin-multi-input": "^1.3.1",
"ts-jest": "^29.1.1",
"ts-node": "^8.10.2",
"tslib": "^2.0.0",
"typescript": "^4.8.4"
"typescript": "^5.3.3"
},
"scripts": {
"prepack": "rm -rf lib && rollup -c",
Expand All @@ -66,6 +70,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
1 change: 0 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export default {
}),
ts({
tsconfig: `tsconfig.dist.json`,
include: `./sources/**/*`,
}),
],
};
2 changes: 1 addition & 1 deletion sources/advanced/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as platform from 'clipanion/platform';
import {Readable, Writable} from 'stream';

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
2 changes: 1 addition & 1 deletion tests/specs/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe(`Browser support`, () => {

await rollup({
input: npath.fromPortablePath(`${tempDir}/index.js`),
plugins: [nodeResolve({preferBuiltins: true})],
plugins: [nodeResolve({preferBuiltins: false})],
onwarn: warning => warnings.push(warning),
});

Expand Down
119 changes: 119 additions & 0 deletions tests/specs/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {execUtils} from '@yarnpkg/core';
import {xfs, PortablePath, npath} from '@yarnpkg/fslib';

describe(`E2E`, () => {
it(`works with CommonJS`, 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-commonjs`});
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 xfs.writeFilePromise(`${tempDir}/index.cjs` as PortablePath,
`const {Command, Option, runExit} = require('clipanion');
runExit(class MainCommand extends Command {
name = Option.String();
async execute() {
this.context.stdout.write(\`Hello \${this.name}!\\n\`);
}
})`,
);

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

it(`works with ESM`, 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-esmodule`});
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 xfs.writeFilePromise(`${tempDir}/index.mjs` as PortablePath,
`import {Command, Option, runExit} from 'clipanion';
runExit(class MainCommand extends Command {
name = Option.String();
async execute() {
this.context.stdout.write(\`Hello \${this.name}!\\n\`);
}
})`,
);

const result = await execUtils.execvp(`node`, [`${tempDir}/index.mjs`, `World`], {cwd: tempDir});
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);
});
3 changes: 2 additions & 1 deletion tsconfig.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"rootDir": "sources",
"outDir": "lib",
"types": [
"node"
"node",
"rollup"
]
},
"include": [
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"DOM",
"ES2018"
],
"module": "commonjs",
"module": "esnext",
"moduleResolution": "bundler",
"noEmit": true,
"skipLibCheck": true,
"strict": true,
Expand Down
Loading

0 comments on commit df01de5

Please sign in to comment.