Skip to content

Commit 01cf92e

Browse files
test: cover the deferred module instantiations
Drop the annotation from the eager slot of a wrapped concatenation member: the root is the only member ever reached that way, and every shape that wraps it routes a `require()` through its own imports, so it is never side-effect free there and the annotation could not be emitted. Refs #17121
1 parent 6ecd97c commit 01cf92e

6 files changed

Lines changed: 63 additions & 8 deletions

File tree

‎lib/optimize/ConcatenatedModule.js‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,10 +2866,7 @@ ${defineGetters}`
28662866
result.add(
28672867
`\n;// ${info.module.readableIdentifier(
28682868
requestShortener
2869-
)}\n${runtimeTemplate.moduleSideEffectsAnnotation(
2870-
info.module,
2871-
moduleGraph
2872-
)}${info.namespaceFnName}();\n`
2869+
)}\n${info.namespaceFnName}();\n`
28732870
);
28742871
} else if (!info.wrapped) {
28752872
result.add(
@@ -2948,10 +2945,7 @@ ${defineGetters}`
29482945
result.add(
29492946
`\n;// EXTERNAL MODULE: ${info.module.readableIdentifier(
29502947
requestShortener
2951-
)}\n${runtimeTemplate.moduleSideEffectsAnnotation(
2952-
info.module,
2953-
moduleGraph
2954-
)}${info.namespaceFnName}();\n`
2948+
)}\n${info.namespaceFnName}();\n`
29552949
);
29562950
}
29572951
// If a module is deferred in other places, but used as non-deferred here,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { readLazy } from "./lib.js";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
const read = (name) =>
7+
fs.readFileSync(path.join(path.dirname(__filename), name), "utf-8");
8+
9+
it("should evaluate the deferred module on access", () => {
10+
expect(readLazy(6, 7)).toBe(43);
11+
});
12+
13+
it("should annotate a deferred import", () => {
14+
expect(read("bundle0.js")).toMatch(
15+
/deferred harmony import \*\/ var \w+ = \/\*#__PURE__\*\/__webpack_require__\.zO\(/
16+
);
17+
});
18+
19+
it("should annotate a deferred module a concatenation reaches", () => {
20+
expect(read("bundle1.js")).toMatch(
21+
/DEFERRED EXTERNAL MODULE: \.\/pure-cjs\.js\nvar \w+ = \/\*#__PURE__\*\/__webpack_require__\.zO\(/
22+
);
23+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as lazy from /* webpackDefer: true */ "./pure-cjs";
2+
import { extra } from "./side.js";
3+
4+
export function readLazy(a, b) {
5+
return lazy.mul(a, b) + extra(0);
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.mul = function (a, b) {
2+
return a * b;
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function extra(a) {
2+
return a + 1;
3+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const target = `async-node${process.versions.node.split(".").map(Number)[0]}`;
4+
5+
/** @type {import("../../../../types").Configuration[]} */
6+
module.exports = [
7+
{
8+
name: "plain",
9+
target,
10+
mode: "production",
11+
experiments: { deferImport: true },
12+
optimization: {
13+
// Renders the deferred import through `importStatement`.
14+
concatenateModules: false
15+
}
16+
},
17+
{
18+
name: "concatenated",
19+
target,
20+
mode: "production",
21+
experiments: { deferImport: true },
22+
// A CommonJS module cannot join the concatenation, so it stays an external
23+
// member of it and `ConcatenatedModule` renders the deferred loader itself.
24+
optimization: { concatenateModules: true }
25+
}
26+
];

0 commit comments

Comments
 (0)