Skip to content

Commit 729a314

Browse files
fix: pass query with hash to other loaders (#1261)
1 parent 4bae2e6 commit 729a314

File tree

7 files changed

+95
-2
lines changed

7 files changed

+95
-2
lines changed

src/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ function normalizeUrl(url, isStringValue) {
8585
}
8686

8787
if (matchNativeWin32Path.test(url)) {
88-
return decodeURIComponent(normalizedUrl);
88+
return decodeURI(normalizedUrl);
8989
}
9090

91-
return decodeURIComponent(unescape(normalizedUrl));
91+
return decodeURI(unescape(normalizedUrl));
9292
}
9393

9494
function requestify(url, rootContext) {

test/__snapshots__/loader.test.js.snap

+31
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,37 @@ Array [
6464

6565
exports[`loader should not generate console.warn when plugins disabled and hideNothingWarning is "true": warnings 1`] = `Array []`;
6666

67+
exports[`loader should pass queries to other loader: errors 1`] = `Array []`;
68+
69+
exports[`loader should pass queries to other loader: module 1`] = `
70+
"// Imports
71+
import ___CSS_LOADER_API_IMPORT___ from \\"../../src/runtime/api.js\\";
72+
import ___CSS_LOADER_GET_URL_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
73+
import ___CSS_LOADER_URL_IMPORT_0___ from \\"./url/image.svg?color=%23BAAFDB%3F\\";
74+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
75+
var ___CSS_LOADER_URL_REPLACEMENT_0___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___, { hash: \\"#foo\\" });
76+
// Module
77+
___CSS_LOADER_EXPORT___.push([module.id, \\".example {\\\\n background-image: url(\\" + ___CSS_LOADER_URL_REPLACEMENT_0___ + \\");\\\\n}\\\\n\\", \\"\\"]);
78+
// Exports
79+
export default ___CSS_LOADER_EXPORT___;
80+
"
81+
`;
82+
83+
exports[`loader should pass queries to other loader: result 1`] = `
84+
Array [
85+
Array [
86+
"./other-loader-query.css",
87+
".example {
88+
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=#foo);
89+
}
90+
",
91+
"",
92+
],
93+
]
94+
`;
95+
96+
exports[`loader should pass queries to other loader: warnings 1`] = `Array []`;
97+
6798
exports[`loader should reuse \`ast\` from "postcss-loader": errors 1`] = `Array []`;
6899

69100
exports[`loader should reuse \`ast\` from "postcss-loader": module 1`] = `

test/fixtures/other-loader-query.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.example {
2+
background-image: url('./url/image.svg?color=%23BAAFDB%3F#foo');
3+
}

test/fixtures/other-loader-query.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './other-loader-query.css';
2+
3+
__export__ = css;
4+
5+
export default css;

test/fixtures/url/image.svg

+5
Loading

test/helpers/svg-color-loader.js

+11
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

+38
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,42 @@ describe("loader", () => {
472472
expect(getWarnings(stats)).toMatchSnapshot("warnings");
473473
expect(getErrors(stats)).toMatchSnapshot("errors");
474474
});
475+
476+
it("should pass queries to other loader", async () => {
477+
const compiler = getCompiler(
478+
"./other-loader-query.js",
479+
{},
480+
{
481+
module: {
482+
rules: [
483+
{
484+
test: /\.svg$/i,
485+
resourceQuery: /color/,
486+
enforce: "pre",
487+
use: {
488+
loader: path.resolve(
489+
__dirname,
490+
"./helpers/svg-color-loader.js"
491+
),
492+
},
493+
},
494+
{
495+
test: /\.css$/i,
496+
rules: [{ loader: path.resolve(__dirname, "../src") }],
497+
},
498+
],
499+
},
500+
}
501+
);
502+
const stats = await compile(compiler);
503+
504+
expect(getModuleSource("./other-loader-query.css", stats)).toMatchSnapshot(
505+
"module"
506+
);
507+
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
508+
"result"
509+
);
510+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
511+
expect(getErrors(stats)).toMatchSnapshot("errors");
512+
});
475513
});

0 commit comments

Comments
 (0)