Skip to content

Commit

Permalink
Deprecate dynamicImportFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 23, 2020
1 parent 605a825 commit 096f224
Show file tree
Hide file tree
Showing 28 changed files with 136 additions and 22 deletions.
1 change: 0 additions & 1 deletion cli/help.md
Expand Up @@ -26,7 +26,6 @@ Basic options:
--chunkFileNames <pattern> Name pattern for emitted secondary chunks
--compact Minify wrapper code
--context <variable> Specify top-level `this` value
--dynamicImportFunction <name> Rename the dynamic `import()` function
--entryFileNames <pattern> Name pattern for emitted entry chunks
--environment <values> Settings passed to config file (see example)
--no-esModule Do not add __esModule property
Expand Down
2 changes: 0 additions & 2 deletions docs/01-command-line-reference.md
Expand Up @@ -85,7 +85,6 @@ export default { // can be an array (for multiple inputs)

// danger zone
amd,
dynamicImportFunction,
esModule,
exports,
externalLiveBindings,
Expand Down Expand Up @@ -228,7 +227,6 @@ Many options have command line equivalents. In those cases, any arguments passed
--chunkFileNames <pattern> Name pattern for emitted secondary chunks
--compact Minify wrapper code
--context <variable> Specify top-level `this` value
--dynamicImportFunction <name> Rename the dynamic `import()` function
--entryFileNames <pattern> Name pattern for emitted entry chunks
--environment <values> Settings passed to config file (see example)
--no-esModule Do not add __esModule property
Expand Down
1 change: 0 additions & 1 deletion docs/02-javascript-api.md
Expand Up @@ -136,7 +136,6 @@ const outputOptions = {

// danger zone
amd,
dynamicImportFunction,
esModule,
exports,
freeze,
Expand Down
16 changes: 9 additions & 7 deletions docs/999-big-list-of-options.md
Expand Up @@ -848,13 +848,6 @@ Default: `true`

Whether to include the 'use strict' pragma at the top of generated non-ES bundles. Strictly speaking, ES modules are *always* in strict mode, so you shouldn't disable this without good reason.

#### output.dynamicImportFunction
Type: `string`<br>
CLI: `--dynamicImportFunction <name>`<br>
Default: `import`

This will rename the dynamic import function to the chosen name when outputting ES 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`<br>
CLI: `--preserveSymlinks`<br>
Expand Down Expand Up @@ -1133,6 +1126,7 @@ export default {
☢️ These options have been deprecated and may be removed in a future Rollup version.

#### treeshake.pureExternalModules
_Use [`treeshake.moduleSideEffects: 'no-external'`](guide/en/#treeshake) instead._<br>
Type: `boolean | string[] | (id: string) => boolean | null`<br>
CLI: `--treeshake.pureExternalModules`/`--no-treeshake.pureExternalModules`<br>
Default: `false`
Expand All @@ -1159,3 +1153,11 @@ console.log(42);
```

You can also supply a list of external ids to be considered pure or a function that is called whenever an external import could be removed.

#### output.dynamicImportFunction
_Use the [`renderDynamicImport`](guide/en/#renderdynamicimport) plugin hook instead._<br>
Type: `string`<br>
CLI: `--dynamicImportFunction <name>`<br>
Default: `import`

This will rename the dynamic import function to the chosen name when outputting ES bundles. This is useful for generating code that uses a dynamic import polyfill such as [this one](https://github.com/uupaa/dynamic-import-polyfill).
6 changes: 6 additions & 0 deletions src/rollup/rollup.ts
Expand Up @@ -197,6 +197,12 @@ export async function rollupInternal(
): Promise<OutputBundle> {
timeStart('GENERATE', 1);

if (outputOptions.dynamicImportFunction) {
graph.warnDeprecation(
`The "output.dynamicImportFunction" option is deprecated. Use the "renderDynamicImport" plugin hook instead.`,
false
);
}
const assetFileNames = outputOptions.assetFileNames || 'assets/[name]-[hash][extname]';
const inputBase = commondir(getAbsoluteEntryModulePaths(chunks));
const outputBundleWithPlaceholders: OutputBundleWithPlaceholders = Object.create(null);
Expand Down
3 changes: 1 addition & 2 deletions src/rollup/types.d.ts
Expand Up @@ -484,6 +484,7 @@ export interface OutputOptions {
compact?: boolean;
// only required for bundle.write
dir?: string;
/** @deprecated Use the "renderDynamicImport" plugin hook instead. */
dynamicImportFunction?: string;
entryFileNames?: string;
esModule?: boolean;
Expand All @@ -493,8 +494,6 @@ export interface OutputOptions {
// only required for bundle.write
file?: string;
footer?: string | (() => string | Promise<string>);
// this is optional at the base-level of RollupWatchOptions,
// which extends from this interface through config merge
format?: ModuleFormat;
freeze?: boolean;
globals?: GlobalsOption;
Expand Down
@@ -0,0 +1,16 @@
module.exports = {
description: 'should not remove inline comments inside dynamic import',
options: {
strictDeprecations: false,
input: 'main.js',
onwarn() {},
plugins: {
resolveDynamicImport() {
return false;
}
},
output: {
dynamicImportFunction: 'foobar'
}
}
};
@@ -0,0 +1,26 @@
define(['require'], function (require) { 'use strict';

function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = {};
if (e) {
Object.keys(e).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
});
}
n['default'] = e;
return n;
}
}

new Promise(function (resolve, reject) { require([
/* webpackChunkName: "chunk-name" */
'./foo'/*suffix*/], function (m) { resolve(_interopNamespace(m)); }, reject) });

});
@@ -0,0 +1,24 @@
'use strict';

function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = {};
if (e) {
Object.keys(e).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
});
}
n['default'] = e;
return n;
}
}

new Promise(function (resolve) { resolve(_interopNamespace(require(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/))); });
@@ -0,0 +1,3 @@
foobar(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

module.import(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);

}
};
});
@@ -0,0 +1,3 @@
import /* () should not break */ (
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);
@@ -1,6 +1,7 @@
module.exports = {
description: 'allows specifying a custom importer function',
options: {
strictDeprecations: false,
input: 'main.js',
onwarn() {},
plugins: {
Expand Down
8 changes: 4 additions & 4 deletions test/chunking-form/samples/dynamic-import-comments/_config.js
@@ -1,15 +1,15 @@
module.exports = {
description: 'should not remove inline comments inside dynamic import',
description: 'does not remove inline comments inside dynamic imports',
options: {
input: 'main.js',
onwarn() {},
plugins: {
renderDynamicImport() {
return { left: 'foobar(', right: ')' };
},
resolveDynamicImport() {
return false;
}
},
output: {
dynamicImportFunction: 'foobar'
}
}
};
Expand Up @@ -19,8 +19,8 @@ define(['require'], function (require) { 'use strict';
}
}

new Promise(function (resolve, reject) { require([
foobar(
/* webpackChunkName: "chunk-name" */
'./foo'/*suffix*/], function (m) { resolve(_interopNamespace(m)); }, reject) });
'./foo'/*suffix*/);

});
Expand Up @@ -19,6 +19,6 @@ function _interopNamespace(e) {
}
}

new Promise(function (resolve) { resolve(_interopNamespace(require(
foobar(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/))); });
'./foo.js'/*suffix*/);
Expand Up @@ -3,7 +3,7 @@ System.register([], function (exports, module) {
return {
execute: function () {

module.import(
foobar(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);

Expand Down
Expand Up @@ -9,6 +9,7 @@ module.exports = {
}
},
options: {
strictDeprecations: false,
input: 'main.js',
plugins: {
resolveDynamicImport() {
Expand Down
Expand Up @@ -10,6 +10,7 @@ module.exports = {
}
},
options: {
strictDeprecations: false,
input: 'main.js',
plugins: {
resolveDynamicImport() {
Expand Down
@@ -0,0 +1,13 @@
module.exports = {
description: 'marks the "output.dynamicImportFunction" option as deprecated',
options: {
output: {
dynamicImportFunction: 'foo'
}
},
generateError: {
code: 'DEPRECATED_FEATURE',
message:
'The "output.dynamicImportFunction" option is deprecated. Use the "renderDynamicImport" plugin hook instead.'
}
};
11 changes: 11 additions & 0 deletions test/function/samples/deprecations/dynamicImportFunction/main.js
@@ -0,0 +1,11 @@
const foo = {};

function doIt(x) {
if (foo[x]) {
return true;
}
foo[x] = true;
}

doIt('x');
assert.ok(doIt('x'), 'foo was not reassigned');

0 comments on commit 096f224

Please sign in to comment.