Skip to content

Commit

Permalink
fix: small perf improvement (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Oct 21, 2021
1 parent a408954 commit 6aeabd8
Show file tree
Hide file tree
Showing 8 changed files with 4,734 additions and 8,974 deletions.
1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

13,621 changes: 4,675 additions & 8,946 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@
"@babel/cli": "^7.14.5",
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@commitlint/cli": "^13.2.1",
"@commitlint/config-conventional": "^13.2.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^27.0.6",
"babel-loader": "^8.2.2",
"cross-env": "^7.0.3",
"del": "^6.0.0",
"del-cli": "^3.0.1",
"eslint": "^7.28.0",
"del-cli": "^4.0.1",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"husky": "^6.0.0",
"husky": "^7.0.0",
"jest": "^27.0.6",
"lint-staged": "^11.0.0",
"memfs": "^3.2.2",
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export default function loader() {
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, this.remainingRequest);
// Change the request from an /absolute/path.js to a relative ./path.js.
// This prevents `[chunkhash]` values from changing when running webpack builds in different directories.
const newRequest = contextify(this, this.context, this.remainingRequest);
const stringifiedNewRequest = stringifyRequest(this, `-!${newRequest}`);

let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
Expand Down
20 changes: 19 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ function getExposes(items) {
return result;
}

function contextify(context, request) {
// TODO simplify for the next major release
function contextify(loaderContext, context, request) {
if (
typeof loaderContext.utils !== "undefined" &&
typeof loaderContext.utils.contextify === "function"
) {
loaderContext.utils.contextify(loaderContext.context, request);
}

return request
.split("!")
.map((r) => {
Expand Down Expand Up @@ -124,7 +132,17 @@ function isAbsolutePath(str) {
return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
}

// TODO simplify for the next major release
function stringifyRequest(loaderContext, request) {
if (
typeof loaderContext.utils !== "undefined" &&
typeof loaderContext.utils.contextify === "function"
) {
return JSON.stringify(
loaderContext.utils.contextify(loaderContext.context, request)
);
}

const splitted = request.split("!");
const context =
loaderContext.context ||
Expand Down
5 changes: 2 additions & 3 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ module.exports = ___EXPOSE_LOADER_IMPORT___;
"
`;

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

exports[`loader should throw an error on existing nested value in the global object: warnings 1`] = `Array []`;

exports[`loader should throw an error on existing value in the global object in the "development" mode: errors 1`] = `Array []`;
Expand Down Expand Up @@ -1072,6 +1070,7 @@ Object {
"isServer": true,
"useCSSOMInjection": true,
},
"server": false,
},
},
"createGlobalStyle": [Function],
Expand All @@ -1080,7 +1079,7 @@ Object {
"isStyledComponent": [Function],
"keyframes": [Function],
"useTheme": [Function],
"version": "5.3.0",
"version": "5.3.3",
"withTheme": [Function],
},
},
Expand Down
43 changes: 29 additions & 14 deletions test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,38 @@

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

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

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

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

exports[`validate options should throw an error on the "exposes" option with "{"globalName":true}" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options.exposes should be one of these:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes
Details:
* options.exposes.globalName should be one of these:
non-empty string | [non-empty string, ...] (should not have fewer than 1 item)
-> The name in the global object.
-> Read more at https://github.com/webpack-contrib/expose-loader#globalname
Details:
* options.exposes.globalName should be a non-empty string.
* options.exposes.globalName should be an array:
Expand All @@ -46,21 +49,24 @@ exports[`validate options should throw an error on the "exposes" option with "{"
exports[`validate options should throw an error on the "exposes" option with "{"override":"test"}" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options.exposes.override should be a boolean.
-> Configure loader to override the existing value in the global object."
-> Configure loader to override the existing value in the global object.
-> Read more at https://github.com/webpack-contrib/expose-loader#override"
`;
exports[`validate options should throw an error on the "exposes" option with "{}" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options.exposes misses the property 'globalName'. Should be:
non-empty string | [non-empty string, ...] (should not have fewer than 1 item)
-> The name in the global object."
-> The name in the global object.
-> Read more at https://github.com/webpack-contrib/expose-loader#globalname"
`;
exports[`validate options should throw an error on the "exposes" option with "false" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options.exposes should be one of these:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes
Details:
* options.exposes should be a non-empty string.
* options.exposes should be an object:
Expand All @@ -74,6 +80,7 @@ exports[`validate options should throw an error on the "exposes" option with "tr
- options.exposes should be one of these:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes
Details:
* options.exposes should be a non-empty string.
* options.exposes should be an object:
Expand All @@ -86,54 +93,62 @@ exports[`validate options should throw an error on the "unknown" option with "/t
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
"Invalid options object. Expose Loader has been initialized using an options object that does not match the API schema.
- options misses the property 'exposes'. Should be:
non-empty string | object { globalName, moduleLocalName?, override? } | [non-empty string | object { globalName, moduleLocalName?, override? }, ...] (should not have fewer than 1 item)
-> List of exposes."
-> List of exposes.
-> Read more at https://github.com/webpack-contrib/expose-loader#exposes"
`;
2 changes: 1 addition & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ describe("loader", () => {
).toMatchSnapshot("module");
expect(() =>
execute(readAsset("main.bundle.js", compiler, stats))
).toThrowErrorMatchingSnapshot("runtime error");
).toThrowError(/Cannot read/);
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});
Expand Down

0 comments on commit 6aeabd8

Please sign in to comment.