Skip to content

Commit aeaa877

Browse files
authored
Merge pull request webpack#7034 from webpack/feature/import_ignore
Add comment directive to disable import parsing
2 parents cf226ef + 32bbb84 commit aeaa877

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

lib/dependencies/ImportParserPlugin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ class ImportParserPlugin {
3333

3434
const importOptions = parser.getCommentOptions(expr.range);
3535
if (importOptions) {
36+
if (typeof importOptions.webpackIgnore !== "undefined") {
37+
if (typeof importOptions.webpackIgnore !== "boolean") {
38+
parser.state.module.warnings.push(
39+
new UnsupportedFeatureWarning(
40+
parser.state.module,
41+
`\`webpackIgnore\` expected a boolean, but received: ${
42+
importOptions.webpackIgnore
43+
}.`
44+
)
45+
);
46+
} else {
47+
// Do not instrument `import()` is `webpackIgnore` is `true`
48+
if (importOptions.webpackIgnore) {
49+
return false;
50+
}
51+
}
52+
}
3653
if (typeof importOptions.webpackChunkName !== "undefined") {
3754
if (typeof importOptions.webpackChunkName !== "string") {
3855
parser.state.module.warnings.push(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const should = require("should");
4+
5+
it("should be able to ignore import()", () => {
6+
const source = fs.readFileSync(path.join(__dirname, "bundle1.js"), "utf-8");
7+
should(source).containEql(`import(/* webpackIgnore: true */ "./other2.js")`);
8+
should(source).not.containEql(`import(/* webpackIgnore: false */ "./other3.js")`);
9+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import(/* webpackIgnore: true */ "./other2.js");
2+
import(/* webpackIgnore: false */ "./other3.js");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "other2";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "other3";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
entry: {
3+
bundle0: "./index.js",
4+
bundle1: "./other.js"
5+
},
6+
output: {
7+
filename: "[name].js"
8+
},
9+
node: {
10+
__dirname: false
11+
}
12+
};

0 commit comments

Comments
 (0)