GeezerVison is showing github.com — esm: consolidate ESM loader hooks · nodejs/node@df22736
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/esm.md +163-149Lines changed: 163 additions & 149 deletions - Display the source diff
- Display the rich diff
Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -981,14 +981,14 @@ Module.prototype.load = function(filename) { | 981 | 981 | Module._extensions[extension](this, filename); | 982 | 982 | this.loaded = true; | 983 | 983 |
| 984 | | - const ESMLoader = asyncESM.ESMLoader; | | 984 | + const esmLoader = asyncESM.esmLoader; | 985 | 985 | // Create module entry at load time to snapshot exports correctly | 986 | 986 | const exports = this.exports; | 987 | 987 | // Preemptively cache | 988 | 988 | if ((module?.module === undefined || | 989 | 989 | module.module.getStatus() < kEvaluated) && | 990 | | - !ESMLoader.cjsCache.has(this)) | 991 | | - ESMLoader.cjsCache.set(this, exports); | | 990 | + !esmLoader.cjsCache.has(this)) | | 991 | + esmLoader.cjsCache.set(this, exports); | 992 | 992 | }; | 993 | 993 |
| 994 | 994 |
| @@ -1022,7 +1022,7 @@ function wrapSafe(filename, content, cjsModuleInstance) { | 1022 | 1022 | lineOffset: 0, | 1023 | 1023 | displayErrors: true, | 1024 | 1024 | importModuleDynamically: async (specifier) => { | 1025 | | - const loader = asyncESM.ESMLoader; | | 1025 | + const loader = asyncESM.esmLoader; | 1026 | 1026 | return loader.import(specifier, normalizeReferrerURL(filename)); | 1027 | 1027 | }, | 1028 | 1028 | }); | @@ -1037,7 +1037,7 @@ function wrapSafe(filename, content, cjsModuleInstance) { | 1037 | 1037 | ], { | 1038 | 1038 | filename, | 1039 | 1039 | importModuleDynamically(specifier) { | 1040 | | - const loader = asyncESM.ESMLoader; | | 1040 | + const loader = asyncESM.esmLoader; | 1041 | 1041 | return loader.import(specifier, normalizeReferrerURL(filename)); | 1042 | 1042 | }, | 1043 | 1043 | }); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js +23-13Lines changed: 23 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 | const { | | 3 | + ObjectAssign, | | 4 | + ObjectCreate, | | 5 | + ObjectPrototypeHasOwnProperty, | 3 | 6 | RegExpPrototypeExec, | 4 | | - StringPrototypeStartsWith, | 5 | 7 | } = primordials; | 6 | 8 | const { extname } = require('path'); | 7 | 9 | const { getOptionValue } = require('internal/options'); | @@ -36,26 +38,25 @@ if (experimentalWasmModules) | 36 | 38 | if (experimentalJsonModules) | 37 | 39 | extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json'; | 38 | 40 |
| 39 | | -function defaultGetFormat(url, context, defaultGetFormatUnused) { | 40 | | - if (StringPrototypeStartsWith(url, 'node:')) { | 41 | | - return { format: 'builtin' }; | 42 | | - } | 43 | | - const parsed = new URL(url); | 44 | | - if (parsed.protocol === 'data:') { | | 41 | +const protocolHandlers = ObjectAssign(ObjectCreate(null), { | | 42 | + 'data:'(parsed) { | 45 | 43 | const { 1: mime } = RegExpPrototypeExec( | 46 | 44 | /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/, | 47 | 45 | parsed.pathname, | 48 | | - ) || [ , null ]; | | 46 | + ) || [, null]; | 49 | 47 | const format = ({ | 50 | 48 | '__proto__': null, | 51 | 49 | 'text/javascript': 'module', | 52 | 50 | 'application/json': experimentalJsonModules ? 'json' : null, | 53 | 51 | 'application/wasm': experimentalWasmModules ? 'wasm' : null | 54 | 52 | })[mime] || null; | 55 | | - return { format }; | 56 | | - } else if (parsed.protocol === 'file:') { | | 53 | + | | 54 | + return format; | | 55 | + }, | | 56 | + 'file:'(parsed, url) { | 57 | 57 | const ext = extname(parsed.pathname); | 58 | 58 | let format; | | 59 | + | 59 | 60 | if (ext === '.js') { | 60 | 61 | format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; | 61 | 62 | } else { | @@ -71,9 +72,18 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { | 71 | 72 | throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); | 72 | 73 | } | 73 | 74 | } | 74 | | - return { format: format || null }; | 75 | | - } | 76 | | - return { format: null }; | | 75 | + | | 76 | + return format || null; | | 77 | + }, | | 78 | + 'node:'() { return 'builtin'; }, | | 79 | +}); | | 80 | + | | 81 | +function defaultGetFormat(url, context) { | | 82 | + const parsed = new URL(url); | | 83 | + | | 84 | + return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ? | | 85 | + protocolHandlers[parsed.protocol](parsed, url) : | | 86 | + null; | 77 | 87 | } | 78 | 88 |
| 79 | 89 | module.exports = { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_source.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,6 +40,6 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) { | 40 | 40 | if (policy?.manifest) { | 41 | 41 | policy.manifest.assertIntegrity(parsed, source); | 42 | 42 | } | 43 | | - return { source }; | | 43 | + return source; | 44 | 44 | } | 45 | 45 | exports.defaultGetSource = defaultGetSource; |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const { defaultGetFormat } = require('internal/modules/esm/get_format'); | | 4 | +const { defaultGetSource } = require('internal/modules/esm/get_source'); | | 5 | +const { translators } = require('internal/modules/esm/translators'); | | 6 | + | | 7 | +async function defaultLoad(url, context) { | | 8 | + let { | | 9 | + format, | | 10 | + source, | | 11 | + } = context; | | 12 | + | | 13 | + if (!translators.has(format)) format = defaultGetFormat(url); | | 14 | + | | 15 | + if ( | | 16 | + format === 'builtin' || | | 17 | + format === 'commonjs' | | 18 | + ) { | | 19 | + source = null; | | 20 | + } else if (source == null) { | | 21 | + source = await defaultGetSource(url, { format }); | | 22 | + } | | 23 | + | | 24 | + return { | | 25 | + format, | | 26 | + source, | | 27 | + }; | | 28 | +} | | 29 | + | | 30 | +module.exports = { | | 31 | + defaultLoad, | | 32 | +}; |
|