GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — esm: consolidate ESM loader hooks · nodejs/node@df22736

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • esm.md
  • lib
    • internal
      • modules
        • cjs
          • loader.js
        • esm
          • get_format.js
          • get_source.js
          • load.js
          • loader.js
          • module_job.js
          • resolve.js
          • transform_source.js
          • translators.js
        • run_main.js
      • process
        • esm_loader.js
        • execution.js
    • repl.js
  • test
    • es-module
      • test-esm-data-urls.js
      • test-esm-example-loader.mjs
      • test-esm-get-source-loader.mjs
      • test-esm-loader-get-format.mjs
      • test-esm-loader-modulemap.js
      • test-esm-loader-stringify-text.mjs
      • test-esm-loader.mjs
      • test-esm-resolve-hook.mjs
      • test-esm-transform-source-loader.mjs
    • fixtures
      • es-module-loaders
        • builtin-named-exports-loader.mjs
        • get-source.mjs
        • hooks-custom.mjs
        • hooks-obsolete.mjs
        • js-loader.mjs
        • loader-invalid-format.mjs
        • loader-unknown-builtin-module.mjs
        • string-sources.mjs
        • transform-source.mjs
      • es-modules
        • file.ext
        • file.unknown
        • stateful.mjs
    • message
      • esm_display_syntax_error.out
      • esm_display_syntax_error_import.out
      • esm_display_syntax_error_import_module.out
      • esm_display_syntax_error_module.out
      • esm_loader_not_found.out
      • esm_loader_not_found_cjs_hint_bare.out
      • esm_loader_not_found_cjs_hint_relative.out
      • esm_loader_syntax_error.out
      • test-esm-loader-obsolete-hooks.mjs
      • test-esm-loader-obsolete-hooks.out
    • parallel
      • test-bootstrap-modules.js

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/esm.md‎

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

‎lib/internal/modules/cjs/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -981,14 +981,14 @@ Module.prototype.load = function(filename) {
981981
Module._extensions[extension](this, filename);
982982
this.loaded = true;
983983

984-
const ESMLoader = asyncESM.ESMLoader;
984+
const esmLoader = asyncESM.esmLoader;
985985
// Create module entry at load time to snapshot exports correctly
986986
const exports = this.exports;
987987
// Preemptively cache
988988
if ((module?.module === undefined ||
989989
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);
992992
};
993993

994994

@@ -1022,7 +1022,7 @@ function wrapSafe(filename, content, cjsModuleInstance) {
10221022
lineOffset: 0,
10231023
displayErrors: true,
10241024
importModuleDynamically: async (specifier) => {
1025-
const loader = asyncESM.ESMLoader;
1025+
const loader = asyncESM.esmLoader;
10261026
return loader.import(specifier, normalizeReferrerURL(filename));
10271027
},
10281028
});
@@ -1037,7 +1037,7 @@ function wrapSafe(filename, content, cjsModuleInstance) {
10371037
], {
10381038
filename,
10391039
importModuleDynamically(specifier) {
1040-
const loader = asyncESM.ESMLoader;
1040+
const loader = asyncESM.esmLoader;
10411041
return loader.import(specifier, normalizeReferrerURL(filename));
10421042
},
10431043
});
Collapse file

‎lib/internal/modules/esm/get_format.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js
+23-13Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22
const {
3+
ObjectAssign,
4+
ObjectCreate,
5+
ObjectPrototypeHasOwnProperty,
36
RegExpPrototypeExec,
4-
StringPrototypeStartsWith,
57
} = primordials;
68
const { extname } = require('path');
79
const { getOptionValue } = require('internal/options');
@@ -36,26 +38,25 @@ if (experimentalWasmModules)
3638
if (experimentalJsonModules)
3739
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
3840

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) {
4543
const { 1: mime } = RegExpPrototypeExec(
4644
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
4745
parsed.pathname,
48-
) || [ , null ];
46+
) || [, null];
4947
const format = ({
5048
'__proto__': null,
5149
'text/javascript': 'module',
5250
'application/json': experimentalJsonModules ? 'json' : null,
5351
'application/wasm': experimentalWasmModules ? 'wasm' : null
5452
})[mime] || null;
55-
return { format };
56-
} else if (parsed.protocol === 'file:') {
53+
54+
return format;
55+
},
56+
'file:'(parsed, url) {
5757
const ext = extname(parsed.pathname);
5858
let format;
59+
5960
if (ext === '.js') {
6061
format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs';
6162
} else {
@@ -71,9 +72,18 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) {
7172
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));
7273
}
7374
}
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;
7787
}
7888

7989
module.exports = {
Collapse file

‎lib/internal/modules/esm/get_source.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_source.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
4040
if (policy?.manifest) {
4141
policy.manifest.assertIntegrity(parsed, source);
4242
}
43-
return { source };
43+
return source;
4444
}
4545
exports.defaultGetSource = defaultGetSource;
Collapse file

‎lib/internal/modules/esm/load.js‎

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
};

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.