diff --git a/packages/commonjs/README.md b/packages/commonjs/README.md index 1dd6f0723..37f63693e 100644 --- a/packages/commonjs/README.md +++ b/packages/commonjs/README.md @@ -76,21 +76,21 @@ commonjs({ Type: `string | string[]`
Default: `null` -A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default non-CommonJS modules are ignored. +A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default, all files with extensions other than those in `extensions` or `".cjs"` are ignored, but you can exclude additional files. See also the `include` option. ### `include` Type: `string | string[]`
Default: `null` -A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default CommonJS modules are targeted. +A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default, all files with extension `".cjs"` or those in `extensions` are included, but you can narrow this list by only including specific files. These files will be analyzed and transpiled if either the analysis does not find ES module specific statements or `transformMixedEsModules` is `true`. ### `extensions` Type: `string[]`
Default: `['.js']` -Search for extensions other than .js in the order specified. +For extensionless imports, search for extensions other than .js in the order specified. Note that you need to make sure that non-JavaScript files are transpiled by another plugin first. ### `ignoreGlobal` @@ -104,28 +104,28 @@ If true, uses of `global` won't be dealt with by this plugin. Type: `boolean`
Default: `true` -If false, skips source map generation for CommonJS modules. +If false, skips source map generation for CommonJS modules. This will improve performance. ### `transformMixedEsModules` Type: `boolean`
Default: `false` -Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation. +Instructs the plugin whether to enable mixed module transformations. This is useful in scenarios with modules that contain a mix of ES `import` statements and CommonJS `require` expressions. Set to `true` if `require` calls should be transformed to imports in mixed modules, or `false` if the `require` expressions should survive the transformation. The latter can be important if the code contains environment detection, or you are coding for an environment with special treatment for `require` calls such as [ElectronJS](https://www.electronjs.org/). See also the "ignore" option. ### `ignore` Type: `string[] | ((id: string) => boolean)`
Default: `[]` -Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function. Only use this option if you know what you're doing! +Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function. ### `esmExternals` -Type: `boolean | string[] || ((id: string) => boolean)` +Type: `boolean | string[] | ((id: string) => boolean)` Default: `false` -Controls how imports from external dependencies are rendered. By default, all external dependencies are assumed to be CommonJS. This means they are rendered as default imports to be compatible with e.g. NodeJS where ES modules can only import a default export from a CommonJS dependency: +Controls how to render imports from external dependencies. By default, this plugin assumes that all external dependencies are CommonJS. This means they are rendered as default imports to be compatible with e.g. NodeJS where ES modules can only import a default export from a CommonJS dependency: ```js // input @@ -137,16 +137,16 @@ import foo from 'foo'; This is likely not desired for ES module dependencies: Here `require` should usually return the namespace to be compatible with how bundled modules are handled. -If you set `esmExternals` to `true`, all external dependencies are assumed to be ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports. +If you set `esmExternals` to `true`, this plugins assumes that all external dependencies are ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports. -You can also supply an array of ids that are to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module. +You can also supply an array of ids to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module. ### `requireReturnsDefault` Type: `boolean | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`
Default: `false` -Controls what is returned when requiring an ES module or external dependency from a CommonJS file. By default, this plugin will render it as a namespace import, i.e. +Controls what is returned when requiring an ES module from a CommonJS file. When using the `esmExternals` option, this will also apply to external modules. By default, this plugin will render those imports as namespace imports, i.e. ```js // input @@ -182,13 +182,44 @@ This is in line with how other bundlers handle this situation and is also the mo For these situations, you can change Rollup's behaviour either globally or per module. To change it globally, set the `requireReturnsDefault` option to one of the following values: -- `false`: This is the default, requiring an ES module returns its namespace. For external dependencies when using `esmExternals: true`, no additional interop code is generated: +- `false`: This is the default, requiring an ES module returns its namespace. This is the only option that will also add a marker `__esModule: true` to the namespace to support interop patterns in CommonJS modules that are transpiled ES modules. ```js // input const dep = require('dep'); console.log(dep); + // output + import * as dep$1 from 'dep'; + + function getAugmentedNamespace(n) { + var a = Object.defineProperty({}, '__esModule', { value: true }); + Object.keys(n).forEach(function (k) { + var d = Object.getOwnPropertyDescriptor(n, k); + Object.defineProperty( + a, + k, + d.get + ? d + : { + enumerable: true, + get: function () { + return n[k]; + }, + } + ); + }); + return a; + } + + var dep = /*@__PURE__*/ getAugmentedNamespace(dep$1); + + console.log(dep); + ``` + +- `"namespace"`: Like `false`, requiring an ES module returns its namespace, but the plugin does not add the `__esModule` marker and thus creates more efficient code. For external dependencies when using `esmExternals: true`, no additional interop code is generated. + + ```js // output import * as dep from 'dep'; diff --git a/packages/commonjs/src/helpers.js b/packages/commonjs/src/helpers.js index b2f50fe2d..461e21f90 100644 --- a/packages/commonjs/src/helpers.js +++ b/packages/commonjs/src/helpers.js @@ -37,11 +37,11 @@ export function getDefaultExportFromCjs (x) { export function createCommonjsModule(fn, basedir, module) { return module = { - path: basedir, - exports: {}, - require: function (path, base) { - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); - } + path: basedir, + exports: {}, + require: function (path, base) { + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base); + } }, fn(module, module.exports), module.exports; } @@ -52,6 +52,21 @@ export function getDefaultExportFromNamespaceIfPresent (n) { export function getDefaultExportFromNamespaceIfNotNamed (n) { return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n; } + +export function getAugmentedNamespace(n) { + if (n.__esModule) return n; + var a = Object.defineProperty({}, '__esModule', {value: true}); + Object.keys(n).forEach(function (k) { + var d = Object.getOwnPropertyDescriptor(n, k); + Object.defineProperty(a, k, d.get ? d : { + enumerable: true, + get: function () { + return n[k]; + } + }); + }); + return a; +} `; const HELPER_NON_DYNAMIC = ` diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index cc89e0793..646da0e18 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -189,11 +189,14 @@ export default function commonjs(options = {}) { transform(code, id) { const extName = extname(id); - if (extName !== '.cjs' && id !== DYNAMIC_PACKAGES_ID && !id.startsWith(DYNAMIC_JSON_PREFIX)) { - if (!filter(id) || !extensions.includes(extName)) { - setIsCjsPromise(id, null); - return null; - } + if ( + extName !== '.cjs' && + id !== DYNAMIC_PACKAGES_ID && + !id.startsWith(DYNAMIC_JSON_PREFIX) && + (!filter(id) || !extensions.includes(extName)) + ) { + setIsCjsPromise(id, null); + return null; } let transformed; diff --git a/packages/commonjs/src/proxies.js b/packages/commonjs/src/proxies.js index 27e2e2922..40ed42657 100644 --- a/packages/commonjs/src/proxies.js +++ b/packages/commonjs/src/proxies.js @@ -17,10 +17,12 @@ export function getUnknownRequireProxy(id, requireReturnsDefault) { const name = getName(id); const exported = requireReturnsDefault === 'auto' - ? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name})` + ? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});` : requireReturnsDefault === 'preferred' - ? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name})` - : `export default ${name}`; + ? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});` + : !requireReturnsDefault + ? `import {getAugmentedNamespace} from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});` + : `export default ${name};`; return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`; } @@ -53,11 +55,15 @@ export async function getStaticRequireProxy( return `import { __moduleExports } from ${JSON.stringify(id)}; export default __moduleExports;`; } else if (isCjs === null) { return getUnknownRequireProxy(id, requireReturnsDefault); + } else if (!requireReturnsDefault) { + return `import {getAugmentedNamespace} from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify( + id + )}; export default /*@__PURE__*/getAugmentedNamespace(${name});`; } else if ( requireReturnsDefault !== true && - (!requireReturnsDefault || + (requireReturnsDefault === 'namespace' || !esModulesWithDefaultExport.has(id) || - (esModulesWithNamedExports.has(id) && requireReturnsDefault === 'auto')) + (requireReturnsDefault === 'auto' && esModulesWithNamedExports.has(id))) ) { return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`; } diff --git a/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js b/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js index 608ac5468..72264b8a3 100644 --- a/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js +++ b/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js @@ -1,13 +1,7 @@ module.exports = { description: 'always uses the default export when esmExternals is not used', options: { - external: [ - 'external-cjs-exports', - 'external-cjs-module-exports', - 'external-esm-named', - 'external-esm-mixed', - 'external-esm-default' - ] + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] }, pluginOptions: { esmExternals: true diff --git a/packages/commonjs/test/fixtures/function/esm-externals-true/main.js b/packages/commonjs/test/fixtures/function/esm-externals-true/main.js index ca7736577..ccabc2b18 100644 --- a/packages/commonjs/test/fixtures/function/esm-externals-true/main.js +++ b/packages/commonjs/test/fixtures/function/esm-externals-true/main.js @@ -1,11 +1,7 @@ -const externalExports = require('external-cjs-exports'); -const externalModuleExports = require('external-cjs-module-exports'); const externalNamed = require('external-esm-named'); const externalMixed = require('external-esm-mixed'); const externalDefault = require('external-esm-default'); -t.deepEqual(externalExports, { foo: 'foo' }, 'external exports'); -t.deepEqual(externalModuleExports, 'bar', 'external module exports'); t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); t.deepEqual(externalMixed, { default: 'bar', foo: 'foo' }, 'external mixed'); t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/_config.js new file mode 100644 index 000000000..f678abe03 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: + 'returns the namespace when requiring an ES module and requireReturnsDefault is "namespace"', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + requireReturnsDefault: 'namespace', + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/default.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/default.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/default.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/main.js new file mode 100644 index 000000000..658db2cdd --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/main.js @@ -0,0 +1,16 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +const namedExports = require('./named.js'); +const mixedExports = require('./mixed.js'); +const defaultExport = require('./default.js'); +const noExports = require('./none.js'); + +t.deepEqual(namedExports, { foo: 'foo' }, 'named exports'); +t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports'); +t.deepEqual(defaultExport, { default: 'bar' }, 'default export'); +t.deepEqual(noExports, {}, 'no exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/mixed.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/mixed.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/mixed.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/named.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/named.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/named.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/none.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-namespace/none.js new file mode 100644 index 000000000..e69de29bb diff --git a/packages/commonjs/test/fixtures/function/import-esm-with-interop/_config.js b/packages/commonjs/test/fixtures/function/import-esm-with-interop/_config.js new file mode 100644 index 000000000..69ae7b6d9 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-with-interop/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'adds the __esModule property when requiring an ES module and support live-bindings' +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-with-interop/lib.js b/packages/commonjs/test/fixtures/function/import-esm-with-interop/lib.js new file mode 100644 index 000000000..e6ae96c7b --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-with-interop/lib.js @@ -0,0 +1,9 @@ +/* eslint-disable import/no-mutable-exports */ +let foo = 'foo'; +let bar = 'bar'; +export { foo as default, bar }; + +export function update(newFoo, newBar) { + foo = newFoo; + bar = newBar; +} diff --git a/packages/commonjs/test/fixtures/function/import-esm-with-interop/main.js b/packages/commonjs/test/fixtures/function/import-esm-with-interop/main.js new file mode 100644 index 000000000..ba5ccfc37 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-with-interop/main.js @@ -0,0 +1,15 @@ +/* eslint-disable */ +var lib = require('./lib.js'); + +function _interopDefault(e) { + return e && e.__esModule ? e : { default: e }; +} + +var lib__default = /*#__PURE__*/_interopDefault(lib); +t.is(lib__default['default'], 'foo') +t.is(lib.bar, 'bar') + +lib.update('newFoo', 'newBar'); +t.is(lib__default['default'], 'newFoo') +t.is(lib.bar, 'newBar') + diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index 913a5d27c..05c8098dd 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -33,11 +33,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -228,11 +228,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -2823,11 +2823,28 @@ Generated by [AVA](https://avajs.dev). var externalMixed = _interopDefault(require('external-esm-mixed'));␊ var externalEsmDefault = require('external-esm-default');␊ ␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var externalDefault = /*@__PURE__*/getAugmentedNamespace(externalEsmDefault);␊ + ␊ t.deepEqual(externalExports, { foo: 'foo' }, 'external exports');␊ t.deepEqual(externalModuleExports, 'bar', 'external module exports');␊ t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ t.deepEqual(externalMixed, 'bar', 'external mixed');␊ - t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + t.deepEqual(externalDefault, { default: 'bar' }, 'external default');␊ ␊ var main = {␊ ␊ @@ -2852,11 +2869,28 @@ Generated by [AVA](https://avajs.dev). var externalMixed = _interopDefault(require('external-esm-mixed'));␊ var externalEsmDefault = require('external-esm-default');␊ ␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var externalDefault = /*@__PURE__*/getAugmentedNamespace(externalEsmDefault);␊ + ␊ t.deepEqual(externalExports, { foo: 'foo' }, 'external exports');␊ t.deepEqual(externalModuleExports, 'bar', 'external module exports');␊ t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ t.deepEqual(externalMixed, 'bar', 'external mixed');␊ - t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + t.deepEqual(externalDefault, { default: 'bar' }, 'external default');␊ ␊ var main = {␊ ␊ @@ -2873,17 +2907,34 @@ Generated by [AVA](https://avajs.dev). { 'main.js': `'use strict';␊ ␊ - var externalCjsExports = require('external-cjs-exports');␊ - var externalCjsModuleExports = require('external-cjs-module-exports');␊ var externalEsmNamed = require('external-esm-named');␊ var externalEsmMixed = require('external-esm-mixed');␊ var externalEsmDefault = require('external-esm-default');␊ ␊ - t.deepEqual(externalCjsExports, { foo: 'foo' }, 'external exports');␊ - t.deepEqual(externalCjsModuleExports, 'bar', 'external module exports');␊ - t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ - t.deepEqual(externalEsmMixed, { default: 'bar', foo: 'foo' }, 'external mixed');␊ - t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var externalNamed = /*@__PURE__*/getAugmentedNamespace(externalEsmNamed);␊ + ␊ + var externalMixed = /*@__PURE__*/getAugmentedNamespace(externalEsmMixed);␊ + ␊ + var externalDefault = /*@__PURE__*/getAugmentedNamespace(externalEsmDefault);␊ + ␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, { default: 'bar', foo: 'foo' }, 'external mixed');␊ + t.deepEqual(externalDefault, { default: 'bar' }, 'external default');␊ ␊ var main = {␊ ␊ @@ -3000,8 +3051,25 @@ Generated by [AVA](https://avajs.dev). two: two␊ });␊ ␊ - t.is(foo.one, 1);␊ - t.is(foo.two, 2);␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var foo$1 = /*@__PURE__*/getAugmentedNamespace(foo);␊ + ␊ + t.is(foo$1.one, 1);␊ + t.is(foo$1.two, 2);␊ ␊ var main = {␊ ␊ @@ -3024,11 +3092,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -3181,13 +3249,42 @@ Generated by [AVA](https://avajs.dev). __proto__: null␊ });␊ ␊ - t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ - t.deepEqual(mixed$1, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ - t.deepEqual(_default$1, { default: 'bar' }, 'default export');␊ - t.deepEqual(none, {}, 'no exports');␊ - t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ - t.deepEqual(externalEsmMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ - t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var externalNamed = /*@__PURE__*/getAugmentedNamespace(externalEsmNamed);␊ + ␊ + var externalMixed = /*@__PURE__*/getAugmentedNamespace(externalEsmMixed);␊ + ␊ + var externalDefault = /*@__PURE__*/getAugmentedNamespace(externalEsmDefault);␊ + ␊ + var namedExports = /*@__PURE__*/getAugmentedNamespace(named);␊ + ␊ + var mixedExports = /*@__PURE__*/getAugmentedNamespace(mixed$1);␊ + ␊ + var defaultExport = /*@__PURE__*/getAugmentedNamespace(_default$1);␊ + ␊ + var noExports = /*@__PURE__*/getAugmentedNamespace(none);␊ + ␊ + t.deepEqual(namedExports, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ + t.deepEqual(defaultExport, { default: 'bar' }, 'default export');␊ + t.deepEqual(noExports, {}, 'no exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ + t.deepEqual(externalDefault, { default: 'bar' }, 'external default');␊ ␊ var main = {␊ ␊ @@ -3258,9 +3355,30 @@ Generated by [AVA](https://avajs.dev). ␊ var trueMixed = 'default';␊ ␊ - t.deepEqual(dep_false_default_$1, { default: 'default' }, 'false default');␊ - t.deepEqual(dep_false_mixed_$1, { default: 'default', named: 'named' }, 'false mixed');␊ - t.deepEqual(dep_false_named_, { named: 'named' }, 'false named');␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var falseDefault = /*@__PURE__*/getAugmentedNamespace(dep_false_default_$1);␊ + ␊ + var falseMixed = /*@__PURE__*/getAugmentedNamespace(dep_false_mixed_$1);␊ + ␊ + var falseNamed = /*@__PURE__*/getAugmentedNamespace(dep_false_named_);␊ + ␊ + t.deepEqual(falseDefault, { default: 'default' }, 'false default');␊ + t.deepEqual(falseMixed, { default: 'default', named: 'named' }, 'false mixed');␊ + t.deepEqual(falseNamed, { named: 'named' }, 'false named');␊ ␊ t.deepEqual(autoDefault, 'default', 'auto default');␊ t.deepEqual(dep_auto_mixed_$1, { default: 'default', named: 'named' }, 'auto mixed');␊ @@ -3325,11 +3443,82 @@ Generated by [AVA](https://avajs.dev). return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;␊ }␊ ␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ var externalNamed = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(externalEsmNamed);␊ ␊ + var externalDefault = /*@__PURE__*/getAugmentedNamespace(externalEsmDefault);␊ + ␊ t.deepEqual(externalNamed, { foo: 'foo' }, 'named');␊ t.deepEqual(externalMixed, 'bar', 'mixed');␊ - t.deepEqual(externalEsmDefault, { default: 'bar' }, 'default');␊ + t.deepEqual(externalDefault, { default: 'bar' }, 'default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-namespace + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalEsmMixed = require('external-esm-mixed');␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + const foo = 'foo';␊ + ␊ + var named = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo␊ + });␊ + ␊ + const foo$1 = 'foo';␊ + var mixed = 'bar';␊ + ␊ + var mixed$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo$1,␊ + 'default': mixed␊ + });␊ + ␊ + var _default = 'bar';␊ + ␊ + var _default$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': _default␊ + });␊ + ␊ + var none = /*#__PURE__*/Object.freeze({␊ + __proto__: null␊ + });␊ + ␊ + t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixed$1, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ + t.deepEqual(_default$1, { default: 'bar' }, 'default export');␊ + t.deepEqual(none, {}, 'no exports');␊ + t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalEsmMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ ␊ var main = {␊ ␊ @@ -3460,13 +3649,42 @@ Generated by [AVA](https://avajs.dev). __proto__: null␊ });␊ ␊ - t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ - t.deepEqual(mixed$1, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ - t.deepEqual(_default$1, { default: 'bar' }, 'default export');␊ - t.deepEqual(none, {}, 'no exports');␊ - t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ - t.deepEqual(externalEsmMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ - t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var externalNamed = /*@__PURE__*/getAugmentedNamespace(externalEsmNamed);␊ + ␊ + var externalMixed = /*@__PURE__*/getAugmentedNamespace(externalEsmMixed);␊ + ␊ + var externalDefault = /*@__PURE__*/getAugmentedNamespace(externalEsmDefault);␊ + ␊ + var namedExports = /*@__PURE__*/getAugmentedNamespace(named);␊ + ␊ + var mixedExports = /*@__PURE__*/getAugmentedNamespace(mixed$1);␊ + ␊ + var defaultExport = /*@__PURE__*/getAugmentedNamespace(_default$1);␊ + ␊ + var noExports = /*@__PURE__*/getAugmentedNamespace(none);␊ + ␊ + t.deepEqual(namedExports, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ + t.deepEqual(defaultExport, { default: 'bar' }, 'default export');␊ + t.deepEqual(noExports, {}, 'no exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ + t.deepEqual(externalDefault, { default: 'bar' }, 'external default');␊ ␊ var main = {␊ ␊ @@ -3476,6 +3694,87 @@ Generated by [AVA](https://avajs.dev). `, } +## import-esm-with-interop + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function getDefaultExportFromCjs (x) {␊ + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ + }␊ + ␊ + function createCommonjsModule(fn, basedir, module) {␊ + return module = {␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ + }, fn(module, module.exports), module.exports;␊ + }␊ + ␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + function commonjsRequire () {␊ + throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');␊ + }␊ + ␊ + /* eslint-disable import/no-mutable-exports */␊ + let foo = 'foo';␊ + let bar = 'bar';␊ + ␊ + function update(newFoo, newBar) {␊ + foo = newFoo;␊ + bar = newBar;␊ + }␊ + ␊ + var lib = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + get default () { return foo; },␊ + get bar () { return bar; },␊ + update: update␊ + });␊ + ␊ + var lib$1 = /*@__PURE__*/getAugmentedNamespace(lib);␊ + ␊ + var main = createCommonjsModule(function (module) {␊ + /* eslint-disable */␊ + ␊ + ␊ + function _interopDefault(e) {␊ + return e && e.__esModule ? e : { default: e };␊ + }␊ + ␊ + var lib__default = /*#__PURE__*/_interopDefault(lib$1);␊ + t.is(lib__default['default'], 'foo');␊ + t.is(lib$1.bar, 'bar');␊ + ␊ + lib$1.update('newFoo', 'newBar');␊ + t.is(lib__default['default'], 'newFoo');␊ + t.is(lib$1.bar, 'newBar');␊ + });␊ + ␊ + var main$1 = /*@__PURE__*/getDefaultExportFromCjs(main);␊ + ␊ + module.exports = main$1;␊ + `, + } + ## index > Snapshot 1 @@ -3581,11 +3880,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -3614,11 +3913,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -3649,11 +3948,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -3783,11 +4082,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -3971,7 +4270,24 @@ Generated by [AVA](https://avajs.dev). result: result␊ });␊ ␊ - t.is(second.result, 'second');␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var require$$0 = /*@__PURE__*/getAugmentedNamespace(second);␊ + ␊ + t.is(require$$0.result, 'second');␊ ␊ var main = {␊ ␊ @@ -3995,7 +4311,24 @@ Generated by [AVA](https://avajs.dev). result: result␊ });␊ ␊ - t.is(second.result, 'second');␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var require$$0 = /*@__PURE__*/getAugmentedNamespace(second);␊ + ␊ + t.is(require$$0.result, 'second');␊ ␊ var main = {␊ ␊ @@ -4098,11 +4431,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4156,8 +4489,25 @@ Generated by [AVA](https://avajs.dev). other: other␊ });␊ ␊ + function getAugmentedNamespace(n) {␊ + if (n.__esModule) return n;␊ + var a = Object.defineProperty({}, '__esModule', {value: true});␊ + Object.keys(n).forEach(function (k) {␊ + var d = Object.getOwnPropertyDescriptor(n, k);␊ + Object.defineProperty(a, k, d.get ? d : {␊ + enumerable: true,␊ + get: function () {␊ + return n[k];␊ + }␊ + });␊ + });␊ + return a;␊ + }␊ + ␊ + var dep$1 = /*@__PURE__*/getAugmentedNamespace(dep);␊ + ␊ t.is(other, 'other');␊ - t.deepEqual(dep, { other: 'other' });␊ + t.deepEqual(dep$1, { other: 'other' });␊ `, } @@ -4178,11 +4528,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4207,7 +4557,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 { - 'entry-becd84f0.js': `'use strict';␊ + 'entry-a2520c87.js': `'use strict';␊ ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ @@ -4215,11 +4565,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4243,7 +4593,7 @@ Generated by [AVA](https://avajs.dev). `, 'entry.js': `'use strict';␊ ␊ - var entry = require('./entry-becd84f0.js');␊ + var entry = require('./entry-a2520c87.js');␊ ␊ ␊ ␊ @@ -4251,7 +4601,7 @@ Generated by [AVA](https://avajs.dev). `, 'main.js': `'use strict';␊ ␊ - var entry = require('./entry-becd84f0.js');␊ + var entry = require('./entry-a2520c87.js');␊ ␊ t.deepEqual(entry.entry, { default: 'default' });␊ `, @@ -4262,7 +4612,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 { - 'entry-2e56ab7c.js': `'use strict';␊ + 'entry-44884401.js': `'use strict';␊ ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ @@ -4270,11 +4620,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4299,7 +4649,7 @@ Generated by [AVA](https://avajs.dev). `, 'entry.js': `'use strict';␊ ␊ - var entry = require('./entry-2e56ab7c.js');␊ + var entry = require('./entry-44884401.js');␊ ␊ ␊ ␊ @@ -4307,7 +4657,7 @@ Generated by [AVA](https://avajs.dev). `, 'main.js': `'use strict';␊ ␊ - var entry = require('./entry-2e56ab7c.js');␊ + var entry = require('./entry-44884401.js');␊ ␊ t.deepEqual(entry.entry, { default: 'default', named: 'named' });␊ `, @@ -4318,7 +4668,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 { - 'entry-00c7c97c.js': `'use strict';␊ + 'entry-3a4176ef.js': `'use strict';␊ ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ @@ -4326,11 +4676,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4354,7 +4704,7 @@ Generated by [AVA](https://avajs.dev). `, 'entry.js': `'use strict';␊ ␊ - var entry = require('./entry-00c7c97c.js');␊ + var entry = require('./entry-3a4176ef.js');␊ ␊ ␊ ␊ @@ -4362,7 +4712,7 @@ Generated by [AVA](https://avajs.dev). `, 'main.js': `'use strict';␊ ␊ - var entry = require('./entry-00c7c97c.js');␊ + var entry = require('./entry-3a4176ef.js');␊ ␊ t.deepEqual(entry.entry, {␊ // Technically, this should ideally not exist, or if we cannot avoid it due␊ @@ -4394,11 +4744,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4432,11 +4782,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4469,11 +4819,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4517,11 +4867,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4568,11 +4918,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4616,11 +4966,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ @@ -4649,11 +4999,11 @@ Generated by [AVA](https://avajs.dev). ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ - path: basedir,␊ - exports: {},␊ - require: function (path, base) {␊ - return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ - }␊ + path: basedir,␊ + exports: {},␊ + require: function (path, base) {␊ + return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);␊ + }␊ }, fn(module, module.exports), module.exports;␊ }␊ ␊ diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 244c8fc65..9503cbcd6 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ diff --git a/packages/commonjs/test/test.js b/packages/commonjs/test/test.js index 120875d87..19cb9ac48 100644 --- a/packages/commonjs/test/test.js +++ b/packages/commonjs/test/test.js @@ -574,7 +574,24 @@ var esm = /*#__PURE__*/Object.freeze({ value: value }); -var main = esm; +function getAugmentedNamespace(n) { + if (n.__esModule) return n; + var a = Object.defineProperty({}, '__esModule', {value: true}); + Object.keys(n).forEach(function (k) { + var d = Object.getOwnPropertyDescriptor(n, k); + Object.defineProperty(a, k, d.get ? d : { + enumerable: true, + get: function () { + return n[k]; + } + }); + }); + return a; +} + +var require$$0 = /*@__PURE__*/getAugmentedNamespace(esm); + +var main = require$$0; module.exports = main; ` diff --git a/packages/commonjs/test/types.ts b/packages/commonjs/test/types.ts index 0dd4449b6..b0844a7ff 100644 --- a/packages/commonjs/test/types.ts +++ b/packages/commonjs/test/types.ts @@ -9,9 +9,11 @@ const config: import('rollup').RollupOptions = { plugins: [ commonjs({ include: 'node_modules/**', + esmExternals: ['foo', 'bar'], exclude: ['node_modules/foo/**', 'node_modules/bar/**', /node_modules/], extensions: ['.js', '.coffee'], ignoreGlobal: false, + requireReturnsDefault: 'auto', sourceMap: false, transformMixedEsModules: false, ignore: ['conditional-runtime-dependency'], diff --git a/packages/commonjs/types/index.d.ts b/packages/commonjs/types/index.d.ts index 6157114dd..b7d8dedf8 100644 --- a/packages/commonjs/types/index.d.ts +++ b/packages/commonjs/types/index.d.ts @@ -1,64 +1,85 @@ import { FilterPattern } from '@rollup/pluginutils'; import { Plugin } from 'rollup'; +type RequireReturnsDefaultOption = boolean | 'auto' | 'preferred' | 'namespace'; + interface RollupCommonJSOptions { /** - * non-CommonJS modules will be ignored, but you can also - * specifically include/exclude files + * A minimatch pattern, or array of patterns, which specifies the files in + * the build the plugin should operate on. By default, all files with + * extension `".cjs"` or those in `extensions` are included, but you can narrow + * this list by only including specific files. These files will be analyzed + * and transpiled if either the analysis does not find ES module specific + * statements or `transformMixedEsModules` is `true`. * @default undefined */ include?: FilterPattern; /** - * non-CommonJS modules will be ignored, but you can also - * specifically include/exclude files + * A minimatch pattern, or array of patterns, which specifies the files in + * the build the plugin should _ignore_. By default, all files with + * extensions other than those in `extensions` or `".cjs"` are ignored, but you + * can exclude additional files. See also the `include` option. * @default undefined */ exclude?: FilterPattern; /** - * search for files other than .js files (must already - * be transpiled by a previous plugin!) + * For extensionless imports, search for extensions other than .js in the + * order specified. Note that you need to make sure that non-JavaScript files + * are transpiled by another plugin first. * @default [ '.js' ] */ - extensions?: ReadonlyArray; + extensions?: ReadonlyArray; /** - * if true then uses of `global` won't be dealt with by this plugin + * If true then uses of `global` won't be dealt with by this plugin * @default false */ ignoreGlobal?: boolean; /** - * if false then skip sourceMap generation for CommonJS modules + * If false, skips source map generation for CommonJS modules. This will improve performance. * @default true */ sourceMap?: boolean; /** - * Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation. + * Instructs the plugin whether to enable mixed module transformations. This + * is useful in scenarios with modules that contain a mix of ES `import` + * statements and CommonJS `require` expressions. Set to `true` if `require` + * calls should be transformed to imports in mixed modules, or `false` if the + * `require` expressions should survive the transformation. The latter can be + * important if the code contains environment detection, or you are coding + * for an environment with special treatment for `require` calls such as + * ElectronJS. See also the `ignore` option. * @default false */ transformMixedEsModules?: boolean; /** - * sometimes you have to leave require statements - * unconverted. Pass an array containing the IDs - * or a `id => boolean` function. Only use this - * option if you know what you're doing! + * Sometimes you have to leave require statements unconverted. Pass an array + * containing the IDs or a `id => boolean` function. + * @default [] */ - ignore?: ReadonlyArray boolean)>; + ignore?: ReadonlyArray | ((id: string) => boolean); /** - * Some modules contain dynamic `require` calls, or require modules that contain - * circular dependencies, which are not handled well by static imports. - * Including those modules as `dynamicRequireTargets` will simulate a CommonJS (NodeJS-like) - * environment for them with support for dynamic and circular dependencies. + * Controls how to render imports from external dependencies. By default, + * this plugin assumes that all external dependencies are CommonJS. This + * means they are rendered as default imports to be compatible with e.g. + * NodeJS where ES modules can only import a default export from a CommonJS + * dependency. + * + * If you set `esmExternals` to `true`, this plugins assumes that all + * external dependencies are ES modules and respect the + * `requireReturnsDefault` option. If that option is not set, they will be + * rendered as namespace imports. * - * Note: In extreme cases, this feature may result in some paths being rendered as - * absolute in the final bundle. The plugin tries to avoid exposing paths from - * the local machine, but if you are `dynamicRequirePaths` with paths that are - * far away from your project's folder, that may require replacing strings - * like `"/Users/John/Desktop/foo-project/"` -> `"/"`. + * You can also supply an array of ids to be treated as ES modules, or a + * function that will be passed each external id to determine if it is an ES + * module. + * @default false */ - dynamicRequireTargets?: string | ReadonlyArray; + esmExternals?: boolean | ReadonlyArray | ((id: string) => boolean); /** - * Controls what is returned when requiring an ES module or external dependency - * from a CommonJS file. By default, this plugin will render it as a namespace - * import, i.e. + * Controls what is returned when requiring an ES module from a CommonJS file. + * When using the `esmExternals` option, this will also apply to external + * modules. By default, this plugin will render those imports as namespace + * imports i.e. * * ```js * // input @@ -68,9 +89,59 @@ interface RollupCommonJSOptions { * import * as foo from 'foo'; * ``` * + * However there are some situations where this may not be desired. + * For these situations, you can change Rollup's behaviour either globally or + * per module. To change it globally, set the `requireReturnsDefault` option + * to one of the following values: + * + * - `false`: This is the default, requiring an ES module returns its + * namespace. This is the only option that will also add a marker + * `__esModule: true` to the namespace to support interop patterns in + * CommonJS modules that are transpiled ES modules. + * - `"namespace"`: Like `false`, requiring an ES module returns its + * namespace, but the plugin does not add the `__esModule` marker and thus + * creates more efficient code. For external dependencies when using + * `esmExternals: true`, no additional interop code is generated. + * - `"auto"`: This is complementary to how `output.exports: "auto"` works in + * Rollup: If a module has a default export and no named exports, requiring + * that module returns the default export. In all other cases, the namespace + * is returned. For external dependencies when using `esmExternals: true`, a + * corresponding interop helper is added. + * - `"preferred"`: If a module has a default export, requiring that module + * always returns the default export, no matter whether additional named + * exports exist. This is similar to how previous versions of this plugin + * worked. Again for external dependencies when using `esmExternals: true`, + * an interop helper is added. + * - `true`: This will always try to return the default export on require + * without checking if it actually exists. This can throw at build time if + * there is no default export. This is how external dependencies are handled + * when `esmExternals` is not used. The advantage over the other options is + * that, like `false`, this does not add an interop helper for external + * dependencies, keeping the code lean. + * + * To change this for individual modules, you can supply a function for + * `requireReturnsDefault` instead. This function will then be called once for + * each required ES module or external dependency with the corresponding id + * and allows you to return different values for different modules. * @default false */ - requireReturnsDefault?: boolean | 'auto' | 'preferred' | ((id: string) => boolean | 'auto' | 'preferred'); + requireReturnsDefault?: + | RequireReturnsDefaultOption + | ((id: string) => RequireReturnsDefaultOption); + /** + * Some modules contain dynamic `require` calls, or require modules that + * contain circular dependencies, which are not handled well by static + * imports. Including those modules as `dynamicRequireTargets` will simulate a + * CommonJS (NodeJS-like) environment for them with support for dynamic and + * circular dependencies. + * + * Note: In extreme cases, this feature may result in some paths being + * rendered as absolute in the final bundle. The plugin tries to avoid + * exposing paths from the local machine, but if you are `dynamicRequirePaths` + * with paths that are far away from your project's folder, that may require + * replacing strings like `"/Users/John/Desktop/foo-project/"` -> `"/"`. + */ + dynamicRequireTargets?: string | ReadonlyArray; } /**