From be6bc320875eea09fd895cc5271ad90a9f9f96b0 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 14 Jul 2024 13:00:49 -0700 Subject: [PATCH] module: support __cjsUnwrapDefault interop flag in require(esm) --- doc/api/modules.md | 53 ++++++++++++++++++- lib/internal/modules/cjs/loader.js | 11 ++-- .../es-module/test-require-as-esm-interop.mjs | 51 ++++++++++++++++++ .../interop-cjs-esm/fauxesmdefault-esm.js | 3 ++ .../interop-cjs-esm/fauxesmdefault.js | 3 ++ .../interop-cjs-esm/fauxesmmixed-esm.js | 4 ++ .../interop-cjs-esm/fauxesmmixed.js | 4 ++ .../interop-cjs-esm/fauxesmnamed-esm.js | 3 ++ .../interop-cjs-esm/fauxesmnamed.js | 3 ++ .../interop-cjs-esm/object-esm.js | 3 ++ .../node_modules/interop-cjs-esm/object.js | 3 ++ .../node_modules/interop-cjs-esm/package.json | 15 ++++++ .../interop-cjs-esm/string-esm.js | 3 ++ .../node_modules/interop-cjs-esm/string.js | 3 ++ .../interop-cjs/fauxesmdefault-esm.js | 1 + .../interop-cjs/fauxesmdefault.js | 1 + .../interop-cjs/fauxesmmixed-esm.js | 1 + .../node_modules/interop-cjs/fauxesmmixed.js | 1 + .../interop-cjs/fauxesmnamed-esm.js | 1 + .../node_modules/interop-cjs/fauxesmnamed.js | 1 + .../node_modules/interop-cjs/object-esm.js | 1 + .../node_modules/interop-cjs/object.js | 1 + .../node_modules/interop-cjs/package.json | 15 ++++++ .../node_modules/interop-cjs/string-esm.js | 1 + .../node_modules/interop-cjs/string.js | 1 + .../interop-cjsdep-fauxesmdefault-esm/dep.js | 7 +++ .../package.json | 4 ++ .../interop-cjsdep-fauxesmdefault/dep.js | 2 + .../package.json | 4 ++ .../interop-cjsdep-fauxesmmixed-esm/dep.js | 9 ++++ .../package.json | 4 ++ .../interop-cjsdep-fauxesmmixed/dep.js | 4 ++ .../interop-cjsdep-fauxesmmixed/package.json | 4 ++ .../interop-cjsdep-fauxesmnamed-esm/dep.js | 8 +++ .../package.json | 4 ++ .../interop-cjsdep-fauxesmnamed/dep.js | 3 ++ .../interop-cjsdep-fauxesmnamed/package.json | 4 ++ .../interop-cjsdep-object-esm/dep.js | 5 ++ .../interop-cjsdep-object-esm/package.json | 4 ++ .../node_modules/interop-cjsdep-object/dep.js | 2 + .../interop-cjsdep-object/package.json | 4 ++ .../interop-cjsdep-string-esm/dep.js | 2 + .../interop-cjsdep-string-esm/package.json | 4 ++ .../node_modules/interop-cjsdep-string/dep.js | 1 + .../interop-depstring-esm/dep.js | 2 + .../interop-depstring-esm/package.json | 4 ++ .../interop-cjsdep-string/package.json | 4 ++ test/fixtures/pkgexports.mjs | 4 ++ 48 files changed, 274 insertions(+), 6 deletions(-) create mode 100644 test/es-module/test-require-as-esm-interop.mjs create mode 100644 test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/object-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/object.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjs-esm/string-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs-esm/string.js create mode 100644 test/fixtures/node_modules/interop-cjs/fauxesmdefault-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs/fauxesmdefault.js create mode 100644 test/fixtures/node_modules/interop-cjs/fauxesmmixed-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs/fauxesmmixed.js create mode 100644 test/fixtures/node_modules/interop-cjs/fauxesmnamed-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs/fauxesmnamed.js create mode 100644 test/fixtures/node_modules/interop-cjs/object-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs/object.js create mode 100644 test/fixtures/node_modules/interop-cjs/package.json create mode 100644 test/fixtures/node_modules/interop-cjs/string-esm.js create mode 100644 test/fixtures/node_modules/interop-cjs/string.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-object-esm/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-object-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-object/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-object/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-string-esm/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-string-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-string/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/dep.js create mode 100644 test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/package.json create mode 100644 test/fixtures/node_modules/interop-cjsdep-string/package.json diff --git a/doc/api/modules.md b/doc/api/modules.md index 56d435115929d8..2c997c51bdc1cf 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -201,10 +201,9 @@ export function distance(a, b) { return (b.x - a.x) ** 2 + (b.y - a.y) ** 2; } ```mjs // point.mjs -class Point { +export default class Point { constructor(x, y) { this.x = x; this.y = y; } } -export default Point; ``` A CommonJS module can load them with `require()` under `--experimental-detect-module`: @@ -233,6 +232,56 @@ This property is experimental and can change in the future. It should only be us by tools converting ES modules into CommonJS modules, following existing ecosystem conventions. Code authored directly in CommonJS should avoid depending on it. +To create an ESM module that should provide an arbitrary value to CommonJS, the +`__cjsUnwrapDefault: true` marker can be used instead: + +```mjs +// point.mjs +export default class Point { + constructor(x, y) { this.x = x; this.y = y; } +} + +// `distance` is lost to CommonJS consumers of this module, unless it's +// added to `Point` as a static property. +export function distance(a, b) { return (b.x - a.x) ** 2 + (b.y - a.y) ** 2; } +export const __cjsUnwrapDefault = true; +``` + +```cjs +const Point = require('./point.mjs'); +console.log(Point); // [class Point] + +// Named exports are lost when __cjsUnwrapDefault is used +const { distance } = require('./point.mjs'); +console.log(distance); // undefined +``` + +Notice in the example above, when `__cjsUnwrapDefault` is used, named exports will be +lost to CommonJS consumers. To allow CommonJS consumers to continue accessing +named exports, the module can make sure that the default export is an object with the +named exports attached to it as properties. For example with the example above, +`distance` can be attached to the default export, the `Point` class, as a static method. + +```mjs +export function distance(a, b) { return (b.x - a.x) ** 2 + (b.y - a.y) ** 2; } + +export default class Point { + constructor(x, y) { this.x = x; this.y = y; } + static distance = distance; +} + +export const __cjsUnwrapDefault = true; +``` + +```cjs +const Point = require('./point.mjs'); +console.log(Point); // [class Point] + +const { distance } = require('./point.mjs'); +console.log(distance); // [Function: distance] +``` + + If the module being `require()`'d contains top-level `await`, or the module graph it `import`s contains top-level `await`, [`ERR_REQUIRE_ASYNC_MODULE`][] will be thrown. In this case, users should diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 451b7c2195e7ad..2326dd5bc53d17 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -1416,10 +1416,13 @@ function loadESMFromCJS(mod, filename) { // createRequiredModuleFacade() to `wrap` which is a ModuleWrap wrapping // over the original module. - // We don't do this to modules that don't have default exports to avoid - // the unnecessary overhead. If __esModule is already defined, we will - // also skip the extension to allow users to override it. - if (!ObjectHasOwn(namespace, 'default') || ObjectHasOwn(namespace, '__esModule')) { + // We don't do this to modules that are marked as CJS ESM or that + // don't have default exports to avoid the unnecessary overhead. + // If __esModule is already defined, we will also skip the extension + // to allow users to override it. + if (namespace.__cjsUnwrapDefault) { + mod.exports = namespace.default; + } else if (!ObjectHasOwn(namespace, 'default') || ObjectHasOwn(namespace, '__esModule')) { mod.exports = namespace; } else { mod.exports = createRequiredModuleFacade(wrap); diff --git a/test/es-module/test-require-as-esm-interop.mjs b/test/es-module/test-require-as-esm-interop.mjs new file mode 100644 index 00000000000000..53650bafe2dc4e --- /dev/null +++ b/test/es-module/test-require-as-esm-interop.mjs @@ -0,0 +1,51 @@ +// Flags: --experimental-require-module +import { mustCall } from '../common/index.mjs'; +import assert from 'assert'; +import { directRequireFixture, importFixture } from '../fixtures/pkgexports.mjs'; + +const tests = { + 'string': 'cjs', + 'object': { a: 'cjs a', b: 'cjs b' }, + 'fauxesmdefault': { default: 'faux esm default' }, + 'fauxesmmixed': { default: 'faux esm default', a: 'faux esm a', b: 'faux esm b' }, + 'fauxesmnamed': { a: 'faux esm a', b: 'faux esm b' } +}; + +// This test demonstrates interop between CJS and CJS represented as ESM +// under the new `export const __cjsModule = true` pattern, for the above cases. +for (const [test, exactShape] of Object.entries(tests)) { + // Each case represents a CJS dependency, which has the expected shape in CJS: + assert.deepStrictEqual(directRequireFixture(`interop-cjsdep-${test}`), exactShape); + + // Each dependency is reexported through CJS as if it is a library being consumed, + // which in CJS is fully shape-preserving: + assert.deepStrictEqual(directRequireFixture(`interop-cjs/${test}`), exactShape); + + // Now we have ESM conversions of these dependencies, using __cjsModule = true, + // staring with the conversion of those dependencies into ESM under require(esm): + assert.deepStrictEqual(directRequireFixture(`interop-cjsdep-${test}-esm`), exactShape); + + // When importing these ESM conversions, from require(esm), we should preserve the shape: + assert.deepStrictEqual(directRequireFixture(`interop-cjs/${test}-esm`), exactShape); + + // Now if the importer itself is converted into ESM, it should still be able to load the original + // CJS and reexport it, preserving the shape: + assert.deepStrictEqual(directRequireFixture(`interop-cjs-esm/${test}`), exactShape); + + // And then if we have the converted CJS to ESM importing from converted CJS to ESM, + // that should also work: + assert.deepStrictEqual(directRequireFixture(`interop-cjs-esm/${test}-esm`), exactShape); + + // Finally, the CJS ESM representation under `import()` should match all these cases equivalently, + // where the CJS module is exported as the default export: + const esmCjsImport = await importFixture(`interop-cjsdep-${test}`); + assert.deepStrictEqual(esmCjsImport.default, exactShape); + + assert.deepStrictEqual((await importFixture(`interop-cjsdep-${test}`)).default, exactShape); + assert.deepStrictEqual((await importFixture(`interop-cjs/${test}`)).default, exactShape); + assert.deepStrictEqual((await importFixture(`interop-cjsdep-${test}-esm`)).default, exactShape); + assert.deepStrictEqual((await importFixture(`interop-cjs/${test}-esm`)).default, exactShape); + assert.deepStrictEqual((await importFixture(`interop-cjs-esm/${test}`)).default, exactShape); + assert.deepStrictEqual((await importFixture(`interop-cjs-esm/${test}-esm`)).default, exactShape); + +} diff --git a/test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault-esm.js b/test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault-esm.js new file mode 100644 index 00000000000000..740a050c6bb9e9 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault-esm.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-fauxesmdefault-esm'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault.js b/test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault.js new file mode 100644 index 00000000000000..e68647b01d1a95 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/fauxesmdefault.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-fauxesmdefault'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed-esm.js b/test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed-esm.js new file mode 100644 index 00000000000000..7ba7fefb9d735a --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed-esm.js @@ -0,0 +1,4 @@ +import dep from 'interop-cjsdep-fauxesmmixed-esm'; +export default dep; +export const __cjsUnwrapDefault = true; + diff --git a/test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed.js b/test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed.js new file mode 100644 index 00000000000000..1bdda5231a7a48 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/fauxesmmixed.js @@ -0,0 +1,4 @@ +import dep from 'interop-cjsdep-fauxesmmixed'; +export default dep; +export const __cjsUnwrapDefault = true; + diff --git a/test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed-esm.js b/test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed-esm.js new file mode 100644 index 00000000000000..f82bc73f0f95c0 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed-esm.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-fauxesmnamed-esm'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed.js b/test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed.js new file mode 100644 index 00000000000000..3274dd46cc144c --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/fauxesmnamed.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-fauxesmnamed'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/object-esm.js b/test/fixtures/node_modules/interop-cjs-esm/object-esm.js new file mode 100644 index 00000000000000..f8228d37200033 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/object-esm.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-object-esm'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/object.js b/test/fixtures/node_modules/interop-cjs-esm/object.js new file mode 100644 index 00000000000000..889d0dd29dad3a --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/object.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-object'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/package.json b/test/fixtures/node_modules/interop-cjs-esm/package.json new file mode 100644 index 00000000000000..ce6e0c8f2de27e --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/package.json @@ -0,0 +1,15 @@ +{ + "type": "module", + "exports": { + "./fauxesmdefault": "./fauxesmdefault.js", + "./fauxesmmixed": "./fauxesmmixed.js", + "./fauxesmnamed": "./fauxesmnamed.js", + "./string": "./string.js", + "./object": "./object.js", + "./fauxesmdefault-esm": "./fauxesmdefault-esm.js", + "./fauxesmmixed-esm": "./fauxesmmixed-esm.js", + "./fauxesmnamed-esm": "./fauxesmnamed-esm.js", + "./string-esm": "./string-esm.js", + "./object-esm": "./object-esm.js" + } +} diff --git a/test/fixtures/node_modules/interop-cjs-esm/string-esm.js b/test/fixtures/node_modules/interop-cjs-esm/string-esm.js new file mode 100644 index 00000000000000..4bb955f888755e --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/string-esm.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-string-esm'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs-esm/string.js b/test/fixtures/node_modules/interop-cjs-esm/string.js new file mode 100644 index 00000000000000..2ebd1d6eb92811 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs-esm/string.js @@ -0,0 +1,3 @@ +import dep from 'interop-cjsdep-string'; +export default dep; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjs/fauxesmdefault-esm.js b/test/fixtures/node_modules/interop-cjs/fauxesmdefault-esm.js new file mode 100644 index 00000000000000..45e91e3c3c80da --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/fauxesmdefault-esm.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-fauxesmdefault-esm'); diff --git a/test/fixtures/node_modules/interop-cjs/fauxesmdefault.js b/test/fixtures/node_modules/interop-cjs/fauxesmdefault.js new file mode 100644 index 00000000000000..c6e5176f528416 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/fauxesmdefault.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-fauxesmdefault'); diff --git a/test/fixtures/node_modules/interop-cjs/fauxesmmixed-esm.js b/test/fixtures/node_modules/interop-cjs/fauxesmmixed-esm.js new file mode 100644 index 00000000000000..6b16d657b0e42d --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/fauxesmmixed-esm.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-fauxesmmixed-esm'); diff --git a/test/fixtures/node_modules/interop-cjs/fauxesmmixed.js b/test/fixtures/node_modules/interop-cjs/fauxesmmixed.js new file mode 100644 index 00000000000000..8897d00fb3e563 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/fauxesmmixed.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-fauxesmmixed'); diff --git a/test/fixtures/node_modules/interop-cjs/fauxesmnamed-esm.js b/test/fixtures/node_modules/interop-cjs/fauxesmnamed-esm.js new file mode 100644 index 00000000000000..67ed05bfa7f6d4 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/fauxesmnamed-esm.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-fauxesmnamed-esm'); diff --git a/test/fixtures/node_modules/interop-cjs/fauxesmnamed.js b/test/fixtures/node_modules/interop-cjs/fauxesmnamed.js new file mode 100644 index 00000000000000..092ec333260157 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/fauxesmnamed.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-fauxesmnamed'); diff --git a/test/fixtures/node_modules/interop-cjs/object-esm.js b/test/fixtures/node_modules/interop-cjs/object-esm.js new file mode 100644 index 00000000000000..f0e0d3fa3dad07 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/object-esm.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-object-esm'); diff --git a/test/fixtures/node_modules/interop-cjs/object.js b/test/fixtures/node_modules/interop-cjs/object.js new file mode 100644 index 00000000000000..ff2d57e11b824f --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/object.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-object'); diff --git a/test/fixtures/node_modules/interop-cjs/package.json b/test/fixtures/node_modules/interop-cjs/package.json new file mode 100644 index 00000000000000..68604dc25e288a --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/package.json @@ -0,0 +1,15 @@ +{ + "type": "commonjs", + "exports": { + "./fauxesmdefault": "./fauxesmdefault.js", + "./fauxesmmixed": "./fauxesmmixed.js", + "./fauxesmnamed": "./fauxesmnamed.js", + "./string": "./string.js", + "./object": "./object.js", + "./fauxesmdefault-esm": "./fauxesmdefault-esm.js", + "./fauxesmmixed-esm": "./fauxesmmixed-esm.js", + "./fauxesmnamed-esm": "./fauxesmnamed-esm.js", + "./string-esm": "./string-esm.js", + "./object-esm": "./object-esm.js" + } +} diff --git a/test/fixtures/node_modules/interop-cjs/string-esm.js b/test/fixtures/node_modules/interop-cjs/string-esm.js new file mode 100644 index 00000000000000..bdd23a1b1db4da --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/string-esm.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-string-esm'); diff --git a/test/fixtures/node_modules/interop-cjs/string.js b/test/fixtures/node_modules/interop-cjs/string.js new file mode 100644 index 00000000000000..65010183f2f76b --- /dev/null +++ b/test/fixtures/node_modules/interop-cjs/string.js @@ -0,0 +1 @@ +module.exports = require('interop-cjsdep-string'); diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/dep.js b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/dep.js new file mode 100644 index 00000000000000..7e8ba0b630a657 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/dep.js @@ -0,0 +1,7 @@ +export const __cjsUnwrapDefault = true; + +const exports = {}; +Object.defineProperty(exports, '__esModule', { value: true }); +exports.default = 'faux esm default'; + +export default exports; diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/package.json b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/package.json new file mode 100644 index 00000000000000..f5b94a85497063 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/dep.js b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/dep.js new file mode 100644 index 00000000000000..5cd0b37b27d054 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/dep.js @@ -0,0 +1,2 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.default = 'faux esm default'; diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/package.json b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/package.json new file mode 100644 index 00000000000000..e6fc9863573e3d --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmdefault/package.json @@ -0,0 +1,4 @@ +{ + "type": "commonjs", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/dep.js b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/dep.js new file mode 100644 index 00000000000000..87419fb0a03375 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/dep.js @@ -0,0 +1,9 @@ +export const __cjsUnwrapDefault = true; + +const exports = {}; +Object.defineProperty(exports, '__esModule', { value: true }); +exports.default = 'faux esm default'; +exports.a = 'faux esm a'; +exports.b = 'faux esm b'; + +export default exports; diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/package.json b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/package.json new file mode 100644 index 00000000000000..f5b94a85497063 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/dep.js b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/dep.js new file mode 100644 index 00000000000000..926ce033f829a0 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/dep.js @@ -0,0 +1,4 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.default = 'faux esm default'; +exports.a = 'faux esm a'; +exports.b = 'faux esm b'; diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/package.json b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/package.json new file mode 100644 index 00000000000000..e6fc9863573e3d --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmmixed/package.json @@ -0,0 +1,4 @@ +{ + "type": "commonjs", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/dep.js b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/dep.js new file mode 100644 index 00000000000000..b50c5d226616e2 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/dep.js @@ -0,0 +1,8 @@ +export const __cjsUnwrapDefault = true; + +const exports = {}; +Object.defineProperty(exports, '__esModule', { value: true }); +exports.a = 'faux esm a'; +exports.b = 'faux esm b'; + +export default exports; diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/package.json b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/package.json new file mode 100644 index 00000000000000..f5b94a85497063 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/dep.js b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/dep.js new file mode 100644 index 00000000000000..e3218c09b35752 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/dep.js @@ -0,0 +1,3 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.a = 'faux esm a'; +exports.b = 'faux esm b'; diff --git a/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/package.json b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/package.json new file mode 100644 index 00000000000000..e6fc9863573e3d --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-fauxesmnamed/package.json @@ -0,0 +1,4 @@ +{ + "type": "commonjs", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-object-esm/dep.js b/test/fixtures/node_modules/interop-cjsdep-object-esm/dep.js new file mode 100644 index 00000000000000..a2f711cdd13343 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-object-esm/dep.js @@ -0,0 +1,5 @@ +export const __cjsUnwrapDefault = true; +export default { + a: 'cjs a', + b: 'cjs b' +}; diff --git a/test/fixtures/node_modules/interop-cjsdep-object-esm/package.json b/test/fixtures/node_modules/interop-cjsdep-object-esm/package.json new file mode 100644 index 00000000000000..f5b94a85497063 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-object-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-object/dep.js b/test/fixtures/node_modules/interop-cjsdep-object/dep.js new file mode 100644 index 00000000000000..c37089bccc741e --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-object/dep.js @@ -0,0 +1,2 @@ +exports.a = 'cjs a'; +exports.b = 'cjs b'; diff --git a/test/fixtures/node_modules/interop-cjsdep-object/package.json b/test/fixtures/node_modules/interop-cjsdep-object/package.json new file mode 100644 index 00000000000000..e6fc9863573e3d --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-object/package.json @@ -0,0 +1,4 @@ +{ + "type": "commonjs", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-string-esm/dep.js b/test/fixtures/node_modules/interop-cjsdep-string-esm/dep.js new file mode 100644 index 00000000000000..70e4978771149b --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-string-esm/dep.js @@ -0,0 +1,2 @@ +export const __cjsUnwrapDefault = true; +export default 'cjs'; diff --git a/test/fixtures/node_modules/interop-cjsdep-string-esm/package.json b/test/fixtures/node_modules/interop-cjsdep-string-esm/package.json new file mode 100644 index 00000000000000..f5b94a85497063 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-string-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-string/dep.js b/test/fixtures/node_modules/interop-cjsdep-string/dep.js new file mode 100644 index 00000000000000..b2825bd3c9949b --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-string/dep.js @@ -0,0 +1 @@ +module.exports = 'cjs'; diff --git a/test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/dep.js b/test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/dep.js new file mode 100644 index 00000000000000..07871c3b4032d7 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/dep.js @@ -0,0 +1,2 @@ +export default 'cjs'; +export const __cjsUnwrapDefault = true; diff --git a/test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/package.json b/test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/package.json new file mode 100644 index 00000000000000..f5b94a85497063 --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-string/interop-depstring-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "exports": "./dep.js" +} diff --git a/test/fixtures/node_modules/interop-cjsdep-string/package.json b/test/fixtures/node_modules/interop-cjsdep-string/package.json new file mode 100644 index 00000000000000..e6fc9863573e3d --- /dev/null +++ b/test/fixtures/node_modules/interop-cjsdep-string/package.json @@ -0,0 +1,4 @@ +{ + "type": "commonjs", + "exports": "./dep.js" +} diff --git a/test/fixtures/pkgexports.mjs b/test/fixtures/pkgexports.mjs index 7d642c443e6b71..b71566b95c79ac 100644 --- a/test/fixtures/pkgexports.mjs +++ b/test/fixtures/pkgexports.mjs @@ -3,6 +3,10 @@ import { createRequire } from 'module'; const rawRequire = createRequire(fileURLToPath(import.meta.url)); +export function directRequireFixture(specifier) { + return rawRequire(specifier); +} + export async function requireFixture(specifier) { return { default: rawRequire(specifier ) }; }