diff --git a/lib/internal/modules/esm/utils.js b/lib/internal/modules/esm/utils.js index d393d4336a0c1e..ef3831928fad54 100644 --- a/lib/internal/modules/esm/utils.js +++ b/lib/internal/modules/esm/utils.js @@ -79,6 +79,7 @@ function initializeDefaultConditions() { defaultConditions = ObjectFreeze([ 'node', 'import', + 'module', ...addonConditions, ...userConditions, ]); diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index a31c2a74d88b36..0cd7816277e37f 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -81,6 +81,9 @@ function initializeCjsConditions() { ...addonConditions, ...userConditions, ]); + if (getOptionValue('--experimental-require-module')) { + cjsConditions.add('module'); + } } /** diff --git a/test/es-module/test-import-module-conditional-exports-module.mjs b/test/es-module/test-import-module-conditional-exports-module.mjs new file mode 100644 index 00000000000000..a7ec46a3821ffc --- /dev/null +++ b/test/es-module/test-import-module-conditional-exports-module.mjs @@ -0,0 +1,27 @@ +import '../common/index.mjs'; +import assert from 'node:assert'; +import * as staticImport from '../fixtures/es-modules/module-condition/import.mjs'; +import { import as _import } from '../fixtures/es-modules/module-condition/dynamic_import.js'; + +async function dynamicImport(id) { + const result = await _import(id); + return result.resolved; +} + +assert.deepStrictEqual({ ...staticImport }, { + import_module_require: 'import', + module_and_import: 'module', + module_and_require: 'module', + module_import_require: 'module', + module_only: 'module', + module_require_import: 'module', + require_module_import: 'module', +}); + +assert.strictEqual(await dynamicImport('import-module-require'), 'import'); +assert.strictEqual(await dynamicImport('module-and-import'), 'module'); +assert.strictEqual(await dynamicImport('module-and-require'), 'module'); +assert.strictEqual(await dynamicImport('module-import-require'), 'module'); +assert.strictEqual(await dynamicImport('module-only'), 'module'); +assert.strictEqual(await dynamicImport('module-require-import'), 'module'); +assert.strictEqual(await dynamicImport('require-module-import'), 'module'); diff --git a/test/es-module/test-require-module-conditional-exports-module.js b/test/es-module/test-require-module-conditional-exports-module.js new file mode 100644 index 00000000000000..2a9e83869053d3 --- /dev/null +++ b/test/es-module/test-require-module-conditional-exports-module.js @@ -0,0 +1,15 @@ +// Flags: --experimental-require-module +'use strict'; + +require('../common'); +const assert = require('assert'); + +const loader = require('../fixtures/es-modules/module-condition/require.cjs'); + +assert.strictEqual(loader.require('import-module-require').resolved, 'module'); +assert.strictEqual(loader.require('module-and-import').resolved, 'module'); +assert.strictEqual(loader.require('module-and-require').resolved, 'module'); +assert.strictEqual(loader.require('module-import-require').resolved, 'module'); +assert.strictEqual(loader.require('module-only').resolved, 'module'); +assert.strictEqual(loader.require('module-require-import').resolved, 'module'); +assert.strictEqual(loader.require('require-module-import').resolved, 'require'); diff --git a/test/fixtures/es-module-loaders/loader-with-custom-condition.mjs b/test/fixtures/es-module-loaders/loader-with-custom-condition.mjs index 3aefed51d57d3e..00ee901be402ab 100644 --- a/test/fixtures/es-module-loaders/loader-with-custom-condition.mjs +++ b/test/fixtures/es-module-loaders/loader-with-custom-condition.mjs @@ -5,6 +5,7 @@ export async function resolve(specifier, context, defaultResolve) { deepStrictEqual([...context.conditions].sort(), [ 'import', + 'module', 'node', 'node-addons', ]); diff --git a/test/fixtures/es-modules/module-condition/dynamic_import.js b/test/fixtures/es-modules/module-condition/dynamic_import.js new file mode 100644 index 00000000000000..7c4cd42d7037f4 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/dynamic_import.js @@ -0,0 +1,5 @@ +function load(id) { + return import(id); +} + +export { load as import }; diff --git a/test/fixtures/es-modules/module-condition/import.mjs b/test/fixtures/es-modules/module-condition/import.mjs new file mode 100644 index 00000000000000..ae12fbf1429424 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/import.mjs @@ -0,0 +1,7 @@ +export { resolved as import_module_require } from 'import-module-require'; +export { resolved as module_and_import } from 'module-and-import'; +export { resolved as module_and_require } from 'module-and-require'; +export { resolved as module_import_require } from 'module-import-require'; +export { resolved as module_only } from 'module-only'; +export { resolved as module_require_import } from 'module-require-import'; +export { resolved as require_module_import } from 'require-module-import'; diff --git a/test/fixtures/es-modules/module-condition/node_modules/import-module-require/import.js b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/import.js new file mode 100644 index 00000000000000..58f10e20bf3041 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/import.js @@ -0,0 +1 @@ +export const resolved = 'import'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/import-module-require/module.js b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/import-module-require/package.json b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/package.json new file mode 100644 index 00000000000000..76f41cafae3fcf --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/package.json @@ -0,0 +1,11 @@ +{ + "type": "module", + "exports": { + "node": { + "import": "./import.js", + "module": "./module.js", + "require": "./require.cjs" + }, + "default": "./module.js" + } +} \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/import-module-require/require.cjs b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/require.cjs new file mode 100644 index 00000000000000..6dd2c2ec97abd6 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/import-module-require/require.cjs @@ -0,0 +1 @@ +exports.resolved = 'require'; diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-and-import/import.js b/test/fixtures/es-modules/module-condition/node_modules/module-and-import/import.js new file mode 100644 index 00000000000000..58f10e20bf3041 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-and-import/import.js @@ -0,0 +1 @@ +export const resolved = 'import'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-and-import/module.js b/test/fixtures/es-modules/module-condition/node_modules/module-and-import/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-and-import/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-and-import/package.json b/test/fixtures/es-modules/module-condition/node_modules/module-and-import/package.json new file mode 100644 index 00000000000000..cad5b69917dcdb --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-and-import/package.json @@ -0,0 +1,10 @@ +{ + "type": "module", + "exports": { + "node": { + "module": "./module.js", + "import": "./import.js" + }, + "default": "./module.js" + } +} diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-and-require/module.js b/test/fixtures/es-modules/module-condition/node_modules/module-and-require/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-and-require/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-and-require/package.json b/test/fixtures/es-modules/module-condition/node_modules/module-and-require/package.json new file mode 100644 index 00000000000000..c2c9cbd8e566a9 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-and-require/package.json @@ -0,0 +1,10 @@ +{ + "type": "module", + "exports": { + "node": { + "module": "./module.js", + "require": "./require.cjs" + }, + "default": "./module.js" + } +} \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-and-require/require.cjs b/test/fixtures/es-modules/module-condition/node_modules/module-and-require/require.cjs new file mode 100644 index 00000000000000..6dd2c2ec97abd6 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-and-require/require.cjs @@ -0,0 +1 @@ +exports.resolved = 'require'; diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-import-require/import.js b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/import.js new file mode 100644 index 00000000000000..58f10e20bf3041 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/import.js @@ -0,0 +1 @@ +export const resolved = 'import'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-import-require/module.js b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-import-require/package.json b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/package.json new file mode 100644 index 00000000000000..1bb3a3e16fecba --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/package.json @@ -0,0 +1,11 @@ +{ + "type": "module", + "exports": { + "node": { + "module": "./module.js", + "import": "./import.js", + "require": "./require.cjs" + }, + "default": "./module.js" + } +} \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-import-require/require.cjs b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/require.cjs new file mode 100644 index 00000000000000..6dd2c2ec97abd6 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-import-require/require.cjs @@ -0,0 +1 @@ +exports.resolved = 'require'; diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-only/module.js b/test/fixtures/es-modules/module-condition/node_modules/module-only/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-only/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-only/package.json b/test/fixtures/es-modules/module-condition/node_modules/module-only/package.json new file mode 100644 index 00000000000000..99276707cc9654 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-only/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "exports": { + "module": "./module.js" + } +} diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-require-import/import.js b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/import.js new file mode 100644 index 00000000000000..58f10e20bf3041 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/import.js @@ -0,0 +1 @@ +export const resolved = 'import'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-require-import/module.js b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-require-import/package.json b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/package.json new file mode 100644 index 00000000000000..f1e07207353d9d --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/package.json @@ -0,0 +1,11 @@ +{ + "type": "module", + "exports": { + "node": { + "module": "./module.js", + "require": "./require.cjs", + "import": "./import.js" + }, + "default": "./module.js" + } +} diff --git a/test/fixtures/es-modules/module-condition/node_modules/module-require-import/require.cjs b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/require.cjs new file mode 100644 index 00000000000000..f5bf7ed32992e1 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/module-require-import/require.cjs @@ -0,0 +1 @@ +export const resolved = 'require'; diff --git a/test/fixtures/es-modules/module-condition/node_modules/require-module-import/import.js b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/import.js new file mode 100644 index 00000000000000..58f10e20bf3041 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/import.js @@ -0,0 +1 @@ +export const resolved = 'import'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/require-module-import/module.js b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/module.js new file mode 100644 index 00000000000000..136ec680a7e3a8 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/module.js @@ -0,0 +1 @@ +export const resolved = 'module'; \ No newline at end of file diff --git a/test/fixtures/es-modules/module-condition/node_modules/require-module-import/package.json b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/package.json new file mode 100644 index 00000000000000..17588fc005dc90 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/package.json @@ -0,0 +1,11 @@ +{ + "type": "module", + "exports": { + "node": { + "require": "./require.cjs", + "module": "./module.js", + "import": "./module.js" + }, + "default": "./module.js" + } +} diff --git a/test/fixtures/es-modules/module-condition/node_modules/require-module-import/require.cjs b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/require.cjs new file mode 100644 index 00000000000000..6dd2c2ec97abd6 --- /dev/null +++ b/test/fixtures/es-modules/module-condition/node_modules/require-module-import/require.cjs @@ -0,0 +1 @@ +exports.resolved = 'require'; diff --git a/test/fixtures/es-modules/module-condition/require.cjs b/test/fixtures/es-modules/module-condition/require.cjs new file mode 100644 index 00000000000000..2457758a98f32b --- /dev/null +++ b/test/fixtures/es-modules/module-condition/require.cjs @@ -0,0 +1 @@ +exports.require = require;