Skip to content

Commit

Permalink
Merge pull request #15494 from webpack/feature/global-options-for-dyn…
Browse files Browse the repository at this point in the history
…amic-import

add parser options for dynamic import
  • Loading branch information
sokra committed May 10, 2022
2 parents 96c57bd + ee6b53d commit e2df0dd
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 29 deletions.
12 changes: 12 additions & 0 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2929,6 +2929,18 @@ export interface JavascriptParserOptions {
* Enable/disable parsing of magic comments in CommonJs syntax.
*/
commonjsMagicComments?: boolean;
/**
* Specifies global mode for dynamic import.
*/
dynamicImportMode?: "eager" | "weak" | "lazy" | "lazy-once";
/**
* Specifies global prefetch for dynamic import.
*/
dynamicImportPrefetch?: number | boolean;
/**
* Specifies global preload for dynamic import.
*/
dynamicImportPreload?: number | boolean;
/**
* Specifies the behavior of invalid export names in "import ... from ..." and "export ... from ...".
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ const applyJavascriptParserOptionsDefaults = (
D(parserOptions, "wrappedContextCritical", false);
D(parserOptions, "strictThisContextOnImports", false);
D(parserOptions, "importMeta", true);
D(parserOptions, "dynamicImportMode", "lazy");
D(parserOptions, "dynamicImportPrefetch", false);
D(parserOptions, "dynamicImportPreload", false);
if (futureDefaults) D(parserOptions, "exportsPresence", "error");
};

Expand Down
56 changes: 31 additions & 25 deletions lib/dependencies/ImportParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ const ImportDependency = require("./ImportDependency");
const ImportEagerDependency = require("./ImportEagerDependency");
const ImportWeakDependency = require("./ImportWeakDependency");

/** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
/** @typedef {import("../ChunkGroup").RawChunkGroupOptions} RawChunkGroupOptions */
/** @typedef {import("../ContextModule").ContextMode} ContextMode */

class ImportParserPlugin {
/**
* @param {JavascriptParserOptions} options options
*/
constructor(options) {
this.options = options;
}
Expand All @@ -28,14 +32,25 @@ class ImportParserPlugin {

let chunkName = null;
/** @type {ContextMode} */
let mode = "lazy";
let mode = this.options.dynamicImportMode;
let include = null;
let exclude = null;
/** @type {string[][] | null} */
let exports = null;
/** @type {RawChunkGroupOptions} */
const groupOptions = {};

const { dynamicImportPreload, dynamicImportPrefetch } = this.options;
if (dynamicImportPreload !== undefined && dynamicImportPreload !== false)
groupOptions.preloadOrder =
dynamicImportPreload === true ? 0 : dynamicImportPreload;
if (
dynamicImportPrefetch !== undefined &&
dynamicImportPrefetch !== false
)
groupOptions.prefetchOrder =
dynamicImportPrefetch === true ? 0 : dynamicImportPrefetch;

const { options: importOptions, errors: commentErrors } =
parser.parseCommentOptions(expr.range);

Expand Down Expand Up @@ -175,16 +190,22 @@ class ImportParserPlugin {
}
}

if (param.isString()) {
if (mode !== "lazy" && mode !== "eager" && mode !== "weak") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackMode\` expected 'lazy', 'eager' or 'weak', but received: ${mode}.`,
expr.loc
)
);
}
if (
mode !== "lazy" &&
mode !== "lazy-once" &&
mode !== "eager" &&
mode !== "weak"
) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
expr.loc
)
);
mode = "lazy";
}

if (param.isString()) {
if (mode === "eager") {
const dep = new ImportEagerDependency(
param.string,
Expand Down Expand Up @@ -215,21 +236,6 @@ class ImportParserPlugin {
}
return true;
} else {
if (
mode !== "lazy" &&
mode !== "lazy-once" &&
mode !== "eager" &&
mode !== "weak"
) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackMode\` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${mode}.`,
expr.loc
)
);
mode = "lazy";
}

if (mode === "weak") {
mode = "async-weak";
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.check.js

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,32 @@
"description": "Enable/disable parsing of magic comments in CommonJs syntax.",
"type": "boolean"
},
"dynamicImportMode": {
"description": "Specifies global mode for dynamic import.",
"enum": ["eager", "weak", "lazy", "lazy-once"]
},
"dynamicImportPrefetch": {
"description": "Specifies global prefetch for dynamic import.",
"anyOf": [
{
"type": "number"
},
{
"type": "boolean"
}
]
},
"dynamicImportPreload": {
"description": "Specifies global preload for dynamic import.",
"anyOf": [
{
"type": "number"
},
{
"type": "boolean"
}
]
},
"exportsPresence": {
"description": "Specifies the behavior of invalid export names in \"import ... from ...\" and \"export ... from ...\".",
"enum": ["error", "warn", "auto", false]
Expand Down
3 changes: 3 additions & 0 deletions test/Defaults.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ describe("snapshots", () => {
},
},
"javascript": Object {
"dynamicImportMode": "lazy",
"dynamicImportPrefetch": false,
"dynamicImportPreload": false,
"exprContextCritical": true,
"exprContextRecursive": true,
"exprContextRegExp": false,
Expand Down

0 comments on commit e2df0dd

Please sign in to comment.