diff --git a/cli/help.md b/cli/help.md index 54bd434fe28..eeea7cce45e 100644 --- a/cli/help.md +++ b/cli/help.md @@ -29,6 +29,7 @@ Basic options: --chunkFileNames Name pattern for emitted secondary chunks --compact Minify wrapper code --context Specify top-level `this` value +--no-dynamicImportInCjs Write external dynamic CommonJS imports as require --entryFileNames Name pattern for emitted entry chunks --environment Settings passed to config file (see example) --no-esModule Do not add __esModule property diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index be1c8a72a00..7690bf07965 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -360,6 +360,7 @@ Many options have command line equivalents. In those cases, any arguments passed --chunkFileNames Name pattern for emitted secondary chunks --compact Minify wrapper code --context Specify top-level `this` value +--no-dynamicImportInCjs Write external dynamic CommonJS imports as require --entryFileNames Name pattern for emitted entry chunks --environment Settings passed to config file (see example) --no-esModule Do not add __esModule property diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index 838bfcd3ffa..3d74dfecba3 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -466,6 +466,52 @@ Type: `boolean`
CLI: `--compact`/`--no-compact`
Default: `false` This will minify the wrapper code generated by rollup. Note that this does not affect code written by the user. This option is useful when bundling pre-minified code. +#### output.dynamicImportInCjs + +Type: `boolean`
CLI: `--dynamicImportInCjs`/`--no-dynamicImportInCjs`
Default: `true` + +While CommonJS output originally supported only `require(…)` to import dependencies, recent Node versions also started to support `import(…)`, which is the only way to import ES modules from CommonJS files. If this option is `true`, which is the default, Rollup will keep external dynamic imports as `import(…)` expressions in CommonJS output. Set this to `false` to rewrite dynamic imports using `require(…)` syntax. + +```js +// input +import('external').then(console.log); + +// cjs output with dynamicImportInCjs: true or not set +import('external').then(console.log); + +// cjs output with dynamicImportInCjs: false +function _interopNamespaceDefault(e) { + var n = Object.create(null); + if (e) { + Object.keys(e).forEach(function (k) { + if (k !== 'default') { + var d = Object.getOwnPropertyDescriptor(e, k); + Object.defineProperty( + n, + k, + d.get + ? d + : { + enumerable: true, + get: function () { + return e[k]; + } + } + ); + } + }); + } + n.default = e; + return Object.freeze(n); +} + +Promise.resolve() + .then(function () { + return /*#__PURE__*/ _interopNamespaceDefault(require('external')); + }) + .then(console.log); +``` + #### output.entryFileNames Type: `string | ((chunkInfo: ChunkInfo) => string)`
CLI: `--entryFileNames `
Default: `"[name].js"` diff --git a/src/ast/nodes/ImportExpression.ts b/src/ast/nodes/ImportExpression.ts index 316d7ea9377..10346a5651a 100644 --- a/src/ast/nodes/ImportExpression.ts +++ b/src/ast/nodes/ImportExpression.ts @@ -1,6 +1,6 @@ import type MagicString from 'magic-string'; import ExternalModule from '../../ExternalModule'; -import type Module from '../../Module'; +import Module from '../../Module'; import type { GetInterop, NormalizedOutputOptions } from '../../rollup/types'; import type { PluginDriver } from '../../utils/PluginDriver'; import type { GenerateCodeSnippets } from '../../utils/generateCodeSnippets'; @@ -134,6 +134,7 @@ export default class ImportExpression extends NodeBase { { compact, dynamicImportFunction, + dynamicImportInCjs, format, generatedCode: { arrowFunctions }, interop @@ -156,6 +157,12 @@ export default class ImportExpression extends NodeBase { const hasDynamicTarget = !this.resolution || typeof this.resolution === 'string'; switch (format) { case 'cjs': { + if ( + dynamicImportInCjs && + (!resolution || typeof resolution === 'string' || resolution instanceof ExternalModule) + ) { + return { helper: null, mechanism: null }; + } const helper = getInteropHelper(resolution, exportMode, interop); let left = `require(`; let right = `)`; diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 0eed05d26b8..1086dd6c900 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -613,6 +613,7 @@ export interface OutputOptions { dir?: string; /** @deprecated Use the "renderDynamicImport" plugin hook instead. */ dynamicImportFunction?: string; + dynamicImportInCjs?: boolean; entryFileNames?: string | ((chunkInfo: PreRenderedChunk) => string); esModule?: boolean | 'if-default-prop'; exports?: 'default' | 'named' | 'none' | 'auto'; @@ -663,6 +664,7 @@ export interface NormalizedOutputOptions { dir: string | undefined; /** @deprecated Use the "renderDynamicImport" plugin hook instead. */ dynamicImportFunction: string | undefined; + dynamicImportInCjs: boolean; entryFileNames: string | ((chunkInfo: PreRenderedChunk) => string); esModule: boolean | 'if-default-prop'; exports: 'default' | 'named' | 'none' | 'auto'; diff --git a/src/utils/options/mergeOptions.ts b/src/utils/options/mergeOptions.ts index b9903f7a13d..ba4e129b722 100644 --- a/src/utils/options/mergeOptions.ts +++ b/src/utils/options/mergeOptions.ts @@ -231,6 +231,7 @@ function mergeOutputOptions( compact: getOption('compact'), dir: getOption('dir'), dynamicImportFunction: getOption('dynamicImportFunction'), + dynamicImportInCjs: getOption('dynamicImportInCjs'), entryFileNames: getOption('entryFileNames'), esModule: getOption('esModule'), exports: getOption('exports'), diff --git a/src/utils/options/normalizeOutputOptions.ts b/src/utils/options/normalizeOutputOptions.ts index 42e901bdffd..b9ad0b5b3d2 100644 --- a/src/utils/options/normalizeOutputOptions.ts +++ b/src/utils/options/normalizeOutputOptions.ts @@ -43,6 +43,7 @@ export function normalizeOutputOptions( compact, dir: getDir(config, file), dynamicImportFunction: getDynamicImportFunction(config, inputOptions, format), + dynamicImportInCjs: config.dynamicImportInCjs ?? true, entryFileNames: getEntryFileNames(config, unsetOptions), esModule: config.esModule ?? 'if-default-prop', exports: getExports(config, unsetOptions), diff --git a/test/chunking-form/samples/deprecated/dynamic-import-comments/_expected/cjs/main.js b/test/chunking-form/samples/deprecated/dynamic-import-comments/_expected/cjs/main.js index 7d584ac8257..3ec906a7e1b 100644 --- a/test/chunking-form/samples/deprecated/dynamic-import-comments/_expected/cjs/main.js +++ b/test/chunking-form/samples/deprecated/dynamic-import-comments/_expected/cjs/main.js @@ -1,22 +1,5 @@ 'use strict'; -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require( +import /* () should not break */ ( /* webpackChunkName: "chunk-name" */ -'./foo.js'/*suffix*/)); }); +'./foo.js'/*suffix*/); diff --git a/test/chunking-form/samples/deprecated/dynamic-import-name/_expected/cjs/main.js b/test/chunking-form/samples/deprecated/dynamic-import-name/_expected/cjs/main.js index df6892e0d79..b23ce492015 100644 --- a/test/chunking-form/samples/deprecated/dynamic-import-name/_expected/cjs/main.js +++ b/test/chunking-form/samples/deprecated/dynamic-import-name/_expected/cjs/main.js @@ -1,20 +1,3 @@ 'use strict'; -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./foo.js')); }).then(result => console.log(result)); +import('./foo.js').then(result => console.log(result)); diff --git a/test/chunking-form/samples/dynamic-import-dynamic/_expected/cjs/main.js b/test/chunking-form/samples/dynamic-import-dynamic/_expected/cjs/main.js index 21521560d39..657ea9f5266 100644 --- a/test/chunking-form/samples/dynamic-import-dynamic/_expected/cjs/main.js +++ b/test/chunking-form/samples/dynamic-import-dynamic/_expected/cjs/main.js @@ -1,22 +1,5 @@ 'use strict'; -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - var dep = 'dep'; -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })(dep); +import(dep); diff --git a/test/chunking-form/samples/dynamic-import-external/_expected/cjs/main.js b/test/chunking-form/samples/dynamic-import-external/_expected/cjs/main.js index e2a92e81f32..324e5eef779 100644 --- a/test/chunking-form/samples/dynamic-import-external/_expected/cjs/main.js +++ b/test/chunking-form/samples/dynamic-import-external/_expected/cjs/main.js @@ -1,20 +1,3 @@ 'use strict'; -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./foo.js')); }); +import('./foo.js'); diff --git a/test/chunking-form/samples/emit-file/emit-chunk/_expected/cjs/main.js b/test/chunking-form/samples/emit-file/emit-chunk/_expected/cjs/main.js index 97a11cdece2..b442767c9b7 100644 --- a/test/chunking-form/samples/emit-file/emit-chunk/_expected/cjs/main.js +++ b/test/chunking-form/samples/emit-file/emit-chunk/_expected/cjs/main.js @@ -2,23 +2,6 @@ var dep = require('./generated-dep.js'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./ext\'ernal')); }); +import('./ext\'ernal'); console.log('main', dep.value); diff --git a/test/chunking-form/samples/nested-chunks/_expected/cjs/main1.js b/test/chunking-form/samples/nested-chunks/_expected/cjs/main1.js index f3593ce6497..6a45175f346 100644 --- a/test/chunking-form/samples/nested-chunks/_expected/cjs/main1.js +++ b/test/chunking-form/samples/nested-chunks/_expected/cjs/main1.js @@ -2,24 +2,7 @@ var dep = require('./generated-dep.js'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - console.log('main1', dep.value); Promise.resolve().then(function () { return require('./generated-dynamic.js'); }).then(result => console.log(result)); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./external.js')); }).then(result => console.log(result)); +import('./external.js').then(result => console.log(result)); diff --git a/test/chunking-form/samples/nested-chunks/_expected/cjs/nested/main2.js b/test/chunking-form/samples/nested-chunks/_expected/cjs/nested/main2.js index 9c920a66810..cb85e04600c 100644 --- a/test/chunking-form/samples/nested-chunks/_expected/cjs/nested/main2.js +++ b/test/chunking-form/samples/nested-chunks/_expected/cjs/nested/main2.js @@ -2,24 +2,7 @@ var dep = require('../generated-dep.js'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - console.log('main2', dep.value); Promise.resolve().then(function () { return require('../generated-dynamic.js'); }).then(result => console.log(result)); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('../external.js')); }).then(result => console.log(result)); +import('../external.js').then(result => console.log(result)); diff --git a/test/chunking-form/samples/resolve-dynamic-import/_expected/cjs/main.js b/test/chunking-form/samples/resolve-dynamic-import/_expected/cjs/main.js index 288899da9cd..75bb253218c 100644 --- a/test/chunking-form/samples/resolve-dynamic-import/_expected/cjs/main.js +++ b/test/chunking-form/samples/resolve-dynamic-import/_expected/cjs/main.js @@ -5,29 +5,12 @@ require('to-indirect-relative-external'); require('direct-absolute-external'); require('to-indirect-absolute-external'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - // nested Promise.resolve().then(function () { return existing; }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./direct-relative-external')); }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('to-indirect-relative-external')); }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('direct-absolute-external')); }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('to-indirect-absolute-external')); }); +import('./direct-relative-external'); +import('to-indirect-relative-external'); +import('direct-absolute-external'); +import('to-indirect-absolute-external'); const value = 'existing'; console.log('existing'); @@ -39,12 +22,12 @@ var existing = /*#__PURE__*/Object.freeze({ //main Promise.resolve().then(function () { return existing; }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./direct-relative-external')); }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('to-indirect-relative-external')); }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('direct-absolute-external')); }); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('to-indirect-absolute-external')); }); +import('./direct-relative-external'); +import('to-indirect-relative-external'); +import('direct-absolute-external'); +import('to-indirect-absolute-external'); -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })('dynamic-direct-external' + unknown); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('to-dynamic-indirect-external')); }); +import('dynamic-direct-external' + unknown); +import('to-dynamic-indirect-external'); Promise.resolve().then(function () { return existing; }); -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })('my' + 'replacement'); +import('my' + 'replacement'); diff --git a/test/form/samples/deconflict-format-specific-globals/_config.js b/test/form/samples/deconflict-format-specific-globals/_config.js index 4c42972e3f4..8b57b4a0a45 100644 --- a/test/form/samples/deconflict-format-specific-globals/_config.js +++ b/test/form/samples/deconflict-format-specific-globals/_config.js @@ -5,7 +5,8 @@ module.exports = { output: { globals: { external: 'external' }, name: 'bundle', - interop: 'auto' + interop: 'auto', + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/dynamic-import-this-arrow/_config.js b/test/form/samples/dynamic-import-this-arrow/_config.js index 53ad52318f0..9f6306ee6cf 100644 --- a/test/form/samples/dynamic-import-this-arrow/_config.js +++ b/test/form/samples/dynamic-import-this-arrow/_config.js @@ -6,7 +6,8 @@ module.exports = { external: ['input', 'output'], output: { generatedCode: { arrowFunctions: true }, - name: 'bundle' + name: 'bundle', + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/dynamic-import-this-function/_config.js b/test/form/samples/dynamic-import-this-function/_config.js index a43e98dd4e1..8877af159f8 100644 --- a/test/form/samples/dynamic-import-this-function/_config.js +++ b/test/form/samples/dynamic-import-this-function/_config.js @@ -6,7 +6,8 @@ module.exports = { external: ['input', 'output'], output: { generatedCode: { arrowFunctions: false }, - name: 'bundle' + name: 'bundle', + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/dynamic-import-unresolvable/_expected/cjs.js b/test/form/samples/dynamic-import-unresolvable/_expected/cjs.js index 6fd33278166..d566db97089 100644 --- a/test/form/samples/dynamic-import-unresolvable/_expected/cjs.js +++ b/test/form/samples/dynamic-import-unresolvable/_expected/cjs.js @@ -1,23 +1,6 @@ 'use strict'; -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })(`${globalThis.unknown}`); -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })(`My ${globalThis.unknown}`); -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })('./seven.js'); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('./seven.js')); }); +import(`${globalThis.unknown}`); +import(`My ${globalThis.unknown}`); +import('./seven.js'); +import('./seven.js'); diff --git a/test/form/samples/generated-code-compact/arrow-functions-false/_config.js b/test/form/samples/generated-code-compact/arrow-functions-false/_config.js index 95ab9567fd7..59b4aa1015e 100644 --- a/test/form/samples/generated-code-compact/arrow-functions-false/_config.js +++ b/test/form/samples/generated-code-compact/arrow-functions-false/_config.js @@ -26,6 +26,7 @@ module.exports = { }, name: 'bundle', noConflict: true, + dynamicImportInCjs: false, systemNullSetters: false } }, diff --git a/test/form/samples/generated-code-compact/arrow-functions-true/_config.js b/test/form/samples/generated-code-compact/arrow-functions-true/_config.js index b7c1613e3de..23d7c2f799c 100644 --- a/test/form/samples/generated-code-compact/arrow-functions-true/_config.js +++ b/test/form/samples/generated-code-compact/arrow-functions-true/_config.js @@ -26,6 +26,7 @@ module.exports = { }, name: 'bundle', noConflict: true, + dynamicImportInCjs: false, systemNullSetters: false } }, diff --git a/test/form/samples/generated-code/arrow-functions-false/_config.js b/test/form/samples/generated-code/arrow-functions-false/_config.js index fb26554fffa..3cf005ea0c0 100644 --- a/test/form/samples/generated-code/arrow-functions-false/_config.js +++ b/test/form/samples/generated-code/arrow-functions-false/_config.js @@ -25,6 +25,7 @@ module.exports = { }, name: 'bundle', noConflict: true, + dynamicImportInCjs: false, systemNullSetters: false } }, diff --git a/test/form/samples/generated-code/arrow-functions-true/_config.js b/test/form/samples/generated-code/arrow-functions-true/_config.js index 102a4d420b7..8ac55b955bd 100644 --- a/test/form/samples/generated-code/arrow-functions-true/_config.js +++ b/test/form/samples/generated-code/arrow-functions-true/_config.js @@ -25,6 +25,7 @@ module.exports = { }, name: 'bundle', noConflict: true, + dynamicImportInCjs: false, systemNullSetters: false } }, diff --git a/test/form/samples/import-expression/_expected/cjs.js b/test/form/samples/import-expression/_expected/cjs.js index e982928f093..bd463137fe4 100644 --- a/test/form/samples/import-expression/_expected/cjs.js +++ b/test/form/samples/import-expression/_expected/cjs.js @@ -2,22 +2,5 @@ var external = require('external'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - -(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })(external.join('a', 'b')); +import(external.join('a', 'b')); console.log(external.join); diff --git a/test/form/samples/interop-per-dependency-no-freeze/_config.js b/test/form/samples/interop-per-dependency-no-freeze/_config.js index 87fef95a7de..b101cfe23b9 100644 --- a/test/form/samples/interop-per-dependency-no-freeze/_config.js +++ b/test/form/samples/interop-per-dependency-no-freeze/_config.js @@ -9,7 +9,8 @@ module.exports = { }, globals(id) { return id.replace('-', ''); - } + }, + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/interop-per-dependency-no-live-binding/_config.js b/test/form/samples/interop-per-dependency-no-live-binding/_config.js index 4c8a5549d3a..7f7e7c22824 100644 --- a/test/form/samples/interop-per-dependency-no-live-binding/_config.js +++ b/test/form/samples/interop-per-dependency-no-live-binding/_config.js @@ -13,7 +13,8 @@ module.exports = { checkedIds.add(id); return id.split('-')[1]; }, - format: 'cjs' + format: 'cjs', + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/interop-per-dependency/_config.js b/test/form/samples/interop-per-dependency/_config.js index 1212b8c0f57..55148ba9758 100644 --- a/test/form/samples/interop-per-dependency/_config.js +++ b/test/form/samples/interop-per-dependency/_config.js @@ -11,7 +11,8 @@ module.exports = { }, globals(id) { return id.replace('-', ''); - } + }, + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/no-external-live-bindings-compact/_config.js b/test/form/samples/no-external-live-bindings-compact/_config.js index 875b61c29c6..33a7445e319 100644 --- a/test/form/samples/no-external-live-bindings-compact/_config.js +++ b/test/form/samples/no-external-live-bindings-compact/_config.js @@ -9,7 +9,8 @@ module.exports = { }, compact: true, externalLiveBindings: false, - name: 'bundle' + name: 'bundle', + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/no-external-live-bindings/_config.js b/test/form/samples/no-external-live-bindings/_config.js index 55b97a19b34..30303d219e2 100644 --- a/test/form/samples/no-external-live-bindings/_config.js +++ b/test/form/samples/no-external-live-bindings/_config.js @@ -8,7 +8,8 @@ module.exports = { external2: 'external2' }, externalLiveBindings: false, - name: 'bundle' + name: 'bundle', + dynamicImportInCjs: false } } }; diff --git a/test/form/samples/paths-function/_expected/cjs.js b/test/form/samples/paths-function/_expected/cjs.js index 61b8caa4a1b..7ea9f42d78d 100644 --- a/test/form/samples/paths-function/_expected/cjs.js +++ b/test/form/samples/paths-function/_expected/cjs.js @@ -2,23 +2,6 @@ var foo = require('https://unpkg.com/foo'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - assert.equal(foo, 42); -Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('https://unpkg.com/foo')); }).then(({ default: foo }) => assert.equal(foo, 42)); +import('https://unpkg.com/foo').then(({ default: foo }) => assert.equal(foo, 42)); diff --git a/test/form/samples/resolve-external-dynamic-imports/_expected/cjs.js b/test/form/samples/resolve-external-dynamic-imports/_expected/cjs.js index c18ed55b1b4..f08412b5ddd 100644 --- a/test/form/samples/resolve-external-dynamic-imports/_expected/cjs.js +++ b/test/form/samples/resolve-external-dynamic-imports/_expected/cjs.js @@ -2,26 +2,9 @@ var myExternal = require('external'); -function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); -} - const test = () => myExternal; -const someDynamicImport = () => Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require('external')); }); +const someDynamicImport = () => import('external'); exports.someDynamicImport = someDynamicImport; exports.test = test; diff --git a/test/function/samples/catch-dynamic-import-failure/_config.js b/test/function/samples/catch-dynamic-import-failure/_config.js index 98030f30575..61f6fac84d5 100644 --- a/test/function/samples/catch-dynamic-import-failure/_config.js +++ b/test/function/samples/catch-dynamic-import-failure/_config.js @@ -10,7 +10,7 @@ module.exports = { return exports.then(result => { assert.strictEqual(result[0].message, 'exists-named'); assert.strictEqual(result[1].message, 'exists-default'); - const expectedError = "Cannot find module 'does-not-exist'"; + const expectedError = "Cannot find package 'does-not-exist'"; assert.strictEqual(result[2].message.slice(0, expectedError.length), expectedError); }); } diff --git a/test/function/samples/deprecated/dynamic-import-name-warn/_config.js b/test/function/samples/deprecated/dynamic-import-name-warn/_config.js index 29ec0a81284..7015643797e 100644 --- a/test/function/samples/deprecated/dynamic-import-name-warn/_config.js +++ b/test/function/samples/deprecated/dynamic-import-name-warn/_config.js @@ -18,7 +18,8 @@ module.exports = { }, output: { dynamicImportFunction: 'myImporter', - format: 'cjs' + format: 'cjs', + dynamicImportInCjs: false } }, exports(exports) { diff --git a/test/function/samples/dynamic-import-expression/_config.js b/test/function/samples/dynamic-import-expression/_config.js index b06c2ee080f..35f7f4a7674 100644 --- a/test/function/samples/dynamic-import-expression/_config.js +++ b/test/function/samples/dynamic-import-expression/_config.js @@ -21,7 +21,8 @@ module.exports = { } } } - ] + ], + output: { dynamicImportInCjs: false } }, exports(exports) { const expectedError = "Cannot find module 'x/y'"; diff --git a/test/function/samples/dynamic-import-rewriting/_config.js b/test/function/samples/dynamic-import-rewriting/_config.js index 3680137bc34..40035e54aa3 100644 --- a/test/function/samples/dynamic-import-rewriting/_config.js +++ b/test/function/samples/dynamic-import-rewriting/_config.js @@ -13,7 +13,7 @@ module.exports = { ] }, exports(exports) { - const expectedError = "Cannot find module 'asdf'"; + const expectedError = "Cannot find package 'asdf'"; return exports.promise.catch(err => assert.strictEqual(err.message.slice(0, expectedError.length), expectedError) ); diff --git a/test/function/samples/dynamic-import-this-arrow/_config.js b/test/function/samples/dynamic-import-this-arrow/_config.js index 0135981db4e..16b307536b9 100644 --- a/test/function/samples/dynamic-import-this-arrow/_config.js +++ b/test/function/samples/dynamic-import-this-arrow/_config.js @@ -20,7 +20,8 @@ module.exports = { options: { external: ['input', 'output'], output: { - generatedCode: { arrowFunctions: true } + generatedCode: { arrowFunctions: true }, + dynamicImportInCjs: false } } }; diff --git a/test/function/samples/dynamic-import-this-function/_config.js b/test/function/samples/dynamic-import-this-function/_config.js index 200aed65846..32f9c34694a 100644 --- a/test/function/samples/dynamic-import-this-function/_config.js +++ b/test/function/samples/dynamic-import-this-function/_config.js @@ -20,7 +20,8 @@ module.exports = { options: { external: ['input', 'output'], output: { - generatedCode: { arrowFunctions: false } + generatedCode: { arrowFunctions: false }, + dynamicImportInCjs: false } } }; diff --git a/test/function/samples/external-dynamic-import-live-binding-compact/_config.js b/test/function/samples/external-dynamic-import-live-binding-compact/_config.js index 0e470b8e402..a7bf1872fbb 100644 --- a/test/function/samples/external-dynamic-import-live-binding-compact/_config.js +++ b/test/function/samples/external-dynamic-import-live-binding-compact/_config.js @@ -8,7 +8,8 @@ module.exports = { }, output: { compact: true, - interop: 'auto' + interop: 'auto', + dynamicImportInCjs: false } }, context: { diff --git a/test/function/samples/external-dynamic-import-live-binding/_config.js b/test/function/samples/external-dynamic-import-live-binding/_config.js index c1af09978c0..1b7b2da40c3 100644 --- a/test/function/samples/external-dynamic-import-live-binding/_config.js +++ b/test/function/samples/external-dynamic-import-live-binding/_config.js @@ -6,7 +6,7 @@ module.exports = { external() { return true; }, - output: { interop: 'auto' } + output: { interop: 'auto', dynamicImportInCjs: false } }, context: { require(id) { diff --git a/test/function/samples/keep-cjs-dynamic-import/_config.js b/test/function/samples/keep-cjs-dynamic-import/_config.js new file mode 100644 index 00000000000..bf5db259d84 --- /dev/null +++ b/test/function/samples/keep-cjs-dynamic-import/_config.js @@ -0,0 +1,14 @@ +const assert = require('assert'); + +module.exports = { + description: 'keeps dynamic imports in CJS output by default', + options: { external: ['external-esm'] }, + async exports({ result }) { + const expected = { + __proto__: null, + default: { external: true } + }; + Object.defineProperty(expected, Symbol.toStringTag, { value: 'Module' }); + assert.deepStrictEqual(await result, expected); + } +}; diff --git a/test/function/samples/keep-cjs-dynamic-import/main.js b/test/function/samples/keep-cjs-dynamic-import/main.js new file mode 100644 index 00000000000..8ea197bac62 --- /dev/null +++ b/test/function/samples/keep-cjs-dynamic-import/main.js @@ -0,0 +1 @@ +export const result = import('external-esm'); diff --git a/test/function/samples/no-external-live-bindings-compact/_config.js b/test/function/samples/no-external-live-bindings-compact/_config.js index 7ad2866177d..29168683feb 100644 --- a/test/function/samples/no-external-live-bindings-compact/_config.js +++ b/test/function/samples/no-external-live-bindings-compact/_config.js @@ -7,7 +7,8 @@ module.exports = { output: { compact: true, externalLiveBindings: false, - name: 'bundle' + name: 'bundle', + dynamicImportInCjs: false } }, context: { diff --git a/test/function/samples/no-external-live-bindings/_config.js b/test/function/samples/no-external-live-bindings/_config.js index 1a359d3d893..a89ec8a4de8 100644 --- a/test/function/samples/no-external-live-bindings/_config.js +++ b/test/function/samples/no-external-live-bindings/_config.js @@ -6,7 +6,8 @@ module.exports = { external: () => true, output: { externalLiveBindings: false, - name: 'bundle' + name: 'bundle', + dynamicImportInCjs: false } }, context: { diff --git a/test/function/samples/output-options-hook/_config.js b/test/function/samples/output-options-hook/_config.js index b15bd6ce4d4..ab64f6f952a 100644 --- a/test/function/samples/output-options-hook/_config.js +++ b/test/function/samples/output-options-hook/_config.js @@ -24,6 +24,7 @@ module.exports = { assetFileNames: 'assets/[name]-[hash][extname]', chunkFileNames: '[name]-[hash].js', compact: false, + dynamicImportInCjs: true, entryFileNames: '[name].js', esModule: 'if-default-prop', exports: 'auto', diff --git a/test/misc/optionList.js b/test/misc/optionList.js index db722ff0ef5..88dfdea6e97 100644 --- a/test/misc/optionList.js +++ b/test/misc/optionList.js @@ -1,6 +1,6 @@ exports.input = 'acorn, acornInjectPlugins, cache, context, experimentalCacheExpiry, external, inlineDynamicImports, input, makeAbsoluteExternalsRelative, manualChunks, maxParallelFileOps, maxParallelFileReads, moduleContext, onwarn, perf, plugins, preserveEntrySignatures, preserveModules, preserveSymlinks, shimMissingExports, strictDeprecations, treeshake, watch'; exports.flags = - 'acorn, acornInjectPlugins, amd, assetFileNames, banner, bundleConfigAsCjs, c, cache, chunkFileNames, compact, config, configPlugin, context, d, dir, dynamicImportFunction, e, entryFileNames, environment, esModule, experimentalCacheExpiry, exports, extend, external, externalLiveBindings, f, failAfterWarnings, file, footer, format, freeze, g, generatedCode, globals, h, hoistTransitiveImports, i, indent, inlineDynamicImports, input, interop, intro, m, makeAbsoluteExternalsRelative, manualChunks, maxParallelFileOps, maxParallelFileReads, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRoot, preserveSymlinks, sanitizeFileName, shimMissingExports, silent, sourcemap, sourcemapBaseUrl, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, validate, w, waitForBundleInput, watch'; + 'acorn, acornInjectPlugins, amd, assetFileNames, banner, bundleConfigAsCjs, c, cache, chunkFileNames, compact, config, configPlugin, context, d, dir, dynamicImportFunction, dynamicImportInCjs, e, entryFileNames, environment, esModule, experimentalCacheExpiry, exports, extend, external, externalLiveBindings, f, failAfterWarnings, file, footer, format, freeze, g, generatedCode, globals, h, hoistTransitiveImports, i, indent, inlineDynamicImports, input, interop, intro, m, makeAbsoluteExternalsRelative, manualChunks, maxParallelFileOps, maxParallelFileReads, minifyInternalExports, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, p, paths, perf, plugin, plugins, preferConst, preserveEntrySignatures, preserveModules, preserveModulesRoot, preserveSymlinks, sanitizeFileName, shimMissingExports, silent, sourcemap, sourcemapBaseUrl, sourcemapExcludeSources, sourcemapFile, stdin, strict, strictDeprecations, systemNullSetters, treeshake, v, validate, w, waitForBundleInput, watch'; exports.output = - 'amd, assetFileNames, banner, chunkFileNames, compact, dir, dynamicImportFunction, entryFileNames, esModule, exports, extend, externalLiveBindings, file, footer, format, freeze, generatedCode, globals, hoistTransitiveImports, indent, inlineDynamicImports, interop, intro, manualChunks, minifyInternalExports, name, namespaceToStringTag, noConflict, outro, paths, plugins, preferConst, preserveModules, preserveModulesRoot, sanitizeFileName, sourcemap, sourcemapBaseUrl, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters, validate'; + 'amd, assetFileNames, banner, chunkFileNames, compact, dir, dynamicImportFunction, dynamicImportInCjs, entryFileNames, esModule, exports, extend, externalLiveBindings, file, footer, format, freeze, generatedCode, globals, hoistTransitiveImports, indent, inlineDynamicImports, interop, intro, manualChunks, minifyInternalExports, name, namespaceToStringTag, noConflict, outro, paths, plugins, preferConst, preserveModules, preserveModulesRoot, sanitizeFileName, sourcemap, sourcemapBaseUrl, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, systemNullSetters, validate'; diff --git a/test/node_modules/external-esm/index.js b/test/node_modules/external-esm/index.js new file mode 100644 index 00000000000..9c87fc2a3c2 --- /dev/null +++ b/test/node_modules/external-esm/index.js @@ -0,0 +1,3 @@ +export default { + external: true +}; diff --git a/test/node_modules/external-esm/package.json b/test/node_modules/external-esm/package.json new file mode 100644 index 00000000000..0203c72855d --- /dev/null +++ b/test/node_modules/external-esm/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "main": "index.js" +}