Skip to content

Commit 5f813b1

Browse files
authored
Fix #1130 (#1136)
* Test against additional node versions: 14.13.0 and 15 * Add ESM test of builtin module resolution * Use version number test to switch node's builtin module URI protocol in ESM resolver * add new test matrix entries to flavors array
1 parent a0f7aa1 commit 5f813b1

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

.github/workflows/continuous-integration.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
matrix:
4141
os: [ubuntu, windows]
4242
# Don't forget to add all new flavors to this list!
43-
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
43+
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
4444
include:
4545
# Node 10
4646
- flavor: 1
@@ -94,20 +94,27 @@ jobs:
9494
nodeFlag: 14
9595
typescript: next
9696
typescriptFlag: next
97-
# Node 15
97+
# Node 14.13.0
98+
# To test ESM builtin module resolution immediately before a node behavioral change: https://github.com/TypeStrong/ts-node/issues/1130
9899
- flavor: 10
100+
node: 14.13.0
101+
nodeFlag: 14_13_0
102+
typescript: latest
103+
typescriptFlag: latest
104+
# Node 15
105+
- flavor: 11
99106
node: 15
100107
nodeFlag: 15
101108
typescript: latest
102109
typescriptFlag: latest
103110
downgradeNpm: true
104-
- flavor: 11
111+
- flavor: 12
105112
node: 15
106113
nodeFlag: 15
107114
typescript: 2.7
108115
typescriptFlag: 2_7
109116
downgradeNpm: true
110-
- flavor: 12
117+
- flavor: 13
111118
node: 15
112119
nodeFlag: 15
113120
typescript: next

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
// upstream changes and understand our modifications.
55
'use strict';
66

7+
const [nodeMajor, nodeMinor, nodePatch] = process.versions.node.split('.').map(s => parseInt(s, 10))
8+
// Test for 14.13.1 or higher
9+
const builtinModuleProtocol = nodeMajor > 14 || (
10+
nodeMajor === 14 && (
11+
nodeMinor > 13 || (
12+
nodeMinor === 13 && nodePatch > 0
13+
)
14+
)
15+
)
16+
? 'node:'
17+
: 'nodejs:';
18+
719
const {
820
ArrayIsArray,
921
JSONParse,
@@ -688,13 +700,13 @@ function defaultResolve(specifier, { parentURL } = {}, defaultResolveUnused) {
688700
};
689701
}
690702
} catch {}
691-
if (parsed && parsed.protocol === 'nodejs:')
703+
if (parsed && parsed.protocol === builtinModuleProtocol)
692704
return { url: specifier };
693705
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
694706
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
695707
if (NativeModule.canBeRequiredByUsers(specifier)) {
696708
return {
697-
url: 'nodejs:' + specifier
709+
url: builtinModuleProtocol + specifier
698710
};
699711
}
700712
if (parentURL && StringPrototypeStartsWith(parentURL, 'data:')) {

tests/esm/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import {bar} from './bar.js'
33
import {baz} from './baz.js'
44
import {biff} from './biff.js'
55

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

812
console.log(`${foo} ${bar} ${baz} ${biff}`)

0 commit comments

Comments
 (0)