Skip to content

Commit 5173c89

Browse files
refactor: allow object syntax for exposes
1 parent 19a54b7 commit 5173c89

17 files changed

+2152
-1812
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@webpack-contrib/defaults": "^6.3.0",
5555
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5656
"babel-jest": "^26.0.1",
57+
"babel-loader": "^8.1.0",
5758
"commitlint-azure-pipelines-cli": "^1.0.3",
5859
"cross-env": "^7.0.2",
5960
"del": "^5.1.0",

src/index.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import path from 'path';
7+
import url from 'url';
78

89
import {
910
getOptions,
@@ -31,10 +32,18 @@ export default function loader(content, sourceMap) {
3132
// Change the request from an /abolute/path.js to a relative ./path.js
3233
// This prevents [chunkhash] values from changing when running webpack
3334
// builds in different directories.
34-
const newRequestPath = `./${path.relative(
35-
this.context,
36-
getRemainingRequest(this)
37-
)}`;
35+
const remainingRequest = getRemainingRequest(this).split('!');
36+
const remainingRequests =
37+
typeof remainingRequest === 'string'
38+
? [remainingRequest]
39+
: remainingRequest;
40+
41+
const newRequestPath = remainingRequests
42+
.map(
43+
(currentUrl) =>
44+
`./${path.relative(this.context, url.parse(currentUrl).pathname)}`
45+
)
46+
.join('!');
3847

3948
/*
4049
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
@@ -52,8 +61,7 @@ export default function loader(content, sourceMap) {
5261
try {
5362
exposes = getExposes(options.exposes);
5463
} catch (error) {
55-
this.emitError(error);
56-
callback(null, content, sourceMap);
64+
callback(error);
5765

5866
return;
5967
}
@@ -68,9 +76,8 @@ export default function loader(content, sourceMap) {
6876
code += `var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___();\n`;
6977

7078
for (const expose of exposes) {
71-
const childProperties = expose.globalName.split('.');
72-
const { length } = childProperties;
73-
const { packageName } = expose;
79+
const { globalName, packageName } = expose;
80+
const { length } = globalName;
7481

7582
if (typeof packageName !== 'undefined') {
7683
code += `var ___EXPOSE_LOADER_IMPORT_NAMED___ = ___EXPOSE_LOADER_IMPORT___.${packageName}\n`;
@@ -83,7 +90,7 @@ export default function loader(content, sourceMap) {
8390
code += `if (!${propertyString}) ${propertyString} = {};\n`;
8491
}
8592

86-
propertyString += `[${JSON.stringify(childProperties[i])}]`;
93+
propertyString += `[${JSON.stringify(globalName[i])}]`;
8794
}
8895

8996
code +=

src/options.json

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
11
{
2+
"definitions": {
3+
"ObjectPattern": {
4+
"type": "object",
5+
"additionalProperties": false,
6+
"properties": {
7+
"globalName": {
8+
"anyOf": [
9+
{
10+
"type": "string",
11+
"minLength": 1
12+
},
13+
{
14+
"type": "array",
15+
"items": {
16+
"type": "string",
17+
"minLength": 1
18+
},
19+
"minItems": 1
20+
}
21+
]
22+
},
23+
"packageName": {
24+
"type": "string",
25+
"minLength": 1
26+
}
27+
},
28+
"anyOf": [{ "required": ["globalName"] }]
29+
}
30+
},
231
"type": "object",
332
"properties": {
433
"exposes": {
@@ -7,16 +36,27 @@
736
"type": "string",
837
"minLength": 1
938
},
39+
{
40+
"$ref": "#/definitions/ObjectPattern"
41+
},
1042
{
1143
"type": "array",
1244
"items": {
13-
"type": "string",
14-
"minLength": 1
45+
"anyOf": [
46+
{
47+
"type": "string",
48+
"minLength": 1
49+
},
50+
{
51+
"$ref": "#/definitions/ObjectPattern"
52+
}
53+
]
1554
},
1655
"minItems": 1
1756
}
1857
]
1958
}
2059
},
60+
"anyOf": [{ "required": ["exposes"] }],
2161
"additionalProperties": false
2262
}

src/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ function resolveExposes(item) {
1212
globalName: splittedItem[0],
1313
packageName: splittedItem[1],
1414
};
15+
} else {
16+
result = item;
1517
}
1618

17-
return result;
19+
const nestedGlobalName =
20+
typeof result.globalName === 'string'
21+
? result.globalName.split('.')
22+
: result.globalName;
23+
24+
return { ...result, globalName: nestedGlobalName };
1825
}
1926

2027
function getExposes(items) {
2128
let result = [];
2229

23-
if (typeof imports === 'string') {
30+
if (typeof items === 'string') {
2431
result.push(resolveExposes(items));
2532
} else {
2633
result = [].concat(items).map((item) => resolveExposes(item));

0 commit comments

Comments
 (0)