Skip to content

Commit

Permalink
fix: compatibility with ES modules syntax and hash in url function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Dec 2, 2019
1 parent e21ab00 commit 8f4d6f5
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 101 deletions.
14 changes: 12 additions & 2 deletions src/runtime/getUrl.js
@@ -1,4 +1,9 @@
module.exports = (url, needQuotes) => {
module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}

// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;

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

if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}

// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/["'() \t\n]/.test(url) || needQuotes) {
if (/["'() \t\n]/.test(url) || options.needQuotes) {
return `"${url.replace(/"/g, '\\"').replace(/\n/g, '\\n')}"`;
}

Expand Down
15 changes: 14 additions & 1 deletion src/utils.js
Expand Up @@ -242,10 +242,23 @@ function getUrlItemCode(item, loaderContext) {
? `"${singleQuery ? '?' : ''}${hashValue ? `#${hashValue}` : ''}"`
: '';

const options = [];

if (hash) {
options.push(`hash: ${hash}`);
}

if (needQuotes) {
options.push(`needQuotes: true`);
}

const preparedOptions =
options.length > 0 ? `, { ${options.join(', ')} }` : '';

return `var ${placeholder} = getUrl(require(${stringifyRequest(
loaderContext,
urlToRequest(normalizedUrl)
)})${hash ? ` + ${hash}` : ''}${needQuotes ? ', true' : ''});`;
)})${preparedOptions});`;
}

function getApiCode(loaderContext, sourceMap, onlyLocals) {
Expand Down
84 changes: 72 additions & 12 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -102,7 +102,12 @@ function toComment(sourceMap) {
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`global\`): errors 1`] = `Array []`;
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`global\`): escape 1`] = `
"module.exports = (url, needQuotes) => {
"module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;
Expand All @@ -116,9 +121,14 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
url = url.slice(1, -1);
}
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
}
Expand Down Expand Up @@ -395,7 +405,12 @@ function toComment(sourceMap) {
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`local\`): errors 1`] = `Array []`;
exports[`loader should compile with \`css\` entry point (with \`modules\` and scope \`local\`): escape 1`] = `
"module.exports = (url, needQuotes) => {
"module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;
Expand All @@ -409,9 +424,14 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
url = url.slice(1, -1);
}
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
}
Expand Down Expand Up @@ -712,7 +732,12 @@ function toComment(sourceMap) {
exports[`loader should compile with \`css\` entry point: errors 1`] = `Array []`;
exports[`loader should compile with \`css\` entry point: escape 1`] = `
"module.exports = (url, needQuotes) => {
"module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;
Expand All @@ -726,9 +751,14 @@ exports[`loader should compile with \`css\` entry point: escape 1`] = `
url = url.slice(1, -1);
}
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
}
Expand Down Expand Up @@ -1005,7 +1035,12 @@ function toComment(sourceMap) {
exports[`loader should compile with \`js\` entry point: errors 1`] = `Array []`;
exports[`loader should compile with \`js\` entry point: escape 1`] = `
"module.exports = (url, needQuotes) => {
"module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;
Expand All @@ -1019,9 +1054,14 @@ exports[`loader should compile with \`js\` entry point: escape 1`] = `
url = url.slice(1, -1);
}
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
}
Expand Down Expand Up @@ -1490,7 +1530,12 @@ exports.push([module.i, \\".foo {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
/* 4 */
/***/ (function(module, exports) {
module.exports = (url, needQuotes) => {
module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;
Expand All @@ -1504,9 +1549,14 @@ module.exports = (url, needQuotes) => {
url = url.slice(1, -1);
}
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
}
Expand Down Expand Up @@ -1755,7 +1805,12 @@ exports.push([module.i, \\".foo {\\\\n color: red;\\\\n}\\\\n\\", \\"\\"]);
/* 4 */
/***/ (function(module, exports) {
module.exports = (url, needQuotes) => {
module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url.__esModule ? url.default : url;
Expand All @@ -1769,9 +1824,14 @@ module.exports = (url, needQuotes) => {
url = url.slice(1, -1);
}
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}
// Should url be wrapped?
// See https://drafts.csswg.org/css-values-3/#urls
if (/[\\"'() \\\\t\\\\n]/.test(url) || needQuotes) {
if (/[\\"'() \\\\t\\\\n]/.test(url) || options.needQuotes) {
return \`\\"\${url.replace(/\\"/g, '\\\\\\\\\\"').replace(/\\\\n/g, '\\\\\\\\n')}\\"\`;
}
Expand Down

0 comments on commit 8f4d6f5

Please sign in to comment.