Skip to content

Commit c7557f6

Browse files
refactor: code
1 parent fe0e0e8 commit c7557f6

File tree

6 files changed

+115
-21
lines changed

6 files changed

+115
-21
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import validateOptions from 'schema-utils';
1818

1919
import schema from './options.json';
2020

21-
import getExposes from './utils';
21+
import { modifyUserRequest, getExposes } from './utils';
2222

2323
export default function loader(content, sourceMap) {
2424
const options = getOptions(this);
@@ -52,7 +52,7 @@ export default function loader(content, sourceMap) {
5252
* - https://github.com/webpack-contrib/expose-loader/issues/55
5353
* - https://github.com/webpack-contrib/expose-loader/issues/49
5454
*/
55-
this._module.userRequest = `${this._module.userRequest}-exposed`;
55+
this._module.userRequest = modifyUserRequest(this._module.userRequest);
5656

5757
const callback = this.async();
5858

src/utils.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
13
function splitCommand(command) {
24
const result = command
35
.split('|')
@@ -53,4 +55,20 @@ function getExposes(items) {
5355
return result;
5456
}
5557

56-
export default getExposes;
58+
function modifyUserRequest(request) {
59+
const splittedRequest = request.split('!');
60+
const lastPartRequest = splittedRequest.pop().split('?', 2);
61+
const pathObject = path.parse(lastPartRequest[0]);
62+
63+
pathObject.base = `${path.basename(pathObject.base, pathObject.ext)}-exposed${
64+
pathObject.ext
65+
}`;
66+
67+
lastPartRequest[0] = path.format(pathObject);
68+
69+
splittedRequest.push(lastPartRequest.join('?'));
70+
71+
return splittedRequest.join('!');
72+
}
73+
74+
export { getExposes, modifyUserRequest };

test/__snapshots__/loader.test.js.snap

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,37 @@ Object {
150150

151151
exports[`loader should work from esModule export: warnings 1`] = `Array []`;
152152

153+
exports[`loader should work inline 1 without extension: errors 1`] = `Array []`;
154+
155+
exports[`loader should work inline 1 without extension: module 1`] = `
156+
"function getAddress() {
157+
return {
158+
city: 'Tokyo'
159+
};
160+
}
161+
162+
const address = getAddress();
163+
const myExports = address === null || address === void 0 ? void 0 : address.city;
164+
export default myExports;
165+
import * as ___EXPOSE_LOADER_IMPORT___ from \\"-!./../../node_modules/babel-loader/lib/index.js!./custom\\";
166+
var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\");
167+
var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___();
168+
var ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___ = ___EXPOSE_LOADER_IMPORT___.default
169+
___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT_MODULE_LOCAL_NAME___;
170+
"
171+
`;
172+
173+
exports[`loader should work inline 1 without extension: result 1`] = `
174+
Object {
175+
"ExposeLoader": Object {
176+
"default": "Tokyo",
177+
},
178+
"myGlobal": "Tokyo",
179+
}
180+
`;
181+
182+
exports[`loader should work inline 1 without extension: warnings 1`] = `Array []`;
183+
153184
exports[`loader should work inline 1: errors 1`] = `Array []`;
154185

155186
exports[`loader should work inline 1: module 1`] = `

test/fixtures/custom

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function getAddress() {
2+
return {city: 'Tokyo'};
3+
}
4+
5+
const address = getAddress();
6+
7+
const myExports = address?.city;
8+
9+
export default myExports;

test/fixtures/inline-import-1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import myExports from '../../src/cjs.js?exposes=myGlobal|default!./custom?foo=bar';
2+
3+
export default myExports;

test/loader.test.js

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('loader', () => {
2222
const stats = await compile(compiler);
2323

2424
expect(
25-
getModuleSource('./global-commonjs2-single-export.js-exposed', stats)
25+
getModuleSource('./global-commonjs2-single-export-exposed.js', stats)
2626
).toMatchSnapshot('module');
2727
expect(
2828
execute(readAsset('main.bundle.js', compiler, stats))
@@ -39,7 +39,7 @@ describe('loader', () => {
3939
const stats = await compile(compiler);
4040

4141
expect(
42-
getModuleSource('./global-commonjs2-multiple-exports.js-exposed', stats)
42+
getModuleSource('./global-commonjs2-multiple-exports-exposed.js', stats)
4343
).toMatchSnapshot('module');
4444
expect(
4545
execute(readAsset('main.bundle.js', compiler, stats))
@@ -56,7 +56,7 @@ describe('loader', () => {
5656
const stats = await compile(compiler);
5757

5858
expect(
59-
getModuleSource('./global-commonjs2-single-export.js-exposed', stats)
59+
getModuleSource('./global-commonjs2-single-export-exposed.js', stats)
6060
).toMatchSnapshot('module');
6161
expect(
6262
execute(readAsset('main.bundle.js', compiler, stats))
@@ -73,7 +73,7 @@ describe('loader', () => {
7373
const stats = await compile(compiler);
7474

7575
expect(
76-
getModuleSource('./global-commonjs2-multiple-exports.js-exposed', stats)
76+
getModuleSource('./global-commonjs2-multiple-exports-exposed.js', stats)
7777
).toMatchSnapshot('module');
7878
expect(
7979
execute(readAsset('main.bundle.js', compiler, stats))
@@ -90,7 +90,7 @@ describe('loader', () => {
9090
const stats = await compile(compiler);
9191

9292
expect(
93-
getModuleSource('./global-commonjs2-single-export.js-exposed', stats)
93+
getModuleSource('./global-commonjs2-single-export-exposed.js', stats)
9494
).toMatchSnapshot('module');
9595
expect(
9696
execute(readAsset('main.bundle.js', compiler, stats))
@@ -107,7 +107,7 @@ describe('loader', () => {
107107
const stats = await compile(compiler);
108108

109109
expect(
110-
getModuleSource('./global-commonjs2-single-export.js-exposed', stats)
110+
getModuleSource('./global-commonjs2-single-export-exposed.js', stats)
111111
).toMatchSnapshot('module');
112112
expect(
113113
execute(readAsset('main.bundle.js', compiler, stats))
@@ -123,7 +123,7 @@ describe('loader', () => {
123123
const stats = await compile(compiler);
124124

125125
expect(
126-
getModuleSource('./global-module-default-export.js-exposed', stats)
126+
getModuleSource('./global-module-default-export-exposed.js', stats)
127127
).toMatchSnapshot('module');
128128
expect(
129129
execute(readAsset('main.bundle.js', compiler, stats))
@@ -145,7 +145,7 @@ describe('loader', () => {
145145
const stats = await compile(compiler);
146146

147147
expect(
148-
getModuleSource('./global-module-named-exports.js-exposed', stats)
148+
getModuleSource('./global-module-named-exports-exposed.js', stats)
149149
).toMatchSnapshot('module');
150150
expect(
151151
execute(readAsset('main.bundle.js', compiler, stats))
@@ -167,7 +167,7 @@ describe('loader', () => {
167167
const stats = await compile(compiler);
168168

169169
expect(
170-
getModuleSource('./global-module-named-exports.js-exposed', stats)
170+
getModuleSource('./global-module-named-exports-exposed.js', stats)
171171
).toMatchSnapshot('module');
172172
expect(
173173
execute(readAsset('main.bundle.js', compiler, stats))
@@ -186,7 +186,7 @@ describe('loader', () => {
186186
const stats = await compile(compiler);
187187

188188
expect(
189-
getModuleSource('./global-module-named-exports.js-exposed', stats)
189+
getModuleSource('./global-module-named-exports-exposed.js', stats)
190190
).toMatchSnapshot('module');
191191
expect(
192192
execute(readAsset('main.bundle.js', compiler, stats))
@@ -212,7 +212,7 @@ describe('loader', () => {
212212
const stats = await compile(compiler);
213213

214214
expect(
215-
getModuleSource('./global-module-named-exports.js-exposed', stats)
215+
getModuleSource('./global-module-named-exports-exposed.js', stats)
216216
).toMatchSnapshot('module');
217217
expect(
218218
execute(readAsset('main.bundle.js', compiler, stats))
@@ -259,7 +259,7 @@ describe('loader', () => {
259259
const stats = await compile(compiler);
260260

261261
expect(
262-
getModuleSource('./custom.js?foo=bar-exposed', stats)
262+
getModuleSource('./custom-exposed.js?foo=bar', stats)
263263
).toMatchSnapshot('module');
264264
expect(
265265
execute(readAsset('main.bundle.js', compiler, stats))
@@ -269,6 +269,39 @@ describe('loader', () => {
269269
expect(getWarnings(stats)).toMatchSnapshot('warnings');
270270
});
271271

272+
it('should work inline 1 without extension', async () => {
273+
const compiler = getCompiler(
274+
'inline-import-1.js',
275+
{},
276+
{
277+
devtool: 'source-map',
278+
module: {
279+
rules: [
280+
{
281+
test: /.*custom/i,
282+
use: [
283+
{
284+
loader: 'babel-loader',
285+
},
286+
],
287+
},
288+
],
289+
},
290+
}
291+
);
292+
const stats = await compile(compiler);
293+
294+
expect(getModuleSource('./custom-exposed?foo=bar', stats)).toMatchSnapshot(
295+
'module'
296+
);
297+
expect(
298+
execute(readAsset('main.bundle.js', compiler, stats))
299+
).toMatchSnapshot('result');
300+
expect(readAsset('main.bundle.js.map', compiler, stats)).toBeDefined();
301+
expect(getErrors(stats)).toMatchSnapshot('errors');
302+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
303+
});
304+
272305
it('should work inline 2', async () => {
273306
const compiler = getCompiler(
274307
'inline-import-2.js',
@@ -280,7 +313,7 @@ describe('loader', () => {
280313
const stats = await compile(compiler);
281314

282315
expect(
283-
getModuleSource('./simple-commonjs2-single-export.js-exposed', stats)
316+
getModuleSource('./simple-commonjs2-single-export-exposed.js', stats)
284317
).toMatchSnapshot('module');
285318
expect(
286319
execute(readAsset('main.bundle.js', compiler, stats))
@@ -307,17 +340,17 @@ describe('loader', () => {
307340
);
308341
const stats = await compile(compiler);
309342
const module = stats.compilation.modules.find((m) =>
310-
m.id.endsWith('./simple-commonjs2-single-export.js-exposed')
343+
m.id.endsWith('./simple-commonjs2-single-export-exposed.js')
311344
);
312345
const isWebpack5 = webpack.version[0] === '5';
313346

314347
expect(
315-
getModuleSource('./simple-commonjs2-single-export.js-exposed', stats)
348+
getModuleSource('./simple-commonjs2-single-export-exposed.js', stats)
316349
).toMatchSnapshot('module');
317350
expect(module.hash).toBe(
318351
isWebpack5
319-
? 'c440ca2d9d70459fecf24e8109d10515'
320-
: 'e8401602c3b42c066e582bcd1f72cb36'
352+
? '60f98cee3e481afa035cc5b317197b92'
353+
: '18981954a65740f4957667890c5b154b'
321354
);
322355
expect(getErrors(stats)).toMatchSnapshot('errors');
323356
expect(getWarnings(stats)).toMatchSnapshot('warnings');
@@ -351,7 +384,7 @@ describe('loader', () => {
351384
const stats = await compile(compiler);
352385

353386
expect(
354-
getModuleSource('./global-commonjs2-single-export.js-exposed', stats)
387+
getModuleSource('./global-commonjs2-single-export-exposed.js', stats)
355388
).toMatchSnapshot('module');
356389
expect(
357390
execute(readAsset('main.bundle.js', compiler, stats))

0 commit comments

Comments
 (0)