-
-
Notifications
You must be signed in to change notification settings - Fork 545
Update esm loader hooks API #1457
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
Merged
Merged
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
2108b58
Initial commit
jonaskello f8ef837
Merge branch 'main' into resolver-hooks-update
jonaskello 073bbfc
Update hooks
jonaskello 765932f
Merge branch 'resolver-hooks-update' of github.com:jonaskello/ts-node…
jonaskello 786ec34
wip impl of load
jonaskello 14220ac
Expose old hooks for backward compat
jonaskello cd44ef3
Some logging
jonaskello 245242b
Add raw copy of default get format
jonaskello 0b0d50c
Adapt defaultGetFormat() from node source
jonaskello 5534722
Fix defaultTransformSource
jonaskello 1030783
Add missing newline
jonaskello 9f28485
Fix require
jonaskello 662b4b8
Check node version to avoid deprecation warning
jonaskello 469437b
Remove load from old hooks
jonaskello b102ee6
Add some comments
jonaskello 2044208
Use versionGte
jonaskello b2fffe7
Remove logging
jonaskello d41204c
Refine comments
jonaskello da34bf7
Wording
jonaskello a7e3475
Use format hint if available
jonaskello e228e08
One more comment
jonaskello f4dee40
Nitpicky changes to comments
cspotcode 0025586
Update index.ts
cspotcode 6ffe9ea
lint-fix
cspotcode 521a1e2
attempt at downloading node nightly in tests
cspotcode 8b94f1b
fix
cspotcode 10e5580
fix
cspotcode 77acae6
Windows install of node nightly
cspotcode 8e40254
update version checks to be ready for node backporting
cspotcode d3ed4a1
Add guards for undefined source
jonaskello 544584a
More error info
jonaskello a23bc47
Skip source transform for builtin and commonjs
jonaskello 63c0618
Update transpile-only.mjs
cspotcode ca9cde9
Merge remote-tracking branch 'origin/main' into resolver-hooks-update
cspotcode e576772
Tweak `createEsmHooks` type
cspotcode 8b328d3
fix test to accomodate new api
cspotcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copied from https://raw.githubusercontent.com/nodejs/node/v15.3.0/lib/internal/modules/esm/get_format.js | ||
| // Then modified to suite our needs. | ||
| // Formatting is intentionally bad to keep the diff as small as possible, to make it easier to merge | ||
| // upstream changes and understand our modifications. | ||
|
|
||
| 'use strict'; | ||
| const { | ||
| RegExpPrototypeExec, | ||
| StringPrototypeStartsWith, | ||
| } = require('./node-primordials'); | ||
| const { extname } = require('path'); | ||
| const { getOptionValue } = require('./node-options'); | ||
|
|
||
| const experimentalJsonModules = getOptionValue('--experimental-json-modules'); | ||
| const experimentalSpeciferResolution = | ||
| getOptionValue('--experimental-specifier-resolution'); | ||
| const experimentalWasmModules = getOptionValue('--experimental-wasm-modules'); | ||
| const { getPackageType } = require('./node-esm-resolve-implementation.js').createResolve({tsExtensions: [], jsExtensions: []}); | ||
| const { URL, fileURLToPath } = require('url'); | ||
| const { ERR_UNKNOWN_FILE_EXTENSION } = require('./node-errors').codes; | ||
|
|
||
| const extensionFormatMap = { | ||
| '__proto__': null, | ||
| '.cjs': 'commonjs', | ||
| '.js': 'module', | ||
| '.mjs': 'module' | ||
| }; | ||
|
|
||
| const legacyExtensionFormatMap = { | ||
| '__proto__': null, | ||
| '.cjs': 'commonjs', | ||
| '.js': 'commonjs', | ||
| '.json': 'commonjs', | ||
| '.mjs': 'module', | ||
| '.node': 'commonjs' | ||
| }; | ||
|
|
||
| if (experimentalWasmModules) | ||
| extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm'; | ||
|
|
||
| if (experimentalJsonModules) | ||
| extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json'; | ||
|
|
||
| function defaultGetFormat(url, context, defaultGetFormatUnused) { | ||
| if (StringPrototypeStartsWith(url, 'node:')) { | ||
| return { format: 'builtin' }; | ||
| } | ||
| const parsed = new URL(url); | ||
| if (parsed.protocol === 'data:') { | ||
| const [ , mime ] = RegExpPrototypeExec( | ||
| /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/, | ||
| parsed.pathname, | ||
| ) || [ null, null, null ]; | ||
| const format = ({ | ||
| '__proto__': null, | ||
| 'text/javascript': 'module', | ||
| 'application/json': experimentalJsonModules ? 'json' : null, | ||
| 'application/wasm': experimentalWasmModules ? 'wasm' : null | ||
| })[mime] || null; | ||
| return { format }; | ||
| } else if (parsed.protocol === 'file:') { | ||
| const ext = extname(parsed.pathname); | ||
| let format; | ||
| if (ext === '.js') { | ||
| format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; | ||
| } else { | ||
| format = extensionFormatMap[ext]; | ||
| } | ||
| if (!format) { | ||
| if (experimentalSpeciferResolution === 'node') { | ||
| process.emitWarning( | ||
| 'The Node.js specifier resolution in ESM is experimental.', | ||
| 'ExperimentalWarning'); | ||
| format = legacyExtensionFormatMap[ext]; | ||
| } else { | ||
| throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); | ||
| } | ||
| } | ||
| return { format: format || null }; | ||
| } | ||
| return { format: null }; | ||
| } | ||
| exports.defaultGetFormat = defaultGetFormat; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.