Skip to content

Commit a646ca1

Browse files
fix: compatibility with ES modules syntax and hash in url function
1 parent 0961304 commit a646ca1

File tree

6 files changed

+290
-101
lines changed

6 files changed

+290
-101
lines changed

src/runtime/getUrl.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
module.exports = (url, needQuotes) => {
1+
module.exports = (url, options) => {
2+
if (!options) {
3+
// eslint-disable-next-line no-param-reassign
4+
options = {};
5+
}
6+
27
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
38
url = url.__esModule ? url.default : url;
49

@@ -12,9 +17,14 @@ module.exports = (url, needQuotes) => {
1217
url = url.slice(1, -1);
1318
}
1419

20+
if (options.hash) {
21+
// eslint-disable-next-line no-param-reassign
22+
url += options.hash;
23+
}
24+
1525
// Should url be wrapped?
1626
// See https://drafts.csswg.org/css-values-3/#urls
17-
if (/["'() \t\n]/.test(url) || needQuotes) {
27+
if (/["'() \t\n]/.test(url) || options.needQuotes) {
1828
return `"${url.replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;
1929
}
2030

src/utils.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,23 @@ function getUrlItemCode(item, loaderContext) {
242242
? `"${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}"`
243243
: '';
244244

245+
const options = [];
246+
247+
if (hash) {
248+
options.push(`hash: ${hash}`);
249+
}
250+
251+
if (needQuotes) {
252+
options.push(`needQuotes: true`);
253+
}
254+
255+
const preparedOptions =
256+
options.length > 0 ? `, { ${options.join(', ')} }` : '';
257+
245258
return `var ${placeholder} = getUrl(require(${stringifyRequest(
246259
loaderContext,
247260
urlToRequest(normalizedUrl)
248-
)})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`;
261+
)})${preparedOptions});`;
249262
}
250263

251264
function getApiCode(loaderContext, sourceMap, onlyLocals) {

test/__snapshots__/loader.test.js.snap

+72-12
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ function toComment(sourceMap) {
102102
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`global\`): errors 1`] = `Array []`;
103103
104104
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`global\`): escape 1`] = `
105-
"module.exports = (url, needQuotes) => {
105+
"module.exports = (url, options) => {
106+
if (!options) {
107+
// eslint-disable-next-line no-param-reassign
108+
options = {};
109+
}
110+
106111
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
107112
url = url.__esModule ? url.default : url;
108113
@@ -116,9 +121,14 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
116121
url = url.slice(1, -1);
117122
}
118123
124+
if (options.hash) {
125+
// eslint-disable-next-line no-param-reassign
126+
url += options.hash;
127+
}
128+
119129
// Should url be wrapped?
120130
// See https://drafts.csswg.org/css-values-3/#urls
121-
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
131+
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
122132
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
123133
}
124134
@@ -395,7 +405,12 @@ function toComment(sourceMap) {
395405
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`local\`): errors 1`] = `Array []`;
396406
397407
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`local\`): escape 1`] = `
398-
"module.exports = (url, needQuotes) => {
408+
"module.exports = (url, options) => {
409+
if (!options) {
410+
// eslint-disable-next-line no-param-reassign
411+
options = {};
412+
}
413+
399414
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
400415
url = url.__esModule ? url.default : url;
401416
@@ -409,9 +424,14 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
409424
url = url.slice(1, -1);
410425
}
411426
427+
if (options.hash) {
428+
// eslint-disable-next-line no-param-reassign
429+
url += options.hash;
430+
}
431+
412432
// Should url be wrapped?
413433
// See https://drafts.csswg.org/css-values-3/#urls
414-
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
434+
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
415435
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
416436
}
417437
@@ -712,7 +732,12 @@ function toComment(sourceMap) {
712732
exports[`loader should compile with \`css\` entry point: errors 1`] = `Array []`;
713733
714734
exports[`loader should compile with \`css\` entry point: escape 1`] = `
715-
"module.exports = (url, needQuotes) => {
735+
"module.exports = (url, options) => {
736+
if (!options) {
737+
// eslint-disable-next-line no-param-reassign
738+
options = {};
739+
}
740+
716741
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
717742
url = url.__esModule ? url.default : url;
718743
@@ -726,9 +751,14 @@ exports[`loader should compile with \`css\` entry point: escape 1`] = `
726751
url = url.slice(1, -1);
727752
}
728753
754+
if (options.hash) {
755+
// eslint-disable-next-line no-param-reassign
756+
url += options.hash;
757+
}
758+
729759
// Should url be wrapped?
730760
// See https://drafts.csswg.org/css-values-3/#urls
731-
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
761+
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
732762
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
733763
}
734764
@@ -1005,7 +1035,12 @@ function toComment(sourceMap) {
10051035
exports[`loader should compile with \`js\` entry point: errors 1`] = `Array []`;
10061036
10071037
exports[`loader should compile with \`js\` entry point: escape 1`] = `
1008-
"module.exports = (url, needQuotes) => {
1038+
"module.exports = (url, options) => {
1039+
if (!options) {
1040+
// eslint-disable-next-line no-param-reassign
1041+
options = {};
1042+
}
1043+
10091044
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
10101045
url = url.__esModule ? url.default : url;
10111046
@@ -1019,9 +1054,14 @@ exports[`loader should compile with \`js\` entry point: escape 1`] = `
10191054
url = url.slice(1, -1);
10201055
}
10211056
1057+
if (options.hash) {
1058+
// eslint-disable-next-line no-param-reassign
1059+
url += options.hash;
1060+
}
1061+
10221062
// Should url be wrapped?
10231063
// See https://drafts.csswg.org/css-values-3/#urls
1024-
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
1064+
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
10251065
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
10261066
}
10271067
@@ -1490,7 +1530,12 @@ exports.push([module.i, \\".foo {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
14901530
/* 4 */
14911531
/***/ (function(module, exports) {
14921532
1493-
module.exports = (url, needQuotes) => {
1533+
module.exports = (url, options) => {
1534+
if (!options) {
1535+
// eslint-disable-next-line no-param-reassign
1536+
options = {};
1537+
}
1538+
14941539
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
14951540
url = url.__esModule ? url.default : url;
14961541
@@ -1504,9 +1549,14 @@ module.exports = (url, needQuotes) => {
15041549
url = url.slice(1, -1);
15051550
}
15061551
1552+
if (options.hash) {
1553+
// eslint-disable-next-line no-param-reassign
1554+
url += options.hash;
1555+
}
1556+
15071557
// Should url be wrapped?
15081558
// See https://drafts.csswg.org/css-values-3/#urls
1509-
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
1559+
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
15101560
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
15111561
}
15121562
@@ -1755,7 +1805,12 @@ exports.push([module.i, \\".foo {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
17551805
/* 4 */
17561806
/***/ (function(module, exports) {
17571807
1758-
module.exports = (url, needQuotes) => {
1808+
module.exports = (url, options) => {
1809+
if (!options) {
1810+
// eslint-disable-next-line no-param-reassign
1811+
options = {};
1812+
}
1813+
17591814
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
17601815
url = url.__esModule ? url.default : url;
17611816
@@ -1769,9 +1824,14 @@ module.exports = (url, needQuotes) => {
17691824
url = url.slice(1, -1);
17701825
}
17711826
1827+
if (options.hash) {
1828+
// eslint-disable-next-line no-param-reassign
1829+
url += options.hash;
1830+
}
1831+
17721832
// Should url be wrapped?
17731833
// See https://drafts.csswg.org/css-values-3/#urls
1774-
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
1834+
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
17751835
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
17761836
}
17771837

0 commit comments

Comments
 (0)