From 9a17e68909a93022e6075dd8913786855bc992db Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Tue, 26 Feb 2019 11:49:38 +0000 Subject: [PATCH 1/6] feat: add dynamicImportFunction option This option allows users of `es` format to rename the `import()` function, which right now has mixed support in browsers. By allowing this function to be renamed we can have polyfills (which cannot use the reserved `import` keyword but could go by a different name like `importModule`). --- src/Chunk.ts | 3 ++- src/ast/nodes/Import.ts | 19 ++++++++++++------- src/rollup/types.d.ts | 1 + src/utils/mergeOptions.ts | 3 ++- src/utils/renderHelpers.ts | 1 + .../samples/dynamic-import-name/_config.js | 14 ++++++++++++++ .../dynamic-import-name/_expected/amd/main.js | 5 +++++ .../dynamic-import-name/_expected/cjs/main.js | 3 +++ .../dynamic-import-name/_expected/es/main.js | 1 + .../_expected/system/main.js | 10 ++++++++++ .../samples/dynamic-import-name/foo.js | 1 + .../samples/dynamic-import-name/main.js | 1 + test/misc/optionList.js | 2 +- 13 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 test/chunking-form/samples/dynamic-import-name/_config.js create mode 100644 test/chunking-form/samples/dynamic-import-name/_expected/amd/main.js create mode 100644 test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js create mode 100644 test/chunking-form/samples/dynamic-import-name/_expected/es/main.js create mode 100644 test/chunking-form/samples/dynamic-import-name/_expected/system/main.js create mode 100644 test/chunking-form/samples/dynamic-import-name/foo.js create mode 100644 test/chunking-form/samples/dynamic-import-name/main.js diff --git a/src/Chunk.ts b/src/Chunk.ts index b9ee6619f57..7085a2942d4 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -659,7 +659,8 @@ export default class Chunk { freeze: options.freeze !== false, indent: this.indentString, namespaceToStringTag: options.namespaceToStringTag === true, - varOrConst: options.preferConst ? 'const' : 'var' + varOrConst: options.preferConst ? 'const' : 'var', + dynamicImportFunction: options.dynamicImportFunction || 'import' }; // Make sure the direct dependencies of a chunk are present to maintain execution order diff --git a/src/ast/nodes/Import.ts b/src/ast/nodes/Import.ts index 37164af3578..c2a7ffea870 100644 --- a/src/ast/nodes/Import.ts +++ b/src/ast/nodes/Import.ts @@ -11,10 +11,10 @@ interface DynamicImportMechanism { interopRight?: string; } -const getDynamicImportMechanism = (format: string, compact: boolean): DynamicImportMechanism => { - switch (format) { +const getDynamicImportMechanism = (options: RenderOptions): DynamicImportMechanism => { + switch (options.format) { case 'cjs': { - const _ = compact ? '' : ' '; + const _ = options.compact ? '' : ' '; return { left: 'Promise.resolve(require(', right: '))', @@ -23,9 +23,9 @@ const getDynamicImportMechanism = (format: string, compact: boolean): DynamicImp }; } case 'amd': { - const _ = compact ? '' : ' '; - const resolve = compact ? 'c' : 'resolve'; - const reject = compact ? 'e' : 'reject'; + const _ = options.compact ? '' : ' '; + const resolve = options.compact ? 'c' : 'resolve'; + const reject = options.compact ? 'e' : 'reject'; return { left: `new Promise(function${_}(${resolve},${_}${reject})${_}{${_}require([`, right: `],${_}${resolve},${_}${reject})${_}})`, @@ -38,6 +38,11 @@ const getDynamicImportMechanism = (format: string, compact: boolean): DynamicImp left: 'module.import(', right: ')' }; + case 'es': + return { + left: `${options.dynamicImportFunction}(`, + right: ')' + }; } }; @@ -78,7 +83,7 @@ export default class Import extends NodeBase { return; } - const importMechanism = getDynamicImportMechanism(options.format, options.compact); + const importMechanism = getDynamicImportMechanism(options); if (importMechanism) { const leftMechanism = (this.resolutionInterop && importMechanism.interopLeft) || importMechanism.left; diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 8489f31d6f6..d0a80077ea1 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -321,6 +321,7 @@ export interface OutputOptions { sourcemapFile?: string; sourcemapPathTransform?: (sourcePath: string) => string; strict?: boolean; + dynamicImportFunction?: string; } export type WarningHandler = (warning: string | RollupWarning) => void; diff --git a/src/utils/mergeOptions.ts b/src/utils/mergeOptions.ts index 30abada9b8f..6dccbdb0cdf 100644 --- a/src/utils/mergeOptions.ts +++ b/src/utils/mergeOptions.ts @@ -260,6 +260,7 @@ function getOutputOptions( sourcemapExcludeSources: getOption('sourcemapExcludeSources'), sourcemapFile: getOption('sourcemapFile'), sourcemapPathTransform: getOption('sourcemapPathTransform'), - strict: getOption('strict', true) + strict: getOption('strict', true), + dynamicImportFunction: getOption('dynamicImportFunction', 'import') }; } diff --git a/src/utils/renderHelpers.ts b/src/utils/renderHelpers.ts index dc11da21357..c8c8fdb2b05 100644 --- a/src/utils/renderHelpers.ts +++ b/src/utils/renderHelpers.ts @@ -9,6 +9,7 @@ export interface RenderOptions { indent: string; namespaceToStringTag: boolean; varOrConst: 'var' | 'const'; + dynamicImportFunction: string; } export interface NodeRenderOptions { diff --git a/test/chunking-form/samples/dynamic-import-name/_config.js b/test/chunking-form/samples/dynamic-import-name/_config.js new file mode 100644 index 00000000000..ca14189518e --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/_config.js @@ -0,0 +1,14 @@ +module.exports = { + description: 'marks dynamic imports as external when resolveDynamicImport returns false', + options: { + input: 'main.js', + plugins: { + resolveDynamicImport(specifier) { + return false; + } + }, + output: { + dynamicImportFunction: 'foobar' + } + } +}; diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/amd/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/amd/main.js new file mode 100644 index 00000000000..b81e4d74b05 --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/_expected/amd/main.js @@ -0,0 +1,5 @@ +define(['require'], function (require) { 'use strict'; + + new Promise(function (resolve, reject) { require(['./foo.js'], resolve, reject) }); + +}); diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js new file mode 100644 index 00000000000..b1acd589703 --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js @@ -0,0 +1,3 @@ +'use strict'; + +Promise.resolve(require('./foo.js')); diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js new file mode 100644 index 00000000000..51e216a3931 --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js @@ -0,0 +1 @@ +foobar('./foo.js'); diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js new file mode 100644 index 00000000000..0824bad8d3f --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js @@ -0,0 +1,10 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + module.import('./foo.js'); + + } + }; +}); diff --git a/test/chunking-form/samples/dynamic-import-name/foo.js b/test/chunking-form/samples/dynamic-import-name/foo.js new file mode 100644 index 00000000000..81afa3157c1 --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/foo.js @@ -0,0 +1 @@ +console.log('foo'); diff --git a/test/chunking-form/samples/dynamic-import-name/main.js b/test/chunking-form/samples/dynamic-import-name/main.js new file mode 100644 index 00000000000..bbe4a5cc219 --- /dev/null +++ b/test/chunking-form/samples/dynamic-import-name/main.js @@ -0,0 +1 @@ +import('./foo.js'); diff --git a/test/misc/optionList.js b/test/misc/optionList.js index 50080676ae6..93204262061 100644 --- a/test/misc/optionList.js +++ b/test/misc/optionList.js @@ -1,3 +1,3 @@ exports.input = 'acorn, acornInjectPlugins, cache, chunkGroupingSize, context, experimentalCacheExpiry, experimentalOptimizeChunks, experimentalTopLevelAwait, external, inlineDynamicImports, input, manualChunks, moduleContext, onwarn, perf, plugins, preserveModules, preserveSymlinks, shimMissingExports, treeshake, watch'; exports.flags = 'acorn, acornInjectPlugins, amd, assetFileNames, banner, c, cache, chunkFileNames, chunkGroupingSize, compact, config, context, d, dir, e, entryFileNames, environment, esModule, experimentalCacheExpiry, experimentalOptimizeChunks, experimentalTopLevelAwait, exports, extend, external, f, file, footer, format, freeze, g, globals, h, i, indent, inlineDynamicImports, input, interop, intro, m, manualChunks, moduleContext, n, name, namespaceToStringTag, noConflict, o, onwarn, outro, paths, perf, plugins, preferConst, preserveModules, preserveSymlinks, shimMissingExports, silent, sourcemap, sourcemapExcludeSources, sourcemapFile, strict, treeshake, v, w, watch'; -exports.output = 'amd, assetFileNames, banner, dir, chunkFileNames, compact, entryFileNames, esModule, exports, extend, file, footer, format, freeze, globals, indent, interop, intro, name, namespaceToStringTag, noConflict, outro, paths, preferConst, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict'; +exports.output = 'amd, assetFileNames, banner, dir, chunkFileNames, compact, entryFileNames, esModule, exports, extend, file, footer, format, freeze, globals, indent, interop, intro, name, namespaceToStringTag, noConflict, outro, paths, preferConst, sourcemap, sourcemapExcludeSources, sourcemapFile, sourcemapPathTransform, strict, dynamicImportFunction'; From e7d455ac135592eec14b73384b43a69ad1917d69 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Fri, 1 Mar 2019 09:33:45 +0000 Subject: [PATCH 2/6] docs: add dynamicImportFunction docs --- bin/src/help.md | 1 + docs/999-big-list-of-options.md | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/bin/src/help.md b/bin/src/help.md index 6d95d741620..a5f69274379 100644 --- a/bin/src/help.md +++ b/bin/src/help.md @@ -51,6 +51,7 @@ Basic options: --no-treeshake.annotations Ignore pure call annotations --no-treeshake.propertyReadSideEffects Ignore property access side-effects --treeshake.pureExternalModules Assume side-effect free externals +--dynamicImportFunction Rename the dynamic `import()` function Examples: diff --git a/docs/999-big-list-of-options.md b/docs/999-big-list-of-options.md index b6fa3a4ead9..38736b1fcf7 100755 --- a/docs/999-big-list-of-options.md +++ b/docs/999-big-list-of-options.md @@ -667,6 +667,13 @@ Default: `true` Whether to include the 'use strict' pragma at the top of generated non-ESM bundles. Strictly-speaking, ES modules are *always* in strict mode, so you shouldn't disable this without good reason. +#### output.dynamicImportFunction +Type: `string`
+CLI: `--dynamicImportFunction `
+Default: `import` + +This will rename the dynamic import function to the chosen name when outputting ESM bundles. This is useful for generating code that uses a dynamic import polyfill such as [this one](https://github.com/uupaa/dynamic-import-polyfill). + #### preserveSymlinks Type: `boolean`
CLI: `--preserveSymlinks`
From b8db9398ce0e0f650712d75a008d781884ff69ed Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Fri, 1 Mar 2019 09:43:40 +0000 Subject: [PATCH 3/6] feat: warn when using dynamicImportFunction in non-esm format --- src/rollup/index.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 42f9d060434..913389139cf 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -177,6 +177,15 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise 1 ); + if (outputOptions.format !== 'esm' && outputOptions.dynamicImportFunction !== 'import') { + inputOptions.onwarn({ + code: 'INVALID_OPTION', + message: `dynamicImportFunction is not used when outputting ${ + outputOptions.format + }. This setting will be ignored.` + }); + } + timeStart('GENERATE', 1); const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; From 32d163f70c2aee380fbbcd9722a2de23ab39bc1e Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 1 Mar 2019 10:45:42 +0100 Subject: [PATCH 4/6] Add functional tests and align warning messages --- src/Chunk.ts | 8 ++++- src/finalisers/iife.ts | 2 +- src/finalisers/umd.ts | 2 +- src/rollup/index.ts | 33 ++++++++++--------- .../samples/dynamic-import-name/_config.js | 5 +-- .../dynamic-import-name/_expected/amd/main.js | 2 +- .../dynamic-import-name/_expected/cjs/main.js | 2 +- .../dynamic-import-name/_expected/es/main.js | 2 +- .../_expected/system/main.js | 2 +- .../samples/dynamic-import-name/foo.js | 1 - .../samples/dynamic-import-name/main.js | 2 +- .../dynamic-import-name-warn/_config.js | 32 ++++++++++++++++++ .../_expected/amd/main.js | 5 +++ .../_expected/cjs/main.js | 3 ++ .../_expected/es/main.js | 1 + .../_expected/system/main.js | 10 ++++++ .../samples/dynamic-import-name-warn/main.js | 1 + .../samples/dynamic-import-name/_config.js | 27 +++++++++++++++ .../dynamic-import-name/_expected/amd/main.js | 5 +++ .../dynamic-import-name/_expected/cjs/main.js | 3 ++ .../dynamic-import-name/_expected/es/main.js | 1 + .../_expected/system/main.js | 10 ++++++ .../samples/dynamic-import-name/main.js | 1 + .../inline-imports-with-manual/_config.js | 2 +- .../inline-imports-with-multiple/_config.js | 2 +- .../inline-imports-with-optimize/_config.js | 2 +- .../preserve-modules-with-inline/_config.js | 2 +- .../preserve-modules-with-manual/_config.js | 2 +- .../preserve-modules-with-optimize/_config.js | 2 +- test/hooks/index.js | 2 +- test/misc/sanity-checks.js | 10 +++--- test/misc/write-bundle.js | 12 +++---- 32 files changed, 151 insertions(+), 45 deletions(-) delete mode 100644 test/chunking-form/samples/dynamic-import-name/foo.js create mode 100644 test/function/samples/dynamic-import-name-warn/_config.js create mode 100644 test/function/samples/dynamic-import-name-warn/_expected/amd/main.js create mode 100644 test/function/samples/dynamic-import-name-warn/_expected/cjs/main.js create mode 100644 test/function/samples/dynamic-import-name-warn/_expected/es/main.js create mode 100644 test/function/samples/dynamic-import-name-warn/_expected/system/main.js create mode 100644 test/function/samples/dynamic-import-name-warn/main.js create mode 100644 test/function/samples/dynamic-import-name/_config.js create mode 100644 test/function/samples/dynamic-import-name/_expected/amd/main.js create mode 100644 test/function/samples/dynamic-import-name/_expected/cjs/main.js create mode 100644 test/function/samples/dynamic-import-name/_expected/es/main.js create mode 100644 test/function/samples/dynamic-import-name/_expected/system/main.js create mode 100644 test/function/samples/dynamic-import-name/main.js diff --git a/src/Chunk.ts b/src/Chunk.ts index 214a1b9ae36..b15af569872 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -546,7 +546,13 @@ export default class Chunk { code: 'INVALID_OPTION', message: `Invalid format: ${options.format} - valid options are ${Object.keys( finalisers - ).join(', ')}` + ).join(', ')}.` + }); + } + if (options.dynamicImportFunction && options.format !== 'es') { + this.graph.warn({ + code: 'INVALID_OPTION', + message: '"output.dynamicImportFunction" is ignored for formats other than "esm".' }); } diff --git a/src/finalisers/iife.ts b/src/finalisers/iife.ts index 95a02278658..1a66a775376 100644 --- a/src/finalisers/iife.ts +++ b/src/finalisers/iife.ts @@ -50,7 +50,7 @@ export default function iife( if (hasExports && !name) { error({ code: 'INVALID_OPTION', - message: `You must supply output.name for IIFE bundles` + message: `You must supply "output.name" for IIFE bundles.` }); } diff --git a/src/finalisers/umd.ts b/src/finalisers/umd.ts index f4663bd4475..86c91032f29 100644 --- a/src/finalisers/umd.ts +++ b/src/finalisers/umd.ts @@ -45,7 +45,7 @@ export default function umd( if (hasExports && !options.name) { error({ code: 'INVALID_OPTION', - message: 'You must supply output.name for UMD bundles' + message: 'You must supply "output.name" for UMD bundles.' }); } diff --git a/src/rollup/index.ts b/src/rollup/index.ts index 42f9d060434..6ad366c878e 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -28,14 +28,14 @@ import { function checkOutputOptions(options: OutputOptions) { if (options.format === 'es6') { error({ - message: 'The `es6` output format is deprecated – use `esm` instead', + message: 'The "es6" output format is deprecated – use "esm" instead', url: `https://rollupjs.org/guide/en#output-format` }); } if (!options.format) { error({ - message: `You must specify output.format, which can be one of 'amd', 'cjs', 'system', 'esm', 'iife' or 'umd'`, + message: `You must specify "output.format", which can be one of "amd", "cjs", "system", "esm", "iife" or "umd".`, url: `https://rollupjs.org/guide/en#output-format` }); } @@ -88,18 +88,18 @@ function getInputOptions(rawInputOptions: GenericConfigObject): any { if (inputOptions.preserveModules) error({ code: 'INVALID_OPTION', - message: `preserveModules does not support the inlineDynamicImports option.` + message: `"preserveModules" does not support the "inlineDynamicImports" option.` }); if (inputOptions.manualChunks) error({ code: 'INVALID_OPTION', - message: '"manualChunks" option is not supported for inlineDynamicImports.' + message: '"manualChunks" option is not supported for "inlineDynamicImports".' }); if (inputOptions.experimentalOptimizeChunks) error({ code: 'INVALID_OPTION', - message: '"experimentalOptimizeChunks" option is not supported for inlineDynamicImports.' + message: '"experimentalOptimizeChunks" option is not supported for "inlineDynamicImports".' }); if ( (inputOptions.input instanceof Array && inputOptions.input.length > 1) || @@ -107,18 +107,18 @@ function getInputOptions(rawInputOptions: GenericConfigObject): any { ) error({ code: 'INVALID_OPTION', - message: 'Multiple inputs are not supported for inlineDynamicImports.' + message: 'Multiple inputs are not supported for "inlineDynamicImports".' }); } else if (inputOptions.preserveModules) { if (inputOptions.manualChunks) error({ code: 'INVALID_OPTION', - message: 'preserveModules does not support the manualChunks option.' + message: '"preserveModules" does not support the "manualChunks" option.' }); if (inputOptions.experimentalOptimizeChunks) error({ code: 'INVALID_OPTION', - message: 'preserveModules does not support the experimentalOptimizeChunks option.' + message: '"preserveModules" does not support the "experimentalOptimizeChunks" option.' }); } @@ -290,7 +290,7 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise { @@ -305,17 +305,17 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise console.log(result)); }); diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js index b1acd589703..64a6539d7e3 100644 --- a/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js +++ b/test/chunking-form/samples/dynamic-import-name/_expected/cjs/main.js @@ -1,3 +1,3 @@ 'use strict'; -Promise.resolve(require('./foo.js')); +Promise.resolve(require('./foo.js')).then(result => console.log(result)); diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js index 51e216a3931..31d9a65e153 100644 --- a/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js +++ b/test/chunking-form/samples/dynamic-import-name/_expected/es/main.js @@ -1 +1 @@ -foobar('./foo.js'); +foobar('./foo.js').then(result => console.log(result)); diff --git a/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js b/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js index 0824bad8d3f..ce0e7ba782a 100644 --- a/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js +++ b/test/chunking-form/samples/dynamic-import-name/_expected/system/main.js @@ -3,7 +3,7 @@ System.register([], function (exports, module) { return { execute: function () { - module.import('./foo.js'); + module.import('./foo.js').then(result => console.log(result)); } }; diff --git a/test/chunking-form/samples/dynamic-import-name/foo.js b/test/chunking-form/samples/dynamic-import-name/foo.js deleted file mode 100644 index 81afa3157c1..00000000000 --- a/test/chunking-form/samples/dynamic-import-name/foo.js +++ /dev/null @@ -1 +0,0 @@ -console.log('foo'); diff --git a/test/chunking-form/samples/dynamic-import-name/main.js b/test/chunking-form/samples/dynamic-import-name/main.js index bbe4a5cc219..56ab47293cd 100644 --- a/test/chunking-form/samples/dynamic-import-name/main.js +++ b/test/chunking-form/samples/dynamic-import-name/main.js @@ -1 +1 @@ -import('./foo.js'); +import('./foo.js').then(result => console.log(result)); diff --git a/test/function/samples/dynamic-import-name-warn/_config.js b/test/function/samples/dynamic-import-name-warn/_config.js new file mode 100644 index 00000000000..df3d06293e3 --- /dev/null +++ b/test/function/samples/dynamic-import-name-warn/_config.js @@ -0,0 +1,32 @@ +const assert = require('assert'); + +module.exports = { + description: 'warns when specifying a custom importer function for formats other than "esm"', + context: { + require(path) { + assert.equal(path, './foo.js'); + return 42; + } + }, + options: { + input: 'main.js', + plugins: { + resolveDynamicImport() { + return false; + } + }, + output: { + dynamicImportFunction: 'myImporter', + format: 'cjs' + } + }, + exports(exports) { + return exports.fromFoo.then(value => assert.strictEqual(value, 42)); + }, + warnings: [ + { + code: 'INVALID_OPTION', + message: '"output.dynamicImportFunction" is ignored for formats other than "esm".' + } + ] +}; diff --git a/test/function/samples/dynamic-import-name-warn/_expected/amd/main.js b/test/function/samples/dynamic-import-name-warn/_expected/amd/main.js new file mode 100644 index 00000000000..b81e4d74b05 --- /dev/null +++ b/test/function/samples/dynamic-import-name-warn/_expected/amd/main.js @@ -0,0 +1,5 @@ +define(['require'], function (require) { 'use strict'; + + new Promise(function (resolve, reject) { require(['./foo.js'], resolve, reject) }); + +}); diff --git a/test/function/samples/dynamic-import-name-warn/_expected/cjs/main.js b/test/function/samples/dynamic-import-name-warn/_expected/cjs/main.js new file mode 100644 index 00000000000..b1acd589703 --- /dev/null +++ b/test/function/samples/dynamic-import-name-warn/_expected/cjs/main.js @@ -0,0 +1,3 @@ +'use strict'; + +Promise.resolve(require('./foo.js')); diff --git a/test/function/samples/dynamic-import-name-warn/_expected/es/main.js b/test/function/samples/dynamic-import-name-warn/_expected/es/main.js new file mode 100644 index 00000000000..51e216a3931 --- /dev/null +++ b/test/function/samples/dynamic-import-name-warn/_expected/es/main.js @@ -0,0 +1 @@ +foobar('./foo.js'); diff --git a/test/function/samples/dynamic-import-name-warn/_expected/system/main.js b/test/function/samples/dynamic-import-name-warn/_expected/system/main.js new file mode 100644 index 00000000000..0824bad8d3f --- /dev/null +++ b/test/function/samples/dynamic-import-name-warn/_expected/system/main.js @@ -0,0 +1,10 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + module.import('./foo.js'); + + } + }; +}); diff --git a/test/function/samples/dynamic-import-name-warn/main.js b/test/function/samples/dynamic-import-name-warn/main.js new file mode 100644 index 00000000000..ed74de99fab --- /dev/null +++ b/test/function/samples/dynamic-import-name-warn/main.js @@ -0,0 +1 @@ +export const fromFoo = import('./foo.js'); diff --git a/test/function/samples/dynamic-import-name/_config.js b/test/function/samples/dynamic-import-name/_config.js new file mode 100644 index 00000000000..e31e98bbc76 --- /dev/null +++ b/test/function/samples/dynamic-import-name/_config.js @@ -0,0 +1,27 @@ +const assert = require('assert'); +let imported = false; + +module.exports = { + description: 'allows specifying a custom importer function', + context: { + myImporter(path) { + assert.equal(path, './foo.js'); + imported = true; + } + }, + options: { + input: 'main.js', + plugins: { + resolveDynamicImport() { + return false; + } + }, + output: { + dynamicImportFunction: 'myImporter', + format: 'esm' + } + }, + exports() { + assert.ok(imported); + } +}; diff --git a/test/function/samples/dynamic-import-name/_expected/amd/main.js b/test/function/samples/dynamic-import-name/_expected/amd/main.js new file mode 100644 index 00000000000..b81e4d74b05 --- /dev/null +++ b/test/function/samples/dynamic-import-name/_expected/amd/main.js @@ -0,0 +1,5 @@ +define(['require'], function (require) { 'use strict'; + + new Promise(function (resolve, reject) { require(['./foo.js'], resolve, reject) }); + +}); diff --git a/test/function/samples/dynamic-import-name/_expected/cjs/main.js b/test/function/samples/dynamic-import-name/_expected/cjs/main.js new file mode 100644 index 00000000000..b1acd589703 --- /dev/null +++ b/test/function/samples/dynamic-import-name/_expected/cjs/main.js @@ -0,0 +1,3 @@ +'use strict'; + +Promise.resolve(require('./foo.js')); diff --git a/test/function/samples/dynamic-import-name/_expected/es/main.js b/test/function/samples/dynamic-import-name/_expected/es/main.js new file mode 100644 index 00000000000..51e216a3931 --- /dev/null +++ b/test/function/samples/dynamic-import-name/_expected/es/main.js @@ -0,0 +1 @@ +foobar('./foo.js'); diff --git a/test/function/samples/dynamic-import-name/_expected/system/main.js b/test/function/samples/dynamic-import-name/_expected/system/main.js new file mode 100644 index 00000000000..0824bad8d3f --- /dev/null +++ b/test/function/samples/dynamic-import-name/_expected/system/main.js @@ -0,0 +1,10 @@ +System.register([], function (exports, module) { + 'use strict'; + return { + execute: function () { + + module.import('./foo.js'); + + } + }; +}); diff --git a/test/function/samples/dynamic-import-name/main.js b/test/function/samples/dynamic-import-name/main.js new file mode 100644 index 00000000000..bbe4a5cc219 --- /dev/null +++ b/test/function/samples/dynamic-import-name/main.js @@ -0,0 +1 @@ +import('./foo.js'); diff --git a/test/function/samples/inline-imports-with-manual/_config.js b/test/function/samples/inline-imports-with-manual/_config.js index 3c74806bc17..2b3af2a2835 100644 --- a/test/function/samples/inline-imports-with-manual/_config.js +++ b/test/function/samples/inline-imports-with-manual/_config.js @@ -9,6 +9,6 @@ module.exports = { }, error: { code: 'INVALID_OPTION', - message: '"manualChunks" option is not supported for inlineDynamicImports.' + message: '"manualChunks" option is not supported for "inlineDynamicImports".' } }; diff --git a/test/function/samples/inline-imports-with-multiple/_config.js b/test/function/samples/inline-imports-with-multiple/_config.js index 651f9083d82..4b5bfd77774 100644 --- a/test/function/samples/inline-imports-with-multiple/_config.js +++ b/test/function/samples/inline-imports-with-multiple/_config.js @@ -6,6 +6,6 @@ module.exports = { }, error: { code: 'INVALID_OPTION', - message: 'Multiple inputs are not supported for inlineDynamicImports.' + message: 'Multiple inputs are not supported for "inlineDynamicImports".' } }; diff --git a/test/function/samples/inline-imports-with-optimize/_config.js b/test/function/samples/inline-imports-with-optimize/_config.js index 5f012bdf5e8..5e350bebd32 100644 --- a/test/function/samples/inline-imports-with-optimize/_config.js +++ b/test/function/samples/inline-imports-with-optimize/_config.js @@ -7,6 +7,6 @@ module.exports = { }, error: { code: 'INVALID_OPTION', - message: '"experimentalOptimizeChunks" option is not supported for inlineDynamicImports.' + message: '"experimentalOptimizeChunks" option is not supported for "inlineDynamicImports".' } }; diff --git a/test/function/samples/preserve-modules-with-inline/_config.js b/test/function/samples/preserve-modules-with-inline/_config.js index 1dff01e2788..1f25bd9968d 100644 --- a/test/function/samples/preserve-modules-with-inline/_config.js +++ b/test/function/samples/preserve-modules-with-inline/_config.js @@ -7,6 +7,6 @@ module.exports = { }, error: { code: 'INVALID_OPTION', - message: 'preserveModules does not support the inlineDynamicImports option.' + message: '"preserveModules" does not support the "inlineDynamicImports" option.' } }; diff --git a/test/function/samples/preserve-modules-with-manual/_config.js b/test/function/samples/preserve-modules-with-manual/_config.js index 1b3725e0008..3d11ec5f9b9 100644 --- a/test/function/samples/preserve-modules-with-manual/_config.js +++ b/test/function/samples/preserve-modules-with-manual/_config.js @@ -9,6 +9,6 @@ module.exports = { }, error: { code: 'INVALID_OPTION', - message: 'preserveModules does not support the manualChunks option.' + message: '"preserveModules" does not support the "manualChunks" option.' } }; diff --git a/test/function/samples/preserve-modules-with-optimize/_config.js b/test/function/samples/preserve-modules-with-optimize/_config.js index 94b7edd55d7..ef6bc94f059 100644 --- a/test/function/samples/preserve-modules-with-optimize/_config.js +++ b/test/function/samples/preserve-modules-with-optimize/_config.js @@ -7,6 +7,6 @@ module.exports = { }, error: { code: 'INVALID_OPTION', - message: 'preserveModules does not support the experimentalOptimizeChunks option.' + message: '"preserveModules" does not support the "experimentalOptimizeChunks" option.' } }; diff --git a/test/hooks/index.js b/test/hooks/index.js index 769d295f995..ef6907f57cf 100644 --- a/test/hooks/index.js +++ b/test/hooks/index.js @@ -1211,7 +1211,7 @@ module.exports = input; else if (evt.code === 'ERROR' || evt.code === 'FATAL') reject(evt.error); }); }).catch(err => { - assert.equal(err.message, 'You must specify output.file or output.dir for the build.'); + assert.equal(err.message, 'You must specify "output.file" or "output.dir" for the build.'); assert.equal(warned, true); }); }); diff --git a/test/misc/sanity-checks.js b/test/misc/sanity-checks.js index 9a89f4e52d0..e16b5ba2217 100644 --- a/test/misc/sanity-checks.js +++ b/test/misc/sanity-checks.js @@ -124,7 +124,7 @@ describe('sanity checks', () => { .then(bundle => { assert.throws(() => { bundle.generate({ file: 'x' }); - }, /You must specify output\.format, which can be one of 'amd', 'cjs', 'system', 'esm', 'iife' or 'umd'/); + }, /You must specify "output\.format", which can be one of "amd", "cjs", "system", "esm", "iife" or "umd"./); }); }); @@ -168,7 +168,7 @@ describe('sanity checks', () => { .then(bundle => { assert.throws(() => { bundle.generate({ file: 'x', format: 'es' }); - }, /You must set output\.dir instead of output\.file when generating multiple chunks\./); + }, /You must set "output\.dir" instead of "output\.file" when generating multiple chunks\./); }); }); @@ -196,7 +196,7 @@ describe('sanity checks', () => { .then(bundle => { assert.throws(() => { bundle.generate({ file: 'x', format: 'es' }); - }, /You must set output\.dir instead of output\.file when generating multiple chunks\./); + }, /You must set "output\.dir" instead of "output\.file" when generating multiple chunks\./); }); }); @@ -225,7 +225,7 @@ describe('sanity checks', () => { .then(bundle => { assert.throws(() => { bundle.generate({ file: 'x', format: 'es' }); - }, /You must set output\.dir instead of output\.file when providing named inputs\./); + }, /You must set "output\.dir" instead of "output\.file" when providing named inputs\./); }); }); @@ -242,7 +242,7 @@ describe('sanity checks', () => { .then(bundle => { assert.throws(() => { bundle.generate({ file: 'x', format: 'es' }); - }, /You must set output\.dir instead of output\.file when using the preserveModules option\./); + }, /You must set "output\.dir" instead of "output\.file" when using the "preserveModules" option\./); }); }); }); diff --git a/test/misc/write-bundle.js b/test/misc/write-bundle.js index dd2b8bfa7ed..9a14204457f 100644 --- a/test/misc/write-bundle.js +++ b/test/misc/write-bundle.js @@ -21,11 +21,11 @@ describe('bundle.write()', () => { .then(bundle => { assert.throws(() => { bundle.write(); - }, /You must specify output\.file/); + }, /You must specify "output\.file"/); assert.throws(() => { bundle.write({}); - }, /You must specify output\.file/); + }, /You must specify "output\.file"/); }); }); @@ -55,7 +55,7 @@ describe('bundle.write()', () => { .catch(err => { assert.throws(() => { throw err; - }, /You must supply output\.name for UMD bundles/); + }, /You must supply "output\.name" for UMD bundles/); }) .then(() => { return bundle.generate({ @@ -65,7 +65,7 @@ describe('bundle.write()', () => { .catch(err => { assert.throws(() => { throw err; - }, /You must supply output\.name for IIFE bundles/); + }, /You must supply "output\.name" for IIFE bundles/); }); }); @@ -87,7 +87,7 @@ describe('bundle.write()', () => { .then(bundle => { assert.throws(() => { return bundle.generate({ format: 'es6' }); - }, /The `es6` output format is deprecated – use `esm` instead/); + }, /The "es6" output format is deprecated – use "esm" instead/); }); }); @@ -110,7 +110,7 @@ describe('bundle.write()', () => { assert.equal(warnings.length, 0, 'No warnings for UNKNOWN'); assert.throws(() => { return Promise.all(options.output.map(o => bundle.write(o))); - }, /You must specify output\.file/); + }, /You must specify "output\.file"./); }); }); }); From d9337fa338d190f074815a2cf9e98ceae58cafaf Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 1 Mar 2019 10:52:29 +0100 Subject: [PATCH 5/6] Fix merge --- src/rollup/index.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/rollup/index.ts b/src/rollup/index.ts index ceb35047756..6ad366c878e 100644 --- a/src/rollup/index.ts +++ b/src/rollup/index.ts @@ -177,15 +177,6 @@ export default function rollup(rawInputOptions: GenericConfigObject): Promise 1 ); - if (outputOptions.format !== 'esm' && outputOptions.dynamicImportFunction !== 'import') { - inputOptions.onwarn({ - code: 'INVALID_OPTION', - message: `dynamicImportFunction is not used when outputting ${ - outputOptions.format - }. This setting will be ignored.` - }); - } - timeStart('GENERATE', 1); const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]'; From 97edd9751e687bd495a4b05143975aab24c45076 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 1 Mar 2019 10:58:30 +0100 Subject: [PATCH 6/6] Add dynamicImportFunction to two more places and fix sorting --- bin/src/help.md | 2 +- docs/01-command-line-reference.md | 1 + docs/02-javascript-api.md | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/src/help.md b/bin/src/help.md index a5f69274379..6258c36649b 100644 --- a/bin/src/help.md +++ b/bin/src/help.md @@ -25,6 +25,7 @@ Basic options: --chunkFileNames Name pattern for emitted secondary chunks --compact Minify wrapper code --context Specify top-level `this` value +--dynamicImportFunction Rename the dynamic `import()` function --entryFileNames Name pattern for emitted entry chunks --environment Settings passed to config file (see example) --no-esModule Do not add __esModule property @@ -51,7 +52,6 @@ Basic options: --no-treeshake.annotations Ignore pure call annotations --no-treeshake.propertyReadSideEffects Ignore property access side-effects --treeshake.pureExternalModules Assume side-effect free externals ---dynamicImportFunction Rename the dynamic `import()` function Examples: diff --git a/docs/01-command-line-reference.md b/docs/01-command-line-reference.md index cad22f778c4..51f41d9a51b 100755 --- a/docs/01-command-line-reference.md +++ b/docs/01-command-line-reference.md @@ -85,6 +85,7 @@ export default { // can be an array (for multiple inputs) // danger zone amd, + dynamicImportFunction, esModule, exports, freeze, diff --git a/docs/02-javascript-api.md b/docs/02-javascript-api.md index 4d246b2979c..4cb6a3f85e9 100755 --- a/docs/02-javascript-api.md +++ b/docs/02-javascript-api.md @@ -134,6 +134,7 @@ const outputOptions = { // danger zone amd, + dynamicImportFunction, esModule, exports, freeze,