Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1130 #1136

Merged
merged 5 commits into from Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}`)