Skip to content

Commit ee2631d

Browse files
fix: set side effects to false (#122)
1 parent 4ded08c commit ee2631d

File tree

8 files changed

+268
-10
lines changed

8 files changed

+268
-10
lines changed

package-lock.json

Lines changed: 134 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@
6767
"memfs": "^3.2.0",
6868
"npm-run-all": "^4.1.5",
6969
"prettier": "^2.2.0",
70+
"react": "^17.0.1",
7071
"rx": "^4.1.0",
7172
"standard-version": "^9.0.0",
73+
"styled-components": "^5.2.1",
7274
"webpack": "^5.6.0"
7375
},
7476
"keywords": [

src/index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ export default function loader() {
2424
baseDataPath: "options",
2525
});
2626

27-
/*
28-
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
29-
*
30-
* Fixes:
31-
* - https://github.com/webpack-contrib/expose-loader/issues/55
32-
* - https://github.com/webpack-contrib/expose-loader/issues/49
33-
*/
34-
this._module.userRequest = getNewUserRequest(this._module.userRequest);
35-
3627
const callback = this.async();
3728

3829
let exposes;
@@ -45,6 +36,25 @@ export default function loader() {
4536
return;
4637
}
4738

39+
/*
40+
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
41+
*
42+
* Fixes:
43+
* - https://github.com/webpack-contrib/expose-loader/issues/55
44+
* - https://github.com/webpack-contrib/expose-loader/issues/49
45+
*/
46+
this._module.userRequest = getNewUserRequest(this._module.userRequest);
47+
48+
/*
49+
* Adding side effects
50+
*
51+
* Fixes:
52+
* - https://github.com/webpack-contrib/expose-loader/issues/120
53+
*/
54+
if (this._module.factoryMeta) {
55+
this._module.factoryMeta.sideEffectFree = false;
56+
}
57+
4858
// Change the request from an /abolute/path.js to a relative ./path.js.
4959
// This prevents [chunkhash] values from changing when running webpack builds in different directories.
5060
const newRequest = contextify(this.context, getRemainingRequest(this));

test/__snapshots__/loader.test.js.snap

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,92 @@ Object {
10061006

10071007
exports[`loader should work with multiple exposes: warnings 1`] = `Array []`;
10081008

1009+
exports[`loader should work with side-effects free modules #1: errors 1`] = `Array []`;
1010+
1011+
exports[`loader should work with side-effects free modules #1: result 1`] = `
1012+
Object {
1013+
"ExposeLoader": Object {
1014+
"default": Object {
1015+
"ServerStyleSheet": [Function],
1016+
"StyleSheetConsumer": Object {
1017+
"$$typeof": Symbol(react.context),
1018+
"Consumer": [Circular],
1019+
"Provider": Object {
1020+
"$$typeof": Symbol(react.provider),
1021+
"_context": [Circular],
1022+
},
1023+
"_calculateChangedBits": null,
1024+
"_currentValue": undefined,
1025+
"_currentValue2": undefined,
1026+
"_threadCount": 0,
1027+
},
1028+
"StyleSheetContext": Object {
1029+
"$$typeof": Symbol(react.context),
1030+
"Consumer": [Circular],
1031+
"Provider": Object {
1032+
"$$typeof": Symbol(react.provider),
1033+
"_context": [Circular],
1034+
},
1035+
"_calculateChangedBits": null,
1036+
"_currentValue": undefined,
1037+
"_currentValue2": undefined,
1038+
"_threadCount": 0,
1039+
},
1040+
"StyleSheetManager": [Function],
1041+
"ThemeConsumer": Object {
1042+
"$$typeof": Symbol(react.context),
1043+
"Consumer": [Circular],
1044+
"Provider": Object {
1045+
"$$typeof": Symbol(react.provider),
1046+
"_context": [Circular],
1047+
},
1048+
"_calculateChangedBits": null,
1049+
"_currentValue": undefined,
1050+
"_currentValue2": undefined,
1051+
"_threadCount": 0,
1052+
},
1053+
"ThemeContext": Object {
1054+
"$$typeof": Symbol(react.context),
1055+
"Consumer": [Circular],
1056+
"Provider": Object {
1057+
"$$typeof": Symbol(react.provider),
1058+
"_context": [Circular],
1059+
},
1060+
"_calculateChangedBits": null,
1061+
"_currentValue": undefined,
1062+
"_currentValue2": undefined,
1063+
"_threadCount": 0,
1064+
},
1065+
"ThemeProvider": [Function],
1066+
"__PRIVATE__": Object {
1067+
"StyleSheet": [Function],
1068+
"masterSheet": e {
1069+
"gs": Object {},
1070+
"names": Map {},
1071+
"options": Object {
1072+
"isServer": true,
1073+
"useCSSOMInjection": true,
1074+
},
1075+
},
1076+
},
1077+
"createGlobalStyle": [Function],
1078+
"css": [Function],
1079+
"default": [Function],
1080+
"isStyledComponent": [Function],
1081+
"keyframes": [Function],
1082+
"useTheme": [Function],
1083+
"version": "5.2.1",
1084+
"withTheme": [Function],
1085+
},
1086+
},
1087+
"global-commonjs2-single-export": Object {
1088+
"foo": "bar",
1089+
},
1090+
}
1091+
`;
1092+
1093+
exports[`loader should work with side-effects free modules #1: warnings 1`] = `Array []`;
1094+
10091095
exports[`loader should work with side-effects free modules: errors 1`] = `Array []`;
10101096

10111097
exports[`loader should work with side-effects free modules: module 1`] = `

test/fixtures/side-effects-1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import "styled-components";
2+
import myGlobalThis from '../../src/runtime/getGlobalThis';
3+
4+
const exported = myGlobalThis.styled;
5+
6+
export default exported;

test/helpers/execute.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default (code) => {
1515
// eslint-disable-next-line no-underscore-dangle
1616
module._compile(
1717
`
18+
window = {};
1819
console.log = () => {};
1920
2021
const result = {};

test/helpers/getCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
1919
module: {
2020
rules: [
2121
{
22-
test: /(global-.+|rx\.all)\.js/i,
22+
test: /(global-.+|rx\.all|styled-components.+)\.js/i,
2323
rules: [
2424
{
2525
loader: path.resolve(__dirname, "../../src"),

test/loader.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,25 @@ describe("loader", () => {
736736
expect(getWarnings(stats)).toMatchSnapshot("warnings");
737737
});
738738

739+
it("should work with side-effects free modules #1", async () => {
740+
const compiler = getCompiler(
741+
"side-effects-1.js",
742+
{
743+
exposes: "styled",
744+
},
745+
{
746+
mode: "production",
747+
}
748+
);
749+
const stats = await compile(compiler);
750+
751+
expect(
752+
execute(readAsset("main.bundle.js", compiler, stats))
753+
).toMatchSnapshot("result");
754+
expect(getErrors(stats)).toMatchSnapshot("errors");
755+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
756+
});
757+
739758
it("should throw an error on invalid exposed value", async () => {
740759
const compiler = getCompiler("simple-commonjs2-single-export.js", {
741760
exposes: "myGlobal foo bar baz",

0 commit comments

Comments
 (0)