Skip to content

fix: compatibility with ES modules syntax and hash in url function #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: compatibility with ES modules syntax and hash in url function
  • Loading branch information
alexander-akait committed Dec 2, 2019
commit a646ca1a4acccf729ca8df925d185f74adb21e95
14 changes: 12 additions & 2 deletions src/runtime/getUrl.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading