From 592b37df30c5c08025d6c007df3a662cb45293cf Mon Sep 17 00:00:00 2001 From: cap-Bernardito Date: Tue, 9 Feb 2021 12:53:57 +0300 Subject: [PATCH] fix: pass query with hash to other loaders --- src/utils.js | 2 +- test/__snapshots__/loader.test.js.snap | 17 ++++++++++++ test/fixtures/other-loader-query.html | 1 + test/fixtures/other-loader-query.js | 3 ++ test/helpers/svg-color-loader.js | 11 ++++++++ test/loader.test.js | 38 ++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/other-loader-query.html create mode 100644 test/fixtures/other-loader-query.js create mode 100644 test/helpers/svg-color-loader.js diff --git a/src/utils.js b/src/utils.js index 283f8238..b2b018cd 100644 --- a/src/utils.js +++ b/src/utils.js @@ -371,7 +371,7 @@ export function parseSrc(input) { } export function normalizeUrl(url) { - return decodeURIComponent(url).replace(/[\t\n\r]/g, ''); + return decodeURI(url).replace(/[\t\n\r]/g, ''); } const moduleRequestRegex = /^[^?]*~/; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 36723c0d..cc6ff17f 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -62,6 +62,23 @@ exports[`loader should not make bad things with templates: result 1`] = ` exports[`loader should not make bad things with templates: warnings 1`] = `Array []`; +exports[`loader should pass queries to other loader: errors 1`] = `Array []`; + +exports[`loader should pass queries to other loader: module 1`] = ` +"// Imports +import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\"; +import ___HTML_LOADER_IMPORT_0___ from \\"./icons.svg?color=%23BAAFDB%3F\\"; +// Module +var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___, { hash: \\"#foo\\" }); +var code = \\"\\"; +// Exports +export default code;" +`; + +exports[`loader should pass queries to other loader: result 1`] = `""`; + +exports[`loader should pass queries to other loader: warnings 1`] = `Array []`; + exports[`loader should work with "resolve.roots": errors 1`] = `Array []`; exports[`loader should work with "resolve.roots": module 1`] = ` diff --git a/test/fixtures/other-loader-query.html b/test/fixtures/other-loader-query.html new file mode 100644 index 00000000..be57c5e1 --- /dev/null +++ b/test/fixtures/other-loader-query.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/fixtures/other-loader-query.js b/test/fixtures/other-loader-query.js new file mode 100644 index 00000000..3cfa00c5 --- /dev/null +++ b/test/fixtures/other-loader-query.js @@ -0,0 +1,3 @@ +import html from './other-loader-query.html'; + +export default html; diff --git a/test/helpers/svg-color-loader.js b/test/helpers/svg-color-loader.js new file mode 100644 index 00000000..2bbcdd4b --- /dev/null +++ b/test/helpers/svg-color-loader.js @@ -0,0 +1,11 @@ +const querystring = require('querystring'); + +module.exports = function loader() { + const query = querystring.parse(this.resourceQuery.slice(1)); + + if (typeof query.color === 'undefined' || query.color !== '#BAAFDB?') { + throw new Error(`Error, 'color' is '${query.color}'`); + } + + return `export default "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";`; +}; diff --git a/test/loader.test.js b/test/loader.test.js index 04c53d78..011e5d56 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -152,4 +152,42 @@ describe('loader', () => { expect(getWarnings(stats)).toMatchSnapshot('warnings'); expect(getErrors(stats)).toMatchSnapshot('errors'); }); + + it('should pass queries to other loader', async () => { + const compiler = getCompiler( + 'other-loader-query.js', + {}, + { + module: { + rules: [ + { + test: /\.svg$/i, + resourceQuery: /color/, + enforce: 'pre', + use: { + loader: path.resolve( + __dirname, + './helpers/svg-color-loader.js' + ), + }, + }, + { + test: /\.html$/i, + rules: [{ loader: path.resolve(__dirname, '../src') }], + }, + ], + }, + } + ); + const stats = await compile(compiler); + + expect(getModuleSource('./other-loader-query.html', stats)).toMatchSnapshot( + 'module' + ); + expect( + execute(readAsset('main.bundle.js', compiler, stats)) + ).toMatchSnapshot('result'); + expect(getWarnings(stats)).toMatchSnapshot('warnings'); + expect(getErrors(stats)).toMatchSnapshot('errors'); + }); });