Skip to content

Commit 373b2be

Browse files
authored
Detect esm builtins protocol change on node 12.20.0 (#1332)
* 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
1 parent 16b66eb commit 373b2be

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
matrix:
4949
os: [ubuntu, windows]
5050
# Don't forget to add all new flavors to this list!
51-
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9]
51+
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
5252
include:
5353
# Node 12.15
5454
# 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
@@ -57,49 +57,56 @@ jobs:
5757
nodeFlag: 12_15
5858
typescript: latest
5959
typescriptFlag: latest
60-
# Node 12
60+
# Node 12.16
61+
# Earliest version that supports getFormat, etc hooks: https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V12.md#12.16.0
6162
- flavor: 2
63+
node: 12.16
64+
nodeFlag: 12_16
65+
typescript: latest
66+
typescriptFlag: latest
67+
# Node 12
68+
- flavor: 3
6269
node: 12
6370
nodeFlag: 12
6471
typescript: latest
6572
typescriptFlag: latest
6673
# Node 14.13.0
6774
# To test ESM builtin module resolution immediately before a node behavioral change: https://github.com/TypeStrong/ts-node/issues/1130
68-
- flavor: 3
75+
- flavor: 4
6976
node: 14.13.0
7077
nodeFlag: 14_13_0
7178
typescript: latest
7279
typescriptFlag: latest
7380
# Node 14
74-
- flavor: 4
81+
- flavor: 5
7582
node: 14
7683
nodeFlag: 14
7784
typescript: latest
7885
typescriptFlag: latest
79-
- flavor: 5
86+
- flavor: 6
8087
node: 14
8188
nodeFlag: 14
8289
typescript: 2.7
8390
typescriptFlag: 2_7
84-
- flavor: 6
91+
- flavor: 7
8592
node: 14
8693
nodeFlag: 14
8794
typescript: next
8895
typescriptFlag: next
8996
# Node 16
90-
- flavor: 7
97+
- flavor: 8
9198
node: 16
9299
nodeFlag: 16
93100
typescript: latest
94101
typescriptFlag: latest
95102
downgradeNpm: true
96-
- flavor: 8
103+
- flavor: 9
97104
node: 16
98105
nodeFlag: 16
99106
typescript: 2.7
100107
typescriptFlag: 2_7
101108
downgradeNpm: true
102-
- flavor: 9
109+
- flavor: 10
103110
node: 16
104111
nodeFlag: 16
105112
typescript: next

dist-raw/node-esm-resolve-implementation.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
'use strict';
66

77
const [nodeMajor, nodeMinor, nodePatch] = process.versions.node.split('.').map(s => parseInt(s, 10))
8-
// Test for 14.13.1 or higher
8+
// Test for node >14.13.1 || (>=12.20.0 && <13)
99
const builtinModuleProtocol = nodeMajor > 14 || (
1010
nodeMajor === 14 && (
1111
nodeMinor > 13 || (
1212
nodeMinor === 13 && nodePatch > 0
1313
)
1414
)
15+
) || (
16+
nodeMajor === 12 && (
17+
nodeMinor > 20 || (
18+
nodeMinor === 20
19+
)
20+
)
1521
)
1622
? 'node:'
1723
: 'nodejs:';

src/test/index.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,9 +1195,12 @@ test.suite('ts-node', (test) => {
11951195
});
11961196

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

1200-
if (semver.gte(process.version, '13.0.0')) {
1203+
if (semver.gte(process.version, '12.16.0')) {
12011204
test('should compile and execute as ESM', async () => {
12021205
const { err, stdout } = await exec(`${cmd} index.ts`, {
12031206
cwd: join(TEST_DIR, './esm'),
@@ -1250,7 +1253,7 @@ test.suite('ts-node', (test) => {
12501253
cwd: join(TEST_DIR, './esm-node-resolver'),
12511254
env: {
12521255
...process.env,
1253-
NODE_OPTIONS: '--experimental-specifier-resolution=node',
1256+
NODE_OPTIONS: `${experimentalModulesFlag} --experimental-specifier-resolution=node`,
12541257
},
12551258
});
12561259
expect(err).to.equal(null);

tests/esm-node-resolver/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { baz } from './baz';
44
import { biff } from './biff';
55
import { libfoo } from 'libfoo';
66

7+
// Test import builtin modules
8+
import { readFileSync } from 'fs';
9+
if (typeof readFileSync !== 'function')
10+
throw new Error('failed to import builtin module');
11+
712
if (typeof module !== 'undefined')
813
throw new Error('module should not exist in ESM');
914

0 commit comments

Comments
 (0)