Skip to content

Commit 9fac1ec

Browse files
refactor: migrate on message api for url
1 parent c170a4a commit 9fac1ec

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

lib/loader.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ module.exports = function loader(content, map) {
123123
.warnings()
124124
.forEach((warning) => this.emitWarning(new Warning(warning)));
125125

126+
const messages = result.messages || [];
126127
const { camelCase, exportOnlyLocals, importLoaders } = options;
127-
const { importItems, urlItems, exports } = parserOptions;
128+
const { importItems, exports } = parserOptions;
129+
128130
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
129131
const importUrlPrefix = getImportPrefix(this, importLoaders);
132+
130133
// Prepare replacer to change from `___CSS_LOADER_IMPORT___INDEX___` to `require('./file.css').locals`
131134
const importItemReplacer = (item) => {
132135
const match = placholderRegExps.importItem.exec(item);
@@ -203,40 +206,40 @@ module.exports = function loader(content, map) {
203206
// helper for ensuring valid CSS strings from requires
204207
let urlEscapeHelperCode = '';
205208

206-
if (resolveUrl && urlItems && urlItems.length > 0) {
207-
urlEscapeHelperCode = `var escape = require(${stringifyRequest(
208-
this,
209-
require.resolve('./runtime/escape.js')
210-
)});\n`;
211-
212-
cssAsString = cssAsString.replace(
213-
placholderRegExps.urlItemG,
214-
(item) => {
215-
const match = placholderRegExps.urlItem.exec(item);
216-
const idx = Number(match[1]);
217-
218-
if (!urlItems[idx]) {
219-
return item;
220-
}
221-
222-
const urlItem = urlItems[idx];
223-
const { url } = urlItem;
224-
// Remove `#hash` and `?#hash` from `require`
225-
const [normalizedUrl, singleQuery, hashValue] = url.split(/(\?)?#/);
226-
const hash =
227-
singleQuery || hashValue
228-
? `"${singleQuery ? '?' : ''}${
229-
hashValue ? `#${hashValue}` : ''
230-
}"`
231-
: '';
232-
233-
return `" + escape(require(${stringifyRequest(
209+
messages
210+
.filter((message) => message.type === 'url' && resolveUrl)
211+
.forEach((message) => {
212+
if (!urlEscapeHelperCode) {
213+
urlEscapeHelperCode = `var escape = require(${stringifyRequest(
234214
this,
235-
normalizedUrl
236-
)})${hash ? ` + ${hash}` : ''}) + "`;
215+
require.resolve('./runtime/escape.js')
216+
)});\n`;
237217
}
238-
);
239-
}
218+
219+
const { item } = message;
220+
const { url, placeholder } = item;
221+
222+
cssAsString = cssAsString.replace(
223+
new RegExp(placeholder, 'g'),
224+
() => {
225+
// Remove `#hash` and `?#hash` from `require`
226+
const [normalizedUrl, singleQuery, hashValue] = url.split(
227+
/(\?)?#/
228+
);
229+
const hash =
230+
singleQuery || hashValue
231+
? `"${singleQuery ? '?' : ''}${
232+
hashValue ? `#${hashValue}` : ''
233+
}"`
234+
: '';
235+
236+
return `" + escape(require(${stringifyRequest(
237+
this,
238+
normalizedUrl
239+
)})${hash ? ` + ${hash}` : ''}) + "`;
240+
}
241+
);
242+
});
240243

241244
if (exportCode) {
242245
exportCode = `exports.locals = ${exportCode};`;

lib/plugins/postcss-url-parser.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,15 @@ module.exports = postcss.plugin(
112112
const urls = {};
113113

114114
paths.forEach((path, index) => {
115-
urls[path] = `___CSS_LOADER_URL___${index}___`;
116-
urlItems.push({ url: path });
115+
const placeholder = `___CSS_LOADER_URL___${index}___`;
116+
117+
urls[path] = placeholder;
118+
119+
result.messages.push({
120+
pluginName,
121+
type: 'url',
122+
item: { url: path, placeholder },
123+
});
117124
});
118125

119126
traversed.forEach((item) => {

lib/utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ const loaderUtils = require('loader-utils');
1010
const placholderRegExps = {
1111
importItemG: /___CSS_LOADER_IMPORT___([0-9]+)___/g,
1212
importItem: /___CSS_LOADER_IMPORT___([0-9]+)___/,
13-
urlItemG: /___CSS_LOADER_URL___([0-9]+)___/g,
14-
urlItem: /___CSS_LOADER_URL___([0-9]+)___/,
1513
};
1614

1715
function getImportPrefix(loaderContext, importLoaders) {

0 commit comments

Comments
 (0)