Skip to content

Commit

Permalink
Detect esm builtins protocol change on node 12.20.0 (#1332)
Browse files Browse the repository at this point in the history
* add test for node builtin module loading in ESM --experimental-specifier-resolution=node tests

* Run ESM tests on all tested node versions, not just >=13

* switch from builtin protocol nodejs: to node: on >=12.20.0, <13

* run esm on minimum of node 12.16

* pass --experimental-modules where necessary

* lint fix

* play nice with node 12.16 picky option parsing

* lint-fix
  • Loading branch information
cspotcode authored May 23, 2021
1 parent 16b66eb commit 373b2be
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
25 changes: 16 additions & 9 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
matrix:
os: [ubuntu, windows]
# Don't forget to add all new flavors to this list!
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9]
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
include:
# Node 12.15
# TODO Add comments about why we test 12.15; I think git blame says it's because of an ESM behavioral change that happened at 12.16
Expand All @@ -57,49 +57,56 @@ jobs:
nodeFlag: 12_15
typescript: latest
typescriptFlag: latest
# Node 12
# Node 12.16
# Earliest version that supports getFormat, etc hooks: https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#12.16.0
- flavor: 2
node: 12.16
nodeFlag: 12_16
typescript: latest
typescriptFlag: latest
# Node 12
- flavor: 3
node: 12
nodeFlag: 12
typescript: latest
typescriptFlag: latest
# Node 14.13.0
# To test ESM builtin module resolution immediately before a node behavioral change: https://github.com/TypeStrong/ts-node/issues/1130
- flavor: 3
- flavor: 4
node: 14.13.0
nodeFlag: 14_13_0
typescript: latest
typescriptFlag: latest
# Node 14
- flavor: 4
- flavor: 5
node: 14
nodeFlag: 14
typescript: latest
typescriptFlag: latest
- flavor: 5
- flavor: 6
node: 14
nodeFlag: 14
typescript: 2.7
typescriptFlag: 2_7
- flavor: 6
- flavor: 7
node: 14
nodeFlag: 14
typescript: next
typescriptFlag: next
# Node 16
- flavor: 7
- flavor: 8
node: 16
nodeFlag: 16
typescript: latest
typescriptFlag: latest
downgradeNpm: true
- flavor: 8
- flavor: 9
node: 16
nodeFlag: 16
typescript: 2.7
typescriptFlag: 2_7
downgradeNpm: true
- flavor: 9
- flavor: 10
node: 16
nodeFlag: 16
typescript: next
Expand Down
8 changes: 7 additions & 1 deletion dist-raw/node-esm-resolve-implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
'use strict';

const [nodeMajor, nodeMinor, nodePatch] = process.versions.node.split('.').map(s => parseInt(s, 10))
// Test for 14.13.1 or higher
// Test for node >14.13.1 || (>=12.20.0 && <13)
const builtinModuleProtocol = nodeMajor > 14 || (
nodeMajor === 14 && (
nodeMinor > 13 || (
nodeMinor === 13 && nodePatch > 0
)
)
) || (
nodeMajor === 12 && (
nodeMinor > 20 || (
nodeMinor === 20
)
)
)
? 'node:'
: 'nodejs:';
Expand Down
9 changes: 6 additions & 3 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,9 +1195,12 @@ test.suite('ts-node', (test) => {
});

test.suite('esm', (test) => {
const cmd = `node --loader ts-node/esm`;
const experimentalModulesFlag = semver.gte(process.version, '12.17.0')
? ''
: '--experimental-modules';
const cmd = `node ${experimentalModulesFlag} --loader ts-node/esm`;

if (semver.gte(process.version, '13.0.0')) {
if (semver.gte(process.version, '12.16.0')) {
test('should compile and execute as ESM', async () => {
const { err, stdout } = await exec(`${cmd} index.ts`, {
cwd: join(TEST_DIR, './esm'),
Expand Down Expand Up @@ -1250,7 +1253,7 @@ test.suite('ts-node', (test) => {
cwd: join(TEST_DIR, './esm-node-resolver'),
env: {
...process.env,
NODE_OPTIONS: '--experimental-specifier-resolution=node',
NODE_OPTIONS: `${experimentalModulesFlag} --experimental-specifier-resolution=node`,
},
});
expect(err).to.equal(null);
Expand Down
5 changes: 5 additions & 0 deletions tests/esm-node-resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { baz } from './baz';
import { biff } from './biff';
import { libfoo } from 'libfoo';

// Test import builtin modules
import { readFileSync } from 'fs';
if (typeof readFileSync !== 'function')
throw new Error('failed to import builtin module');

if (typeof module !== 'undefined')
throw new Error('module should not exist in ESM');

Expand Down

0 comments on commit 373b2be

Please sign in to comment.