Skip to content

Commit

Permalink
Fix #1130 (#1136)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cspotcode committed Oct 31, 2020
1 parent a0f7aa1 commit 5f813b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/continuous-integration.yml
Expand Up @@ -40,7 +40,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, 10, 11, 12]
flavor: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
include:
# Node 10
- flavor: 1
Expand Down Expand Up @@ -94,20 +94,27 @@ jobs:
nodeFlag: 14
typescript: next
typescriptFlag: next
# Node 15
# 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: 10
node: 14.13.0
nodeFlag: 14_13_0
typescript: latest
typescriptFlag: latest
# Node 15
- flavor: 11
node: 15
nodeFlag: 15
typescript: latest
typescriptFlag: latest
downgradeNpm: true
- flavor: 11
- flavor: 12
node: 15
nodeFlag: 15
typescript: 2.7
typescriptFlag: 2_7
downgradeNpm: true
- flavor: 12
- flavor: 13
node: 15
nodeFlag: 15
typescript: next
Expand Down
16 changes: 14 additions & 2 deletions dist-raw/node-esm-resolve-implementation.js
Expand Up @@ -4,6 +4,18 @@
// upstream changes and understand our modifications.
'use strict';

const [nodeMajor, nodeMinor, nodePatch] = process.versions.node.split('.').map(s => parseInt(s, 10))
// Test for 14.13.1 or higher
const builtinModuleProtocol = nodeMajor > 14 || (
nodeMajor === 14 && (
nodeMinor > 13 || (
nodeMinor === 13 && nodePatch > 0
)
)
)
? 'node:'
: 'nodejs:';

const {
ArrayIsArray,
JSONParse,
Expand Down Expand Up @@ -688,13 +700,13 @@ function defaultResolve(specifier, { parentURL } = {}, defaultResolveUnused) {
};
}
} catch {}
if (parsed && parsed.protocol === 'nodejs:')
if (parsed && parsed.protocol === builtinModuleProtocol)
return { url: specifier };
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME();
if (NativeModule.canBeRequiredByUsers(specifier)) {
return {
url: 'nodejs:' + specifier
url: builtinModuleProtocol + specifier
};
}
if (parentURL && StringPrototypeStartsWith(parentURL, 'data:')) {
Expand Down
4 changes: 4 additions & 0 deletions tests/esm/index.ts
Expand Up @@ -3,6 +3,10 @@ import {bar} from './bar.js'
import {baz} from './baz.js'
import {biff} from './biff.js'

// 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')

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

0 comments on commit 5f813b1

Please sign in to comment.