| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f9dc1ea commit 0d327c8
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ const { | |||
| 17 | 17 | mimeToFormat, | |
| 18 | 18 | } = require('internal/modules/esm/formats'); | |
| 19 | 19 | ||
| 20 | + const detectModule = getOptionValue('--experimental-detect-module'); | ||
| 20 | 21 | const experimentalNetworkImports = | |
| 21 | 22 | getOptionValue('--experimental-network-imports'); | |
| 22 | 23 | const { containsModuleSyntax } = internalBinding('contextify'); | |
@@ -33,6 +34,17 @@ const protocolHandlers = { | |||
| 33 | 34 | 'node:'() { return 'builtin'; }, | |
| 34 | 35 | }; | |
| 35 | 36 | ||
| 37 | + /** | ||
| 38 | + * Determine whether the given ambiguous source contains CommonJS or ES module syntax. | ||
| 39 | + * @param {string | Buffer | undefined} source | ||
| 40 | + * @param {URL} url | ||
| 41 | + */ | ||
| 42 | + function detectModuleFormat(source, url) { | ||
| 43 | + if (!source) { return detectModule ? null : 'commonjs'; } | ||
| 44 | + if (!detectModule) { return 'commonjs'; } | ||
| 45 | + return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs'; | ||
| 46 | + } | ||
| 47 | + | ||
| 36 | 48 | /** | |
| 37 | 49 | * @param {URL} parsed | |
| 38 | 50 | * @returns {string | null} | |
@@ -112,26 +124,23 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE | |||
| 112 | 124 | default: { // The user did not pass `--experimental-default-type`. | |
| 113 | 125 | // `source` is undefined when this is called from `defaultResolve`; | |
| 114 | 126 | // but this gets called again from `defaultLoad`/`defaultLoadSync`. | |
| 115 | - if (getOptionValue('--experimental-detect-module')) { | ||
| 116 | - const format = source ? | ||
| 117 | - (containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs') : | ||
| 118 | - null; | ||
| 119 | - if (format === 'module') { | ||
| 120 | - // This module has a .js extension, a package.json with no `type` field, and ESM syntax. | ||
| 121 | - // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. | ||
| 122 | - typelessPackageJsonFilesWarnedAbout ??= new SafeSet(); | ||
| 123 | - if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) { | ||
| 124 | - const warning = `${url} parsed as an ES module because module syntax was detected;` + | ||
| 125 | - ` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`; | ||
| 126 | - process.emitWarning(warning, { | ||
| 127 | - code: 'MODULE_TYPELESS_PACKAGE_JSON', | ||
| 128 | - }); | ||
| 129 | - typelessPackageJsonFilesWarnedAbout.add(pjsonPath); | ||
| 130 | - } | ||
| 127 | + // For ambiguous files (no type field, .js extension) we return | ||
| 128 | + // undefined from `resolve` and re-run the check in `load`. | ||
| 129 | + const format = detectModuleFormat(source, url); | ||
| 130 | + if (format === 'module') { | ||
| 131 | + // This module has a .js extension, a package.json with no `type` field, and ESM syntax. | ||
| 132 | + // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. | ||
| 133 | + typelessPackageJsonFilesWarnedAbout ??= new SafeSet(); | ||
| 134 | + if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) { | ||
| 135 | + const warning = `${url} parsed as an ES module because module syntax was detected;` + | ||
| 136 | + ` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`; | ||
| 137 | + process.emitWarning(warning, { | ||
| 138 | + code: 'MODULE_TYPELESS_PACKAGE_JSON', | ||
| 139 | + }); | ||
| 140 | + typelessPackageJsonFilesWarnedAbout.add(pjsonPath); | ||
| 131 | 141 | } | |
| 132 | - return format; | ||
| 133 | 142 | } | |
| 134 | - return 'commonjs'; | ||
| 143 | + return format; | ||
| 135 | 144 | } | |
| 136 | 145 | } | |
| 137 | 146 | } | |
@@ -154,15 +163,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE | |||
| 154 | 163 | return 'commonjs'; | |
| 155 | 164 | } | |
| 156 | 165 | default: { // The user did not pass `--experimental-default-type`. | |
| 157 | - if (getOptionValue('--experimental-detect-module')) { | ||
| 158 | - if (!source) { return null; } | ||
| 159 | - const format = getFormatOfExtensionlessFile(url); | ||
| 160 | - if (format === 'module') { | ||
| 161 | - return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs'; | ||
| 162 | - } | ||
| 166 | + if (!source) { | ||
| 167 | + return null; | ||
| 168 | + } | ||
| 169 | + const format = getFormatOfExtensionlessFile(url); | ||
| 170 | + if (format === 'wasm') { | ||
| 163 | 171 | return format; | |
| 164 | 172 | } | |
| 165 | - return 'commonjs'; | ||
| 173 | + return detectModuleFormat(source, url); | ||
| 166 | 174 | } | |
| 167 | 175 | } | |
| 168 | 176 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments