-
-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit * Update hooks * wip impl of load * Expose old hooks for backward compat * Some logging * Add raw copy of default get format * Adapt defaultGetFormat() from node source * Fix defaultTransformSource * Add missing newline * Fix require * Check node version to avoid deprecation warning * Remove load from old hooks * Add some comments * Use versionGte * Remove logging * Refine comments * Wording * Use format hint if available * One more comment * Nitpicky changes to comments * Update index.ts * lint-fix * attempt at downloading node nightly in tests * fix * fix * Windows install of node nightly * update version checks to be ready for node backporting * Add guards for undefined source * More error info * Skip source transform for builtin and commonjs * Update transpile-only.mjs * Tweak `createEsmHooks` type * fix test to accomodate new api Co-authored-by: Andrew Bradley <[email protected]>
- Loading branch information
1 parent
4a0db31
commit a979dd6
Showing
8 changed files
with
235 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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