Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1017bf5

Browse filesBrowse files
joyeecheungaduh95
authored andcommitted
lib: remove top-level getOptionValue() calls in lib/internal/modules
PR-URL: #61769 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent a20bf9a commit 1017bf5
Copy full SHA for 1017bf5

6 files changed

+65-82Lines changed: 65 additions & 82 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/formats.js
-70Lines changed: 0 additions & 70 deletions
This file was deleted.
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js
+58-6Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,63 @@ const {
99
StringPrototypeSlice,
1010
} = primordials;
1111
const { getOptionValue } = require('internal/options');
12-
const {
13-
extensionFormatMap,
14-
getFormatOfExtensionlessFile,
15-
mimeToFormat,
16-
} = require('internal/modules/esm/formats');
12+
const { getValidatedPath } = require('internal/fs/utils');
13+
const fsBindings = internalBinding('fs');
14+
const { internal: internalConstants } = internalBinding('constants');
15+
16+
const extensionFormatMap = {
17+
'__proto__': null,
18+
'.cjs': 'commonjs',
19+
'.js': 'module',
20+
'.json': 'json',
21+
'.mjs': 'module',
22+
'.wasm': 'wasm',
23+
};
24+
25+
function initializeExtensionFormatMap() {
26+
if (getOptionValue('--experimental-addon-modules')) {
27+
extensionFormatMap['.node'] = 'addon';
28+
}
29+
30+
if (getOptionValue('--strip-types')) {
31+
extensionFormatMap['.ts'] = 'module-typescript';
32+
extensionFormatMap['.mts'] = 'module-typescript';
33+
extensionFormatMap['.cts'] = 'commonjs-typescript';
34+
}
35+
}
1736

18-
const detectModule = getOptionValue('--experimental-detect-module');
37+
/**
38+
* @param {string} mime
39+
* @returns {string | null}
40+
*/
41+
function mimeToFormat(mime) {
42+
if (
43+
RegExpPrototypeExec(
44+
/^\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?$/i,
45+
mime,
46+
) !== null
47+
) { return 'module'; }
48+
if (mime === 'application/json') { return 'json'; }
49+
if (mime === 'application/wasm') { return 'wasm'; }
50+
return null;
51+
}
52+
53+
/**
54+
* For extensionless files in a `module` package scope, we check the file contents to disambiguate between ES module
55+
* JavaScript and Wasm.
56+
* We do this by taking advantage of the fact that all Wasm files start with the header `0x00 0x61 0x73 0x6d` (`_asm`).
57+
* @param {URL} url
58+
* @returns {'wasm'|'module'}
59+
*/
60+
function getFormatOfExtensionlessFile(url) {
61+
const path = getValidatedPath(url);
62+
switch (fsBindings.getFormatOfExtensionlessFile(path)) {
63+
case internalConstants.EXTENSIONLESS_FORMAT_WASM:
64+
return 'wasm';
65+
default:
66+
return 'module';
67+
}
68+
}
1969
const { containsModuleSyntax } = internalBinding('contextify');
2070
const { getPackageScopeConfig, getPackageType } = require('internal/modules/package_json_reader');
2171
const { fileURLToPath } = require('internal/url');
@@ -35,6 +85,7 @@ const protocolHandlers = {
3585
* @returns {'module'|'commonjs'}
3686
*/
3787
function detectModuleFormat(source, url) {
88+
const detectModule = getOptionValue('--experimental-detect-module');
3889
if (!source) { return detectModule ? null : 'commonjs'; }
3990
if (!detectModule) { return 'commonjs'; }
4091
return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';
@@ -216,4 +267,5 @@ module.exports = {
216267
defaultGetFormatWithoutErrors,
217268
extensionFormatMap,
218269
extname,
270+
initializeExtensionFormatMap,
219271
};
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ const { realpathSync } = require('fs');
2929
const { getOptionValue } = require('internal/options');
3030
// Do not eagerly grab .manifest, it may be in TDZ
3131
const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
32-
const preserveSymlinks = getOptionValue('--preserve-symlinks');
33-
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
34-
const inputTypeFlag = getOptionValue('--input-type');
3532
const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
3633
const { getCWDURL, setOwnProperty } = require('internal/util');
3734
const { canParse: URLCanParse } = internalBinding('url');
@@ -982,7 +979,7 @@ function defaultResolve(specifier, context = {}) {
982979
// input, to avoid user confusion over how expansive the effect of the
983980
// flag should be (i.e. entry point only, package scope surrounding the
984981
// entry point, etc.).
985-
if (inputTypeFlag) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
982+
if (getOptionValue('--input-type')) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
986983
}
987984

988985
conditions = getConditionsSet(conditions);
@@ -992,7 +989,7 @@ function defaultResolve(specifier, context = {}) {
992989
specifier,
993990
parentURL,
994991
conditions,
995-
isMain ? preserveSymlinksMain : preserveSymlinks,
992+
isMain ? getOptionValue('--preserve-symlinks-main') : getOptionValue('--preserve-symlinks'),
996993
);
997994
} catch (error) {
998995
// Try to give the user a hint of what would have been the
Collapse file

‎lib/internal/process/pre_execution.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/pre_execution.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ function prepareExecution(options) {
161161
assert(!initializeModules);
162162
}
163163

164+
const { initializeExtensionFormatMap } = require('internal/modules/esm/get_format');
165+
initializeExtensionFormatMap();
166+
164167
setupVmModules();
165168
if (initializeModules) {
166169
initializeModuleLoaders({ shouldSpawnLoaderHookWorker, shouldPreloadModules });
Collapse file

‎lib/internal/repl/completion.js‎

Copy file name to clipboardExpand all lines: lib/internal/repl/completion.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const CJSModule = require('internal/modules/cjs/loader').Module;
4848

4949
const {
5050
extensionFormatMap,
51-
} = require('internal/modules/esm/formats');
51+
} = require('internal/modules/esm/get_format');
5252

5353
const path = require('path');
5454
const fs = require('fs');
Collapse file

‎test/parallel/test-bootstrap-modules.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-bootstrap-modules.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ expected.beforePreExec = new Set([
118118
]);
119119

120120
expected.atRunTime = new Set([
121+
'NativeModule internal/modules/esm/get_format',
121122
'NativeModule internal/process/pre_execution',
122123
]);
123124

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.