GeezerVison is showing github.com — lib: remove top-level getOptionValue() calls in lib/internal/modules · nodejs/node@deda50a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/formats.js -70Lines changed: 0 additions & 70 deletions Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js +58-6Lines changed: 58 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,13 +9,63 @@ const { | 9 | 9 | StringPrototypeSlice, | 10 | 10 | } = primordials; | 11 | 11 | 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 | +} | 17 | 36 |
| 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 | +} | 19 | 69 | const { containsModuleSyntax } = internalBinding('contextify'); | 20 | 70 | const { getPackageScopeConfig, getPackageType } = require('internal/modules/package_json_reader'); | 21 | 71 | const { fileURLToPath } = require('internal/url'); | @@ -35,6 +85,7 @@ const protocolHandlers = { | 35 | 85 | * @returns {'module'|'commonjs'} | 36 | 86 | */ | 37 | 87 | function detectModuleFormat(source, url) { | | 88 | + const detectModule = getOptionValue('--experimental-detect-module'); | 38 | 89 | if (!source) { return detectModule ? null : 'commonjs'; } | 39 | 90 | if (!detectModule) { return 'commonjs'; } | 40 | 91 | return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs'; | @@ -216,4 +267,5 @@ module.exports = { | 216 | 267 | defaultGetFormatWithoutErrors, | 217 | 268 | extensionFormatMap, | 218 | 269 | extname, | | 270 | + initializeExtensionFormatMap, | 219 | 271 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js +2-5Lines changed: 2 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,9 +29,6 @@ const { realpathSync } = require('fs'); | 29 | 29 | const { getOptionValue } = require('internal/options'); | 30 | 30 | // Do not eagerly grab .manifest, it may be in TDZ | 31 | 31 | 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'); | 35 | 32 | const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url'); | 36 | 33 | const { getCWDURL, setOwnProperty } = require('internal/util'); | 37 | 34 | const { canParse: URLCanParse } = internalBinding('url'); | @@ -982,7 +979,7 @@ function defaultResolve(specifier, context = {}) { | 982 | 979 | // input, to avoid user confusion over how expansive the effect of the | 983 | 980 | // flag should be (i.e. entry point only, package scope surrounding the | 984 | 981 | // 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(); } | 986 | 983 | } | 987 | 984 |
| 988 | 985 | conditions = getConditionsSet(conditions); | @@ -992,7 +989,7 @@ function defaultResolve(specifier, context = {}) { | 992 | 989 | specifier, | 993 | 990 | parentURL, | 994 | 991 | conditions, | 995 | | - isMain ? preserveSymlinksMain : preserveSymlinks, | | 992 | + isMain ? getOptionValue('--preserve-symlinks-main') : getOptionValue('--preserve-symlinks'), | 996 | 993 | ); | 997 | 994 | } catch (error) { | 998 | 995 | // Try to give the user a hint of what would have been the | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/pre_execution.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,6 +161,9 @@ function prepareExecution(options) { | 161 | 161 | assert(!initializeModules); | 162 | 162 | } | 163 | 163 |
| | 164 | + const { initializeExtensionFormatMap } = require('internal/modules/esm/get_format'); | | 165 | + initializeExtensionFormatMap(); | | 166 | + | 164 | 167 | setupVmModules(); | 165 | 168 | if (initializeModules) { | 166 | 169 | initializeModuleLoaders({ shouldSpawnLoaderHookWorker, shouldPreloadModules }); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/repl/completion.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,7 +48,7 @@ const CJSModule = require('internal/modules/cjs/loader').Module; | 48 | 48 |
| 49 | 49 | const { | 50 | 50 | extensionFormatMap, | 51 | | -} = require('internal/modules/esm/formats'); | | 51 | +} = require('internal/modules/esm/get_format'); | 52 | 52 |
| 53 | 53 | const path = require('path'); | 54 | 54 | const fs = require('fs'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-bootstrap-modules.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -118,6 +118,7 @@ expected.beforePreExec = new Set([ | 118 | 118 | ]); | 119 | 119 |
| 120 | 120 | expected.atRunTime = new Set([ | | 121 | + 'NativeModule internal/modules/esm/get_format', | 121 | 122 | 'NativeModule internal/process/pre_execution', | 122 | 123 | ]); | 123 | 124 |
| |
|