| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ececd22 commit 1db210a
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1404,7 +1404,7 @@ function loadESMFromCJS(mod, filename) { | |||
| 1404 | 1404 | * @param {'commonjs'|undefined} format Intended format of the module. | |
| 1405 | 1405 | */ | |
| 1406 | 1406 | function wrapSafe(filename, content, cjsModuleInstance, format) { | |
| 1407 | - assert(format !== 'module'); // ESM should be handled in loadESMFromCJS(). | ||
| 1407 | + assert(format !== 'module', 'ESM should be handled in loadESMFromCJS()'); | ||
| 1408 | 1408 | const hostDefinedOptionId = vm_dynamic_import_default_internal; | |
| 1409 | 1409 | const importModuleDynamically = vm_dynamic_import_default_internal; | |
| 1410 | 1410 | if (patched) { | |
@@ -1434,7 +1434,17 @@ function wrapSafe(filename, content, cjsModuleInstance, format) { | |||
| 1434 | 1434 | }; | |
| 1435 | 1435 | } | |
| 1436 | 1436 | ||
| 1437 | - const shouldDetectModule = (format !== 'commonjs' && getOptionValue('--experimental-detect-module')); | ||
| 1437 | + let shouldDetectModule = false; | ||
| 1438 | + if (format !== 'commonjs') { | ||
| 1439 | + if (cjsModuleInstance?.[kIsMainSymbol]) { | ||
| 1440 | + // For entry points, format detection is used unless explicitly disabled. | ||
| 1441 | + shouldDetectModule = getOptionValue('--experimental-detect-module'); | ||
| 1442 | + } else { | ||
| 1443 | + // For modules being loaded by `require()`, if require(esm) is disabled, | ||
| 1444 | + // don't try to reparse to detect format and just throw for ESM syntax. | ||
| 1445 | + shouldDetectModule = getOptionValue('--experimental-require-module'); | ||
| 1446 | + } | ||
| 1447 | + } | ||
| 1438 | 1448 | const result = compileFunctionForCJSLoader(content, filename, false /* is_sea_main */, shouldDetectModule); | |
| 1439 | 1449 | ||
| 1440 | 1450 | // Cache the source map for the module if present. | |
@@ -1471,8 +1481,6 @@ Module.prototype._compile = function(content, filename, format) { | |||
| 1471 | 1481 | } | |
| 1472 | 1482 | } | |
| 1473 | 1483 | ||
| 1474 | - // TODO(joyeecheung): when the module is the entry point, consider allowing TLA. | ||
| 1475 | - // Only modules being require()'d really need to avoid TLA. | ||
| 1476 | 1484 | if (format === 'module') { | |
| 1477 | 1485 | // Pass the source into the .mjs extension handler indirectly through the cache. | |
| 1478 | 1486 | this[kModuleSource] = content; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + // Flags: --no-experimental-require-module | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + // Tests that --experimental-require-module is not implied by --experimental-detect-module | ||
| 5 | + // and is checked independently. | ||
| 6 | + require('../common'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + | ||
| 9 | + // Check that require() still throws SyntaxError for an ambiguous module that's detected to be ESM. | ||
| 10 | + // TODO(joyeecheung): now that --experimental-detect-module is unflagged, it makes more sense | ||
| 11 | + // to either throw ERR_REQUIRE_ESM for require() of detected ESM instead, or add a hint about the | ||
| 12 | + // use of require(esm) to the SyntaxError. | ||
| 13 | + | ||
| 14 | + assert.throws( | ||
| 15 | + () => require('../fixtures/es-modules/loose.js'), | ||
| 16 | + { | ||
| 17 | + name: 'SyntaxError', | ||
| 18 | + message: /Unexpected token 'export'/ | ||
| 19 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | - // This file can be run or imported only if `--experimental-default-type=module` is set. | ||
| 1 | + // This file can be run or imported only if `--experimental-default-type=module` is set | ||
| 2 | + // or `--experimental-detect-module` is not disabled. If it's loaded by | ||
| 3 | + // require(), then `--experimental-require-module` must not be disabled. | ||
| 2 | 4 | export default 'module'; | |
| 3 | 5 | console.log('executed'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments