Skip to content

Commit c6582ce

Browse files
authored
Merge pull request #12205 from webpack/bugfix/side-effects
Bugfix/side effects
2 parents 04b66d6 + 06fcc0a commit c6582ce

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

lib/FlagAllModulesAsUsedPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class FlagAllModulesAsUsedPlugin {
4040
const exportsInfo = moduleGraph.getExportsInfo(module);
4141
exportsInfo.setUsedInUnknownWay(runtime);
4242
moduleGraph.addExtraReason(module, this.explanation);
43+
if (module.factoryMeta === undefined) {
44+
module.factoryMeta = {};
45+
}
46+
module.factoryMeta.sideEffectFree = false;
4347
}
4448
}
4549
);

lib/NormalModule.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,10 @@ class NormalModule extends Module {
868868
* @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
869869
*/
870870
getSideEffectsConnectionState(moduleGraph) {
871-
if (this.factoryMeta !== undefined && this.factoryMeta.sideEffectFree)
872-
return false;
871+
if (this.factoryMeta !== undefined) {
872+
if (this.factoryMeta.sideEffectFree) return false;
873+
if (this.factoryMeta.sideEffectFree === false) return true;
874+
}
873875
if (this.buildMeta !== undefined && this.buildMeta.sideEffectFree) {
874876
if (this._isEvaluatingSideEffects)
875877
return ModuleGraphConnection.CIRCULAR_CONNECTION;

lib/optimize/SideEffectsFlagPlugin.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ class SideEffectsFlagPlugin {
8282
resolveData.relativePath
8383
) {
8484
const sideEffects = resolveData.descriptionFileData.sideEffects;
85-
const hasSideEffects = SideEffectsFlagPlugin.moduleHasSideEffects(
86-
resolveData.relativePath,
87-
sideEffects,
88-
cache
89-
);
90-
if (!hasSideEffects) {
85+
if (sideEffects !== undefined) {
9186
if (module.factoryMeta === undefined) {
9287
module.factoryMeta = {};
9388
}
94-
module.factoryMeta.sideEffectFree = true;
89+
const hasSideEffects = SideEffectsFlagPlugin.moduleHasSideEffects(
90+
resolveData.relativePath,
91+
sideEffects,
92+
cache
93+
);
94+
module.factoryMeta.sideEffectFree = !hasSideEffects;
9595
}
9696
}
9797

@@ -101,15 +101,11 @@ class SideEffectsFlagPlugin {
101101
normalModuleFactory.hooks.module.tap(
102102
"SideEffectsFlagPlugin",
103103
(module, data) => {
104-
if (data.settings.sideEffects === false) {
104+
if (typeof data.settings.sideEffects === "boolean") {
105105
if (module.factoryMeta === undefined) {
106106
module.factoryMeta = {};
107107
}
108-
module.factoryMeta.sideEffectFree = true;
109-
} else if (data.settings.sideEffects === true) {
110-
if (module.factoryMeta !== undefined) {
111-
module.factoryMeta.sideEffectFree = false;
112-
}
108+
module.factoryMeta.sideEffectFree = !data.settings.sideEffects;
113109
}
114110
return module;
115111
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export default "d";
2-
console.log.bind(console);

test/configCases/dll-plugin/2-use-dll-without-scope/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@ import { x2, y1 } from "../0-create-dll/e";
44
import { B } from "../0-create-dll/h";
55
import { A } from "../0-create-dll/h1";
66

7-
it("should load a module from dll", function() {
7+
it("should load a module from dll", function () {
88
expect(require("../0-create-dll/a")).toBe("a");
99
});
1010

11-
it("should load a module of non-default type without extension from dll", function() {
11+
it("should load a module of non-default type without extension from dll", function () {
1212
expect(require("../0-create-dll/f")).toBe("f");
1313
});
1414

15-
it("should load an async module from dll", function(done) {
15+
it("should load an async module from dll", function (done) {
1616
require("../0-create-dll/b")()
17-
.then(function(c) {
17+
.then(function (c) {
1818
expect(c).toEqual(nsObj({ default: "c" }));
1919
done();
2020
})
2121
.catch(done);
2222
});
2323

24-
it("should load an harmony module from dll (default export)", function() {
24+
it("should load an harmony module from dll (default export)", function () {
2525
expect(d).toBe("d");
2626
});
2727

28-
it("should load an harmony module from dll (star export)", function() {
28+
it("should load an harmony module from dll (star export)", function () {
2929
expect(x1).toBe(123);
3030
expect(x2).toBe(123);
3131
expect(y1).toBe(456);
3232
expect(y2).toBe(456);
3333
});
3434

35-
it("should load a module with loader applied", function() {
35+
it("should load a module with loader applied", function () {
3636
expect(require("../0-create-dll/g.abc.js")).toBe("number");
3737
});
3838

39-
it("should give modules the correct ids", function() {
39+
it("should give modules the correct ids", function () {
4040
expect(
4141
Object.keys(__webpack_modules__)
4242
.filter(m => !m.startsWith("../.."))
@@ -51,16 +51,16 @@ it("should give modules the correct ids", function() {
5151
"../0-create-dll/f.jsx",
5252
"../0-create-dll/g.abc.js",
5353
"../0-create-dll/h.js",
54-
"../0-create-dll/hb.js",
54+
"../0-create-dll/h1.js",
5555
"./index.js",
5656
"dll-reference ../0-create-dll/dll.js"
5757
]);
5858
});
5959

60-
it("should not crash on side-effect-free modules", function() {
60+
it("should not crash on side-effect-free modules", function () {
6161
expect(B).toBe("B");
6262
});
6363

64-
it("should be able to reference side-effect-free reexport-only module", function() {
64+
it("should be able to reference side-effect-free reexport-only module", function () {
6565
expect(A).toBe("A");
6666
});

0 commit comments

Comments
 (0)