Skip to content

Commit 6271fc4

Browse files
authored
fix: don't strip loader "ref" from import string
1 parent 09047cf commit 6271fc4

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function contextify(context, request) {
9595
return request
9696
.split('!')
9797
.map((r) => {
98-
const splitPath = r.split('?', 2);
98+
const splitPath = r.split('?');
9999

100100
if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
101101
splitPath[0] = path.win32.relative(context, splitPath[0]);

test/__snapshots__/loader.test.js.snap

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,35 @@ Object {
349349

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

352+
exports[`loader should work inline 1 with config loader options: errors 1`] = `Array []`;
353+
354+
exports[`loader should work inline 1 with config loader options: module 1`] = `
355+
"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!../../node_modules/babel-loader/lib/index.js??{{config-reference}}!./global-commonjs2-single-export.js\\");
356+
var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\");
357+
var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;
358+
if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___;
359+
else throw new Error('[exposes-loader] The \\"myGlobal\\" value exists in the global scope, it may not be safe to overwrite it, use the \\"override\\" option')
360+
module.exports = ___EXPOSE_LOADER_IMPORT___;
361+
"
362+
`;
363+
364+
exports[`loader should work inline 1 with config loader options: result 1`] = `
365+
Object {
366+
"ExposeLoader": Object {
367+
"default": Object {
368+
"exposedEqualsGlobal": true,
369+
"exposedEqualsImported": true,
370+
"importedEqualsGlobal": true,
371+
},
372+
},
373+
"myGlobal": Object {
374+
"foo": "bar",
375+
},
376+
}
377+
`;
378+
379+
exports[`loader should work inline 1 with config loader options: warnings 1`] = `Array []`;
380+
352381
exports[`loader should work inline 1 without extension: errors 1`] = `Array []`;
353382

354383
exports[`loader should work inline 1 without extension: module 1`] = `
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import exposed from '../../src/cjs.js?exposes=myGlobal!./global-commonjs2-single-export.js';
2+
3+
import imported from './global-commonjs2-single-export.js'
4+
5+
export default {
6+
exposedEqualsGlobal: exposed === myGlobal,
7+
importedEqualsGlobal: imported === myGlobal,
8+
exposedEqualsImported: exposed === imported,
9+
}

test/loader.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,48 @@ describe('loader', () => {
268268
expect(getWarnings(stats)).toMatchSnapshot('warnings');
269269
});
270270

271+
it('should work inline 1 with config loader options', async () => {
272+
const compiler = getCompiler(
273+
'inline-import-equality.js',
274+
{},
275+
{
276+
devtool: 'source-map',
277+
module: {
278+
rules: [
279+
{
280+
test: /.*global-commonjs2-single-export\.js/i,
281+
use: [
282+
{
283+
loader: 'babel-loader',
284+
options: {
285+
presets: ['@babel/preset-env'],
286+
},
287+
},
288+
],
289+
},
290+
],
291+
},
292+
}
293+
);
294+
const stats = await compile(compiler);
295+
296+
const isWebpack5 = webpack.version[0] === '5';
297+
const refRegexp = isWebpack5 ? /\?ruleSet\[\d+\].*!/ : /\?ref--[0-9-]+!/;
298+
299+
expect(
300+
getModuleSource(
301+
'./global-commonjs2-single-export-exposed.js',
302+
stats
303+
).replace(refRegexp, '?{{config-reference}}!')
304+
).toMatchSnapshot('module');
305+
expect(
306+
execute(readAsset('main.bundle.js', compiler, stats))
307+
).toMatchSnapshot('result');
308+
expect(readAsset('main.bundle.js.map', compiler, stats)).toBeDefined();
309+
expect(getErrors(stats)).toMatchSnapshot('errors');
310+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
311+
});
312+
271313
it('should work inline 1 without extension', async () => {
272314
const compiler = getCompiler(
273315
'inline-import-1.js',
@@ -348,7 +390,7 @@ describe('loader', () => {
348390
).toMatchSnapshot('module');
349391
expect(module.hash).toBe(
350392
isWebpack5
351-
? 'ca629829313dd6de9e673c154aa723c4'
393+
? '53b5c93a2ac82d2e55921ab5bcf9649e'
352394
: 'c3e516476bee11406ecca2a29b66c743'
353395
);
354396
expect(getErrors(stats)).toMatchSnapshot('errors');

0 commit comments

Comments
 (0)