Skip to content

Commit 6aeabd8

Browse files
fix: small perf improvement (#145)
1 parent a408954 commit 6aeabd8

File tree

8 files changed

+4734
-8974
lines changed

8 files changed

+4734
-8974
lines changed

.husky/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

package-lock.json

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

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@
4343
"@babel/cli": "^7.14.5",
4444
"@babel/core": "^7.14.6",
4545
"@babel/preset-env": "^7.14.7",
46-
"@commitlint/cli": "^12.1.4",
47-
"@commitlint/config-conventional": "^12.1.4",
46+
"@commitlint/cli": "^13.2.1",
47+
"@commitlint/config-conventional": "^13.2.0",
4848
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
4949
"babel-jest": "^27.0.6",
5050
"babel-loader": "^8.2.2",
5151
"cross-env": "^7.0.3",
5252
"del": "^6.0.0",
53-
"del-cli": "^3.0.1",
54-
"eslint": "^7.28.0",
53+
"del-cli": "^4.0.1",
54+
"eslint": "^8.0.1",
5555
"eslint-config-prettier": "^8.3.0",
5656
"eslint-plugin-import": "^2.23.4",
57-
"husky": "^6.0.0",
57+
"husky": "^7.0.0",
5858
"jest": "^27.0.6",
5959
"lint-staged": "^11.0.0",
6060
"memfs": "^3.2.2",

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export default function loader() {
4646
this._module.factoryMeta.sideEffectFree = false;
4747
}
4848

49-
// Change the request from an /abolute/path.js to a relative ./path.js.
50-
// This prevents [chunkhash] values from changing when running webpack builds in different directories.
51-
const newRequest = contextify(this.context, this.remainingRequest);
49+
// Change the request from an /absolute/path.js to a relative ./path.js.
50+
// This prevents `[chunkhash]` values from changing when running webpack builds in different directories.
51+
const newRequest = contextify(this, this.context, this.remainingRequest);
5252
const stringifiedNewRequest = stringifyRequest(this, `-!${newRequest}`);
5353

5454
let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;

src/utils.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,15 @@ function getExposes(items) {
9393
return result;
9494
}
9595

96-
function contextify(context, request) {
96+
// TODO simplify for the next major release
97+
function contextify(loaderContext, context, request) {
98+
if (
99+
typeof loaderContext.utils !== "undefined" &&
100+
typeof loaderContext.utils.contextify === "function"
101+
) {
102+
loaderContext.utils.contextify(loaderContext.context, request);
103+
}
104+
97105
return request
98106
.split("!")
99107
.map((r) => {
@@ -124,7 +132,17 @@ function isAbsolutePath(str) {
124132
return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
125133
}
126134

135+
// TODO simplify for the next major release
127136
function stringifyRequest(loaderContext, request) {
137+
if (
138+
typeof loaderContext.utils !== "undefined" &&
139+
typeof loaderContext.utils.contextify === "function"
140+
) {
141+
return JSON.stringify(
142+
loaderContext.utils.contextify(loaderContext.context, request)
143+
);
144+
}
145+
128146
const splitted = request.split("!");
129147
const context =
130148
loaderContext.context ||

test/__snapshots__/loader.test.js.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ module.exports = ___EXPOSE_LOADER_IMPORT___;
116116
"
117117
`;
118118

119-
exports[`loader should throw an error on existing nested value in the global object: runtime error 1`] = `"Cannot read property 'bar' of undefined"`;
120-
121119
exports[`loader should throw an error on existing nested value in the global object: warnings 1`] = `Array []`;
122120

123121
exports[`loader should throw an error on existing value in the global object in the "development" mode: errors 1`] = `Array []`;
@@ -1072,6 +1070,7 @@ Object {
10721070
"isServer": true,
10731071
"useCSSOMInjection": true,
10741072
},
1073+
"server": false,
10751074
},
10761075
},
10771076
"createGlobalStyle": [Function],
@@ -1080,7 +1079,7 @@ Object {
10801079
"isStyledComponent": [Function],
10811080
"keyframes": [Function],
10821081
"useTheme": [Function],
1083-
"version": "5.3.0",
1082+
"version": "5.3.3",
10841083
"withTheme": [Function],
10851084
},
10861085
},

test/__snapshots__/validate-options.test.js.snap

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,38 @@
22

33
exports[`validate options should throw an error on the "exposes" option with "" value 1`] = `
44
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
5-
- options.exposes should be an non-empty string."
5+
- options.exposes should be a non-empty string."
66
`;
77

88
exports[`validate options should throw an error on the "exposes" option with "/test/" value 1`] = `
99
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
1010
- options.exposes misses the property 'globalName'. Should be:
1111
non-empty string | [non-empty string, ...] (should not have fewer than 1 item)
12-
-> The name in the global object."
12+
-> The name in the global object.
13+
-> Read more at https://github.com/webpack-contrib/expose-loader#globalname"
1314
`;
1415

1516
exports[`validate options should throw an error on the "exposes" option with "[""]" value 1`] = `
1617
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
17-
- options.exposes[0] should be an non-empty string."
18+
- options.exposes[0] should be a non-empty string."
1819
`;
1920

2021
exports[`validate options should throw an error on the "exposes" option with "[]" value 1`] = `
2122
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
22-
- options.exposes should be an non-empty array."
23+
- options.exposes should be a non-empty array."
2324
`;
2425

2526
exports[`validate options should throw an error on the "exposes" option with "{"globalName":true}" value 1`] = `
2627
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
2728
- options.exposes should be one of these:
2829
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
2930
-> List of exposes.
31+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes
3032
Details:
3133
* options.exposes.globalName should be one of these:
3234
non-empty string | [non-empty string, ...] (should not have fewer than 1 item)
3335
-> The name in the global object.
36+
-> Read more at https://github.com/webpack-contrib/expose-loader#globalname
3437
Details:
3538
* options.exposes.globalName should be a non-empty string.
3639
* options.exposes.globalName should be an array:
@@ -46,21 +49,24 @@ exports[`validate options should throw an error on the "exposes" option with "{"
4649
exports[`validate options should throw an error on the "exposes" option with "{"override":"test"}" value 1`] = `
4750
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
4851
- options.exposes.override should be a boolean.
49-
-> Configure loader to override the existing value in the global object."
52+
-> Configure loader to override the existing value in the global object.
53+
-> Read more at https://github.com/webpack-contrib/expose-loader#override"
5054
`;
5155
5256
exports[`validate options should throw an error on the "exposes" option with "{}" value 1`] = `
5357
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
5458
- options.exposes misses the property 'globalName'. Should be:
5559
non-empty string | [non-empty string, ...] (should not have fewer than 1 item)
56-
-> The name in the global object."
60+
-> The name in the global object.
61+
-> Read more at https://github.com/webpack-contrib/expose-loader#globalname"
5762
`;
5863
5964
exports[`validate options should throw an error on the "exposes" option with "false" value 1`] = `
6065
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
6166
- options.exposes should be one of these:
6267
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
6368
-> List of exposes.
69+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes
6470
Details:
6571
* options.exposes should be a non-empty string.
6672
* options.exposes should be an object:
@@ -74,6 +80,7 @@ exports[`validate options should throw an error on the "exposes" option with "tr
7480
- options.exposes should be one of these:
7581
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
7682
-> List of exposes.
83+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes
7784
Details:
7885
* options.exposes should be a non-empty string.
7986
* options.exposes should be an object:
@@ -86,54 +93,62 @@ exports[`validate options should throw an error on the "unknown" option with "/t
8693
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
8794
- options misses the property 'exposes'. Should be:
8895
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
89-
-> List of exposes."
96+
-> List of exposes.
97+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
9098
`;
9199
92100
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
93101
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
94102
- options misses the property 'exposes'. Should be:
95103
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
96-
-> List of exposes."
104+
-> List of exposes.
105+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
97106
`;
98107
99108
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
100109
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
101110
- options misses the property 'exposes'. Should be:
102111
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
103-
-> List of exposes."
112+
-> List of exposes.
113+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
104114
`;
105115
106116
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
107117
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
108118
- options misses the property 'exposes'. Should be:
109119
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
110-
-> List of exposes."
120+
-> List of exposes.
121+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
111122
`;
112123
113124
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
114125
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
115126
- options misses the property 'exposes'. Should be:
116127
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
117-
-> List of exposes."
128+
-> List of exposes.
129+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
118130
`;
119131
120132
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
121133
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
122134
- options misses the property 'exposes'. Should be:
123135
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
124-
-> List of exposes."
136+
-> List of exposes.
137+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
125138
`;
126139
127140
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
128141
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
129142
- options misses the property 'exposes'. Should be:
130143
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
131-
-> List of exposes."
144+
-> List of exposes.
145+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
132146
`;
133147
134148
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
135149
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
136150
- options misses the property 'exposes'. Should be:
137151
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
138-
-> List of exposes."
152+
-> List of exposes.
153+
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
139154
`;

test/loader.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ describe("loader", () => {
601601
).toMatchSnapshot("module");
602602
expect(() =>
603603
execute(readAsset("main.bundle.js", compiler, stats))
604-
).toThrowErrorMatchingSnapshot("runtime error");
604+
).toThrowError(/Cannot read/);
605605
expect(getErrors(stats)).toMatchSnapshot("errors");
606606
expect(getWarnings(stats)).toMatchSnapshot("warnings");
607607
});

0 commit comments

Comments
 (0)