Skip to content

Commit

Permalink
fix: don't strip loader "ref" from import string
Browse files Browse the repository at this point in the history
  • Loading branch information
skoging committed Nov 25, 2020
1 parent 09047cf commit 6271fc4
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function contextify(context, request) {
return request
.split('!')
.map((r) => {
const splitPath = r.split('?', 2);
const splitPath = r.split('?');

if (/^[a-zA-Z]:\\/.test(splitPath[0])) {
splitPath[0] = path.win32.relative(context, splitPath[0]);
Expand Down
29 changes: 29 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,35 @@ Object {

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

exports[`loader should work inline 1 with config loader options: errors 1`] = `Array []`;

exports[`loader should work inline 1 with config loader options: module 1`] = `
"var ___EXPOSE_LOADER_IMPORT___ = require(\\"-!../../node_modules/babel-loader/lib/index.js??{{config-reference}}!./global-commonjs2-single-export.js\\");
var ___EXPOSE_LOADER_GET_GLOBAL_THIS___ = require(\\"../../src/runtime/getGlobalThis.js\\");
var ___EXPOSE_LOADER_GLOBAL_THIS___ = ___EXPOSE_LOADER_GET_GLOBAL_THIS___;
if (typeof ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] === 'undefined') ___EXPOSE_LOADER_GLOBAL_THIS___[\\"myGlobal\\"] = ___EXPOSE_LOADER_IMPORT___;
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')
module.exports = ___EXPOSE_LOADER_IMPORT___;
"
`;

exports[`loader should work inline 1 with config loader options: result 1`] = `
Object {
"ExposeLoader": Object {
"default": Object {
"exposedEqualsGlobal": true,
"exposedEqualsImported": true,
"importedEqualsGlobal": true,
},
},
"myGlobal": Object {
"foo": "bar",
},
}
`;

exports[`loader should work inline 1 with config loader options: warnings 1`] = `Array []`;

exports[`loader should work inline 1 without extension: errors 1`] = `Array []`;

exports[`loader should work inline 1 without extension: module 1`] = `
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/inline-import-equality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import exposed from '../../src/cjs.js?exposes=myGlobal!./global-commonjs2-single-export.js';

import imported from './global-commonjs2-single-export.js'

export default {
exposedEqualsGlobal: exposed === myGlobal,
importedEqualsGlobal: imported === myGlobal,
exposedEqualsImported: exposed === imported,
}
44 changes: 43 additions & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,48 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work inline 1 with config loader options', async () => {
const compiler = getCompiler(
'inline-import-equality.js',
{},
{
devtool: 'source-map',
module: {
rules: [
{
test: /.*global-commonjs2-single-export\.js/i,
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
],
},
],
},
}
);
const stats = await compile(compiler);

const isWebpack5 = webpack.version[0] === '5';
const refRegexp = isWebpack5 ? /\?ruleSet\[\d+\].*!/ : /\?ref--[0-9-]+!/;

expect(
getModuleSource(
'./global-commonjs2-single-export-exposed.js',
stats
).replace(refRegexp, '?{{config-reference}}!')
).toMatchSnapshot('module');
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(readAsset('main.bundle.js.map', compiler, stats)).toBeDefined();
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should work inline 1 without extension', async () => {
const compiler = getCompiler(
'inline-import-1.js',
Expand Down Expand Up @@ -348,7 +390,7 @@ describe('loader', () => {
).toMatchSnapshot('module');
expect(module.hash).toBe(
isWebpack5
? 'ca629829313dd6de9e673c154aa723c4'
? '53b5c93a2ac82d2e55921ab5bcf9649e'
: 'c3e516476bee11406ecca2a29b66c743'
);
expect(getErrors(stats)).toMatchSnapshot('errors');
Expand Down

0 comments on commit 6271fc4

Please sign in to comment.