Skip to content

Commit e97d3c8

Browse files
fix: pass query with hash to other loaders (#349)
1 parent 6fff934 commit e97d3c8

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export function parseSrc(input) {
371371
}
372372

373373
export function normalizeUrl(url) {
374-
return decodeURIComponent(url).replace(/[\t\n\r]/g, '');
374+
return decodeURI(url).replace(/[\t\n\r]/g, '');
375375
}
376376

377377
const moduleRequestRegex = /^[^?]*~/;

test/__snapshots__/loader.test.js.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ exports[`loader should not make bad things with templates: result 1`] = `
6262
6363
exports[`loader should not make bad things with templates: warnings 1`] = `Array []`;
6464
65+
exports[`loader should pass queries to other loader: errors 1`] = `Array []`;
66+
67+
exports[`loader should pass queries to other loader: module 1`] = `
68+
"// Imports
69+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
70+
import ___HTML_LOADER_IMPORT_0___ from \\"./icons.svg?color=%23BAAFDB%3F\\";
71+
// Module
72+
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___, { hash: \\"#foo\\" });
73+
var code = \\"<img src=\\\\\\"\\" + ___HTML_LOADER_REPLACEMENT_0___ + \\"\\\\\\">\\";
74+
// Exports
75+
export default code;"
76+
`;
77+
78+
exports[`loader should pass queries to other loader: result 1`] = `"<img src=\\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=#foo\\">"`;
79+
80+
exports[`loader should pass queries to other loader: warnings 1`] = `Array []`;
81+
6582
exports[`loader should work with "resolve.roots": errors 1`] = `Array []`;
6683
6784
exports[`loader should work with "resolve.roots": module 1`] = `

test/fixtures/other-loader-query.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<img src="icons.svg?color=%23BAAFDB%3F#foo">

test/fixtures/other-loader-query.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import html from './other-loader-query.html';
2+
3+
export default html;

test/helpers/svg-color-loader.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const querystring = require('querystring');
2+
3+
module.exports = function loader() {
4+
const query = querystring.parse(this.resourceQuery.slice(1));
5+
6+
if (typeof query.color === 'undefined' || query.color !== '#BAAFDB?') {
7+
throw new Error(`Error, 'color' is '${query.color}'`);
8+
}
9+
10+
return `export default "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";`;
11+
};

test/loader.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,42 @@ describe('loader', () => {
152152
expect(getWarnings(stats)).toMatchSnapshot('warnings');
153153
expect(getErrors(stats)).toMatchSnapshot('errors');
154154
});
155+
156+
it('should pass queries to other loader', async () => {
157+
const compiler = getCompiler(
158+
'other-loader-query.js',
159+
{},
160+
{
161+
module: {
162+
rules: [
163+
{
164+
test: /\.svg$/i,
165+
resourceQuery: /color/,
166+
enforce: 'pre',
167+
use: {
168+
loader: path.resolve(
169+
__dirname,
170+
'./helpers/svg-color-loader.js'
171+
),
172+
},
173+
},
174+
{
175+
test: /\.html$/i,
176+
rules: [{ loader: path.resolve(__dirname, '../src') }],
177+
},
178+
],
179+
},
180+
}
181+
);
182+
const stats = await compile(compiler);
183+
184+
expect(getModuleSource('./other-loader-query.html', stats)).toMatchSnapshot(
185+
'module'
186+
);
187+
expect(
188+
execute(readAsset('main.bundle.js', compiler, stats))
189+
).toMatchSnapshot('result');
190+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
191+
expect(getErrors(stats)).toMatchSnapshot('errors');
192+
});
155193
});

0 commit comments

Comments
 (0)