Skip to content

fix: set side effects to false #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.0",
"react": "^17.0.1",
"rx": "^4.1.0",
"standard-version": "^9.0.0",
"styled-components": "^5.2.1",
"webpack": "^5.6.0"
},
"keywords": [
Expand Down
28 changes: 19 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ export default function loader() {
baseDataPath: "options",
});

/*
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
*
* Fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/55
* - https://github.com/webpack-contrib/expose-loader/issues/49
*/
this._module.userRequest = getNewUserRequest(this._module.userRequest);

const callback = this.async();

let exposes;
Expand All @@ -45,6 +36,25 @@ export default function loader() {
return;
}

/*
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
*
* Fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/55
* - https://github.com/webpack-contrib/expose-loader/issues/49
*/
this._module.userRequest = getNewUserRequest(this._module.userRequest);

/*
* Adding side effects
*
* Fixes:
* - https://github.com/webpack-contrib/expose-loader/issues/120
*/
if (this._module.factoryMeta) {
this._module.factoryMeta.sideEffectFree = false;
}

// Change the request from an /abolute/path.js to a relative ./path.js.
// This prevents [chunkhash] values from changing when running webpack builds in different directories.
const newRequest = contextify(this.context, getRemainingRequest(this));
Expand Down
86 changes: 86 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,92 @@ Object {

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

exports[`loader should work with side-effects free modules #1: errors 1`] = `Array []`;

exports[`loader should work with side-effects free modules #1: result 1`] = `
Object {
"ExposeLoader": Object {
"default": Object {
"ServerStyleSheet": [Function],
"StyleSheetConsumer": Object {
"$$typeof": Symbol(react.context),
"Consumer": [Circular],
"Provider": Object {
"$$typeof": Symbol(react.provider),
"_context": [Circular],
},
"_calculateChangedBits": null,
"_currentValue": undefined,
"_currentValue2": undefined,
"_threadCount": 0,
},
"StyleSheetContext": Object {
"$$typeof": Symbol(react.context),
"Consumer": [Circular],
"Provider": Object {
"$$typeof": Symbol(react.provider),
"_context": [Circular],
},
"_calculateChangedBits": null,
"_currentValue": undefined,
"_currentValue2": undefined,
"_threadCount": 0,
},
"StyleSheetManager": [Function],
"ThemeConsumer": Object {
"$$typeof": Symbol(react.context),
"Consumer": [Circular],
"Provider": Object {
"$$typeof": Symbol(react.provider),
"_context": [Circular],
},
"_calculateChangedBits": null,
"_currentValue": undefined,
"_currentValue2": undefined,
"_threadCount": 0,
},
"ThemeContext": Object {
"$$typeof": Symbol(react.context),
"Consumer": [Circular],
"Provider": Object {
"$$typeof": Symbol(react.provider),
"_context": [Circular],
},
"_calculateChangedBits": null,
"_currentValue": undefined,
"_currentValue2": undefined,
"_threadCount": 0,
},
"ThemeProvider": [Function],
"__PRIVATE__": Object {
"StyleSheet": [Function],
"masterSheet": e {
"gs": Object {},
"names": Map {},
"options": Object {
"isServer": true,
"useCSSOMInjection": true,
},
},
},
"createGlobalStyle": [Function],
"css": [Function],
"default": [Function],
"isStyledComponent": [Function],
"keyframes": [Function],
"useTheme": [Function],
"version": "5.2.1",
"withTheme": [Function],
},
},
"global-commonjs2-single-export": Object {
"foo": "bar",
},
}
`;

exports[`loader should work with side-effects free modules #1: warnings 1`] = `Array []`;

exports[`loader should work with side-effects free modules: errors 1`] = `Array []`;

exports[`loader should work with side-effects free modules: module 1`] = `
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/side-effects-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import "styled-components";
import myGlobalThis from '../../src/runtime/getGlobalThis';

const exported = myGlobalThis.styled;

export default exported;
1 change: 1 addition & 0 deletions test/helpers/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default (code) => {
// eslint-disable-next-line no-underscore-dangle
module._compile(
`
window = {};
console.log = () => {};

const result = {};
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
module: {
rules: [
{
test: /(global-.+|rx\.all)\.js/i,
test: /(global-.+|rx\.all|styled-components.+)\.js/i,
rules: [
{
loader: path.resolve(__dirname, "../../src"),
Expand Down
19 changes: 19 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,25 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should work with side-effects free modules #1", async () => {
const compiler = getCompiler(
"side-effects-1.js",
{
exposes: "styled",
},
{
mode: "production",
}
);
const stats = await compile(compiler);

expect(
execute(readAsset("main.bundle.js", compiler, stats))
).toMatchSnapshot("result");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it("should throw an error on invalid exposed value", async () => {
const compiler = getCompiler("simple-commonjs2-single-export.js", {
exposes: "myGlobal foo bar baz",
Expand Down