Skip to content

Commit 6de72f1

Browse files
refactor: dropped webpack@4 (#126)
BREAKING CHANGE: minimum supported `webpack` version is `5`
1 parent d893f28 commit 6de72f1

File tree

14 files changed

+937
-1270
lines changed

14 files changed

+937
-1270
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
matrix:
5757
os: [ubuntu-latest, windows-latest, macos-latest]
5858
node-version: [10.x, 12.x, 14.x]
59-
webpack-version: [4, latest]
59+
webpack-version: [latest]
6060

6161
runs-on: ${{ matrix.os }}
6262

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The documentation and syntax examples can be read [here](#syntax).
3636
> `%20` is space in a query string, because you can't use spaces in URLs
3737
3838
```js
39-
import $ from "expose-loader?exposes[]=$&exposes[]=jQuery!jquery";
39+
import $ from "expose-loader?exposes=$,jQuery!jquery";
4040
//
4141
// Adds the `jquery` to the global object under the names `$` and `jQuery`
4242
```
@@ -51,7 +51,7 @@ import { concat } from "expose-loader?exposes=_.concat!lodash/concat";
5151
import {
5252
map,
5353
reduce,
54-
} from "expose-loader?exposes[]=_.map|map&exposes[]=_.reduce|reduce!underscore";
54+
} from "expose-loader?exposes=_.map|map,_.reduce|reduce!underscore";
5555
//
5656
// Adds the `map` and `reduce` method from `underscore` to the global object under the name `_.map` and `_.reduce`
5757
```

package-lock.json

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

package.json

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,36 @@
3838
"dist"
3939
],
4040
"peerDependencies": {
41-
"webpack": "^4.0.0 || ^5.0.0"
42-
},
43-
"dependencies": {
44-
"loader-utils": "^2.0.0",
45-
"schema-utils": "^3.0.0"
41+
"webpack": "^5.0.0"
4642
},
4743
"devDependencies": {
48-
"@babel/cli": "^7.12.8",
49-
"@babel/core": "^7.12.9",
50-
"@babel/preset-env": "^7.12.7",
44+
"@babel/cli": "^7.12.10",
45+
"@babel/core": "^7.12.10",
46+
"@babel/preset-env": "^7.12.11",
5147
"@commitlint/cli": "^11.0.0",
5248
"@commitlint/config-conventional": "^11.0.0",
5349
"@webpack-contrib/defaults": "^6.3.0",
5450
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5551
"babel-jest": "^26.6.3",
56-
"babel-loader": "^8.2.1",
57-
"cross-env": "^7.0.2",
52+
"babel-loader": "^8.2.2",
53+
"cross-env": "^7.0.3",
5854
"del": "^6.0.0",
5955
"del-cli": "^3.0.1",
60-
"eslint": "^7.14.0",
61-
"eslint-config-prettier": "^6.15.0",
56+
"eslint": "^7.19.0",
57+
"eslint-config-prettier": "^7.2.0",
6258
"eslint-plugin-import": "^2.22.1",
6359
"eslint-plugin-prettier": "^3.1.4",
64-
"husky": "^4.3.0",
60+
"husky": "^4.3.8",
6561
"jest": "^26.6.3",
66-
"lint-staged": "^10.5.2",
62+
"lint-staged": "^10.5.3",
6763
"memfs": "^3.2.0",
6864
"npm-run-all": "^4.1.5",
69-
"prettier": "^2.2.0",
65+
"prettier": "^2.2.1",
7066
"react": "^17.0.1",
7167
"rx": "^4.1.0",
72-
"standard-version": "^9.0.0",
68+
"standard-version": "^9.1.0",
7369
"styled-components": "^5.2.1",
74-
"webpack": "^5.6.0"
70+
"webpack": "^5.19.0"
7571
},
7672
"keywords": [
7773
"webpack"

src/index.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,18 @@
33
Author Tobias Koppers @sokra
44
*/
55

6+
import schema from "./options.json";
7+
68
import {
7-
getOptions,
9+
getExposes,
10+
contextify,
11+
getNewUserRequest,
812
stringifyRequest,
9-
getRemainingRequest,
1013
interpolateName,
11-
} from "loader-utils";
12-
13-
import { validate } from "schema-utils";
14-
15-
import schema from "./options.json";
16-
17-
import { getExposes, contextify, getNewUserRequest } from "./utils";
14+
} from "./utils";
1815

1916
export default function loader() {
20-
const options = getOptions(this);
21-
22-
validate(schema, options, {
23-
name: "Expose Loader",
24-
baseDataPath: "options",
25-
});
26-
17+
const options = this.getOptions(schema);
2718
const callback = this.async();
2819

2920
let exposes;
@@ -57,7 +48,7 @@ export default function loader() {
5748

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

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

src/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"title": "Expose Loader options",
23
"definitions": {
34
"ObjectPattern": {
45
"type": "object",

src/utils.js

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ function resolveExposes(item) {
8080
}
8181

8282
function getExposes(items) {
83-
let result = [];
83+
let result;
84+
const exposeItems =
85+
typeof items === "string" && items.includes(",") ? items.split(",") : items;
8486

85-
if (typeof items === "string") {
86-
result.push(resolveExposes(items));
87+
if (typeof exposeItems === "string") {
88+
result = [resolveExposes(exposeItems)];
8789
} else {
88-
result = [].concat(items).map((item) => resolveExposes(item));
90+
result = [].concat(exposeItems).map((item) => resolveExposes(item));
8991
}
9092

9193
return result;
@@ -118,4 +120,52 @@ function contextify(context, request) {
118120
.join("!");
119121
}
120122

121-
export { getNewUserRequest, getExposes, contextify };
123+
function isAbsolutePath(str) {
124+
return path.posix.isAbsolute(str) || path.win32.isAbsolute(str);
125+
}
126+
127+
function stringifyRequest(loaderContext, request) {
128+
const splitted = request.split("!");
129+
const context =
130+
loaderContext.context ||
131+
(loaderContext.options && loaderContext.options.context);
132+
133+
return JSON.stringify(
134+
splitted
135+
.map((part) => {
136+
// First, separate singlePath from query, because the query might contain paths again
137+
const splittedPart = part.match(/^(.*?)(\?.*)/);
138+
const query = splittedPart ? splittedPart[2] : "";
139+
let singlePath = splittedPart ? splittedPart[1] : part;
140+
141+
if (isAbsolutePath(singlePath) && context) {
142+
singlePath = path.relative(context, singlePath);
143+
}
144+
145+
return singlePath.replace(/\\/g, "/") + query;
146+
})
147+
.join("!")
148+
);
149+
}
150+
151+
function interpolateName(loaderContext, filename) {
152+
let basename = "file";
153+
154+
if (loaderContext.resourcePath) {
155+
const parsed = path.parse(loaderContext.resourcePath);
156+
157+
if (parsed.dir) {
158+
basename = parsed.name;
159+
}
160+
}
161+
162+
return filename.replace(/\[name\]/gi, () => basename);
163+
}
164+
165+
export {
166+
getNewUserRequest,
167+
getExposes,
168+
contextify,
169+
stringifyRequest,
170+
interpolateName,
171+
};

test/fixtures/inline-import-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import myExports from '../../src/cjs.js?exposes=myGlobal!./simple-commonjs2-single-export.js';
2-
import myExports2 from '../../src/cjs.js?exposes[]=myOtherGlobal.globalObject2|globalObject2&exposes[]=myOtherGlobal.globalObject3|globalObject3!./simple-commonjs2-multiple-export.js';
2+
import myExports2 from '../../src/cjs.js?exposes=myOtherGlobal.globalObject2|globalObject2,myOtherGlobal.globalObject3|globalObject3!./simple-commonjs2-multiple-export.js';
33

44
export { myExports, myExports2 };

test/helpers/compile.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export default (compiler) => {
2-
return new Promise((resolve, reject) => {
1+
export default (compiler) =>
2+
new Promise((resolve, reject) => {
33
compiler.run((error, stats) => {
44
if (error) {
55
return reject(error);
@@ -8,4 +8,3 @@ export default (compiler) => {
88
return resolve(stats);
99
});
1010
});
11-
};

test/helpers/getCompiler.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
3636
const compiler = webpack(fullConfig);
3737

3838
if (!config.outputFileSystem) {
39-
const outputFileSystem = createFsFromVolume(new Volume());
40-
// Todo remove when we drop webpack@4 support
41-
outputFileSystem.join = path.join.bind(path);
42-
43-
compiler.outputFileSystem = outputFileSystem;
39+
compiler.outputFileSystem = createFsFromVolume(new Volume());
4440
}
4541

4642
return compiler;

0 commit comments

Comments
 (0)