Skip to content

Commit 04c1fa7

Browse files
refactor: code
1 parent eb5dd22 commit 04c1fa7

15 files changed

+378
-108
lines changed

.eslintrc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
module.exports = {
22
root: true,
33
extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'],
4+
overrides: [
5+
{
6+
globals: {
7+
globalThis: 'readonly',
8+
},
9+
env: {
10+
browser: true,
11+
node: true,
12+
},
13+
files: ['**/runtime/**/*.js'],
14+
},
15+
],
416
};

src/index.js

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55

6-
import path from 'path';
7-
import url from 'url';
8-
96
import {
107
getOptions,
118
stringifyRequest,
@@ -17,7 +14,7 @@ import validateOptions from 'schema-utils';
1714

1815
import schema from './options.json';
1916

20-
import { modifyUserRequest, getExposes } from './utils';
17+
import { getExposes, contextify, getNewUserRequest } from './utils';
2118

2219
export default function loader() {
2320
const options = getOptions(this);
@@ -27,31 +24,14 @@ export default function loader() {
2724
baseDataPath: 'options',
2825
});
2926

30-
// Change the request from an /abolute/path.js to a relative ./path.js
31-
// This prevents [chunkhash] values from changing when running webpack
32-
// builds in different directories.
33-
const newRequestPath = getRemainingRequest(this)
34-
.split('!')
35-
.map((currentUrl) => {
36-
const result = `./${path.relative(
37-
this.context,
38-
url.parse(currentUrl).pathname
39-
)}`;
40-
41-
return process.platform === 'win32'
42-
? result.split(path.sep).join('/')
43-
: result;
44-
})
45-
.join('!');
46-
4727
/*
4828
* Workaround until module.libIdent() in webpack/webpack handles this correctly.
4929
*
5030
* fixes:
5131
* - https://github.com/webpack-contrib/expose-loader/issues/55
5232
* - https://github.com/webpack-contrib/expose-loader/issues/49
5333
*/
54-
this._module.userRequest = modifyUserRequest(this._module.userRequest);
34+
this._module.userRequest = getNewUserRequest(this._module.userRequest);
5535

5636
const callback = this.async();
5737

@@ -65,21 +45,12 @@ export default function loader() {
6545
return;
6646
}
6747

68-
const isModule = options.type !== 'commonjs';
48+
// Change the request from an /abolute/path.js to a relative ./path.js.
49+
// This prevents [chunkhash] values from changing when running webpack builds in different directories.
50+
const newRequest = contextify(this.rootContext, getRemainingRequest(this));
51+
const stringifiedNewRequest = stringifyRequest(this, `-!${newRequest}`);
6952

70-
let code = '';
71-
72-
if (isModule) {
73-
code = `import * as ___EXPOSE_LOADER_IMPORT___ from ${stringifyRequest(
74-
this,
75-
`-!${newRequestPath}`
76-
)};\n`;
77-
} else {
78-
code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifyRequest(
79-
this,
80-
`-!${newRequestPath}`
81-
)});\n`;
82-
}
53+
let code = `var ___EXPOSE_LOADER_IMPORT___ = require(${stringifiedNewRequest});\n`;
8354

8455
code += `var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(${stringifyRequest(
8556
this,
@@ -89,9 +60,9 @@ export default function loader() {
8960

9061
for (const expose of exposes) {
9162
const { globalName, moduleLocalName } = expose;
92-
const globalNameInterpolated = globalName.map((item) => {
93-
return interpolateName(this, item, {});
94-
});
63+
const globalNameInterpolated = globalName.map((item) =>
64+
interpolateName(this, item, {})
65+
);
9566

9667
if (typeof moduleLocalName !== 'undefined') {
9768
code += `var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.${moduleLocalName}\n`;
@@ -113,15 +84,7 @@ export default function loader() {
11384
: `${propertyString} = ___EXPOSE_LOADER_IMPORT___;\n`;
11485
}
11586

116-
if (isModule) {
117-
code += `export { default } from ${stringifyRequest(
118-
this,
119-
`-!${newRequestPath}`
120-
)};\n`;
121-
code += `export * from ${stringifyRequest(this, `-!${newRequestPath}`)};\n`;
122-
} else {
123-
code += `module.exports = ___EXPOSE_LOADER_IMPORT___;`;
124-
}
87+
code += `module.exports = ___EXPOSE_LOADER_IMPORT___;\n`;
12588

12689
callback(null, code);
12790
}

src/options.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
},
3131
"type": "object",
3232
"properties": {
33-
"type": {
34-
"enum": ["module", "commonjs"]
35-
},
3633
"exposes": {
3734
"anyOf": [
3835
{

src/runtime/getGlobalThis.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
module.exports = function getGlobalThis() {
22
if (typeof globalThis !== 'undefined') {
3-
// eslint-disable-next-line no-undef
43
return globalThis;
54
}
65

76
if (typeof self !== 'undefined') {
8-
// eslint-disable-next-line no-undef
97
return self;
108
}
119

1210
if (typeof window !== 'undefined') {
13-
// eslint-disable-next-line no-undef
1411
return window;
1512
}
1613

src/utils.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import path from 'path';
22

3+
function getNewUserRequest(request) {
4+
const splittedRequest = request.split('!');
5+
const lastPartRequest = splittedRequest.pop().split('?', 2);
6+
const pathObject = path.parse(lastPartRequest[0]);
7+
8+
pathObject.base = `${path.basename(pathObject.base, pathObject.ext)}-exposed${
9+
pathObject.ext
10+
}`;
11+
12+
lastPartRequest[0] = path.format(pathObject);
13+
14+
splittedRequest.push(lastPartRequest.join('?'));
15+
16+
return splittedRequest.join('!');
17+
}
18+
319
function splitCommand(command) {
420
const result = command
521
.split('|')
@@ -55,20 +71,31 @@ function getExposes(items) {
5571
return result;
5672
}
5773

58-
function modifyUserRequest(request) {
59-
const splittedRequest = request.split('!');
60-
const lastPartRequest = splittedRequest.pop().split('?', 2);
61-
const pathObject = path.parse(lastPartRequest[0]);
74+
function contextify(context, request) {
75+
return request
76+
.split('!')
77+
.map((r) => {
78+
const splitPath = r.split('?', 2);
6279

63-
pathObject.base = `${path.basename(pathObject.base, pathObject.ext)}-exposed${
64-
pathObject.ext
65-
}`;
80+
if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
81+
splitPath[0] = path.win32.relative(context, splitPath[0]);
6682

67-
lastPartRequest[0] = path.format(pathObject);
83+
if (!/^[a-zA-Z]:\\/.test(splitPath[0])) {
84+
splitPath[0] = splitPath[0].replace(/\\/g, '/');
85+
}
86+
}
6887

69-
splittedRequest.push(lastPartRequest.join('?'));
88+
if (/^\//.test(splitPath[0])) {
89+
splitPath[0] = path.posix.relative(context, splitPath[0]);
90+
}
7091

71-
return splittedRequest.join('!');
92+
if (!/^(\.\.\/|\/|[a-zA-Z]:\\)/.test(splitPath[0])) {
93+
splitPath[0] = `./${splitPath[0]}`;
94+
}
95+
96+
return splitPath.join('?');
97+
})
98+
.join('!');
7299
}
73100

74-
export { getExposes, modifyUserRequest };
101+
export { getNewUserRequest, getExposes, contextify };

0 commit comments

Comments
 (0)