Skip to content

Commit 2e62346

Browse files
refactor: importItemMatcher util (#823)
1 parent f9aa73c commit 2e62346

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

lib/loader.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
const loaderUtils = require('loader-utils');
66

77
const processCss = require('./processCss');
8-
const { getImportPrefix, compileExports } = require('./utils');
8+
const {
9+
getImportPrefix,
10+
placeholderImportItemReplacer,
11+
compileExports,
12+
} = require('./utils');
913

1014
module.exports = function loader(content, map) {
1115
const callback = this.async();
@@ -78,20 +82,9 @@ module.exports = function loader(content, map) {
7882
}, this)
7983
.join('\n');
8084

81-
function importItemMatcher(item) {
82-
const match = result.importItemRegExp.exec(item);
83-
const idx = +match[1];
84-
const importItem = result.importItems[idx];
85-
const importUrl = importUrlPrefix + importItem.url;
86-
return `" + require(${loaderUtils.stringifyRequest(
87-
this,
88-
importUrl
89-
)}).locals[${JSON.stringify(importItem.export)}] + "`;
90-
}
91-
9285
cssAsString = cssAsString.replace(
9386
result.importItemRegExpG,
94-
importItemMatcher.bind(this)
87+
placeholderImportItemReplacer(this, result, importUrlPrefix)
9588
);
9689

9790
// helper for ensuring valid CSS strings from requires
@@ -136,7 +129,7 @@ module.exports = function loader(content, map) {
136129

137130
let exportJs = compileExports(
138131
result,
139-
importItemMatcher.bind(this),
132+
placeholderImportItemReplacer(this, result, importUrlPrefix),
140133
options.camelCase
141134
);
142135
if (exportJs) {
@@ -170,7 +163,7 @@ module.exports = function loader(content, map) {
170163
moduleJs = `exports.push([module.id, ${cssAsString}, ""]);`;
171164
}
172165

173-
// embed runtime
166+
// Embed runtime
174167
return callback(
175168
null,
176169
`${urlEscapeHelper}exports = module.exports = require(${loaderUtils.stringifyRequest(

lib/localsLoader.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
const loaderUtils = require('loader-utils');
66

77
const processCss = require('./processCss');
8-
const { getImportPrefix, compileExports } = require('./utils');
8+
const {
9+
getImportPrefix,
10+
placeholderImportItemReplacer,
11+
compileExports,
12+
} = require('./utils');
913

1014
module.exports = function loader(content) {
1115
const callback = this.async();
@@ -26,20 +30,9 @@ module.exports = function loader(content) {
2630
// for importing CSS
2731
const importUrlPrefix = getImportPrefix(this, options);
2832

29-
function importItemMatcher(item) {
30-
const match = result.importItemRegExp.exec(item);
31-
const idx = +match[1];
32-
const importItem = result.importItems[idx];
33-
const importUrl = importUrlPrefix + importItem.url;
34-
return (
35-
`" + require(${loaderUtils.stringifyRequest(this, importUrl)})` +
36-
`[${JSON.stringify(importItem.export)}] + "`
37-
);
38-
}
39-
4033
let exportJs = compileExports(
4134
result,
42-
importItemMatcher.bind(this),
35+
placeholderImportItemReplacer(this, result, importUrlPrefix, true),
4336
options.camelCase
4437
);
4538
if (exportJs) {

lib/utils.js

+27
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,36 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
110110
.replace(/^((-?[0-9])|--)/, '_$1');
111111
}
112112

113+
function placeholderImportItemReplacer(
114+
loaderContext,
115+
result,
116+
importUrlPrefix,
117+
onlyLocals = false
118+
) {
119+
return (item) => {
120+
const match = result.importItemRegExp.exec(item);
121+
const idx = +match[1];
122+
const importItem = result.importItems[idx];
123+
const importUrl = importUrlPrefix + importItem.url;
124+
125+
if (onlyLocals) {
126+
return `" + require(${loaderUtils.stringifyRequest(
127+
loaderContext,
128+
importUrl
129+
)})[${JSON.stringify(importItem.export)}] + "`;
130+
}
131+
132+
return `" + require(${loaderUtils.stringifyRequest(
133+
loaderContext,
134+
importUrl
135+
)}).locals[${JSON.stringify(importItem.export)}] + "`;
136+
};
137+
}
138+
113139
module.exports = {
114140
dashesCamelCase,
115141
compileExports,
116142
getImportPrefix,
117143
getLocalIdent,
144+
placeholderImportItemReplacer,
118145
};

0 commit comments

Comments
 (0)