Skip to content

Commit 0a68bb4

Browse files
refactor: code (#811)
1 parent cb559f2 commit 0a68bb4

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

lib/loader.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ const { getImportPrefix, compileExports } = require('./utils');
99

1010
module.exports = function loader(content, map) {
1111
const callback = this.async();
12-
const query = loaderUtils.getOptions(this) || {};
13-
const moduleMode = query.modules;
14-
const camelCaseKeys = query.camelCase;
15-
const sourceMap = query.sourceMap || false;
12+
const options = loaderUtils.getOptions(this) || {};
13+
const sourceMap = options.sourceMap || false;
1614

1715
/* eslint-disable no-param-reassign */
1816
if (sourceMap) {
@@ -36,17 +34,8 @@ module.exports = function loader(content, map) {
3634
content,
3735
map,
3836
{
39-
mode: moduleMode ? 'local' : 'global',
40-
from: loaderUtils
41-
.getRemainingRequest(this)
42-
.split('!')
43-
.pop(),
44-
to: loaderUtils
45-
.getCurrentRequest(this)
46-
.split('!')
47-
.pop(),
48-
query,
4937
loaderContext: this,
38+
loaderOptions: options,
5039
sourceMap,
5140
},
5241
(err, result) => {
@@ -57,7 +46,7 @@ module.exports = function loader(content, map) {
5746
let cssAsString = JSON.stringify(result.source);
5847

5948
// for importing CSS
60-
const importUrlPrefix = getImportPrefix(this, query);
49+
const importUrlPrefix = getImportPrefix(this, options);
6150

6251
const alreadyImported = {};
6352
const importJs = result.importItems
@@ -109,7 +98,7 @@ module.exports = function loader(content, map) {
10998
// helper for ensuring valid CSS strings from requires
11099
let urlEscapeHelper = '';
111100

112-
if (query.url !== false && result.urlItems.length > 0) {
101+
if (options.url !== false && result.urlItems.length > 0) {
113102
urlEscapeHelper = `var escape = require(${loaderUtils.stringifyRequest(
114103
this,
115104
require.resolve('./runtime/escape.js')
@@ -145,7 +134,7 @@ module.exports = function loader(content, map) {
145134
let exportJs = compileExports(
146135
result,
147136
importItemMatcher.bind(this),
148-
camelCaseKeys
137+
options.camelCase
149138
);
150139
if (exportJs) {
151140
exportJs = `exports.locals = ${exportJs};`;

lib/localsLoader.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ const { getImportPrefix, compileExports } = require('./utils');
99

1010
module.exports = function loader(content) {
1111
const callback = this.async();
12-
const query = loaderUtils.getOptions(this) || {};
13-
const moduleMode = query.modules;
14-
const camelCaseKeys = query.camelCase;
12+
const options = loaderUtils.getOptions(this) || {};
1513

1614
processCss(
1715
content,
1816
null,
1917
{
20-
mode: moduleMode ? 'local' : 'global',
21-
query,
2218
loaderContext: this,
19+
loaderOptions: options,
2320
},
2421
(err, result) => {
2522
if (err) {
2623
return callback(err);
2724
}
2825

2926
// for importing CSS
30-
const importUrlPrefix = getImportPrefix(this, query);
27+
const importUrlPrefix = getImportPrefix(this, options);
3128

3229
function importItemMatcher(item) {
3330
const match = result.importItemRegExp.exec(item);
@@ -43,7 +40,7 @@ module.exports = function loader(content) {
4340
let exportJs = compileExports(
4441
result,
4542
importItemMatcher.bind(this),
46-
camelCaseKeys
43+
options.camelCase
4744
);
4845
if (exportJs) {
4946
exportJs = `module.exports = ${exportJs};`;

lib/processCss.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,20 @@ const Warning = require('./Warning');
1616
const CssSyntaxError = require('./CssSyntaxError');
1717
const { getLocalIdent } = require('./utils');
1818

19-
module.exports = function processCss(inputSource, inputMap, options, callback) {
20-
const { query } = options;
21-
const { context, localIdentRegExp } = query;
22-
const localIdentName = query.localIdentName || '[hash:base64]';
23-
const customGetLocalIdent = query.getLocalIdent || getLocalIdent;
19+
module.exports = function processCss(content, map, options, callback) {
20+
const { loaderContext, loaderOptions } = options;
21+
const localIdentName = loaderOptions.localIdentName || '[hash:base64]';
22+
const customGetLocalIdent = loaderOptions.getLocalIdent || getLocalIdent;
2423

2524
const parserOptions = {
26-
mode: options.mode,
27-
url: query.url !== false,
28-
import: query.import !== false,
29-
resolve: options.resolve,
25+
url: loaderOptions.url !== false,
26+
import: loaderOptions.import !== false,
3027
};
3128

3229
const pipeline = postcss([
3330
modulesValues,
3431
localByDefault({
35-
mode: options.mode,
32+
mode: loaderOptions.modules ? 'local' : 'global',
3633
rewriteUrl(global, url) {
3734
if (parserOptions.url) {
3835
// eslint-disable-next-line no-param-reassign
@@ -59,9 +56,9 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
5956
localIdentName,
6057
exportName,
6158
{
62-
regExp: localIdentRegExp,
63-
hashPrefix: query.hashPrefix || '',
64-
context,
59+
regExp: loaderOptions.localIdentRegExp,
60+
hashPrefix: loaderOptions.hashPrefix || '',
61+
context: loaderOptions.context,
6562
}
6663
);
6764
},
@@ -70,13 +67,19 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
7067
]);
7168

7269
pipeline
73-
.process(inputSource, {
70+
.process(content, {
7471
// we need a prefix to avoid path rewriting of PostCSS
75-
from: `/css-loader!${options.from}`,
76-
to: options.to,
72+
from: `/css-loader!${loaderUtils
73+
.getRemainingRequest(loaderContext)
74+
.split('!')
75+
.pop()}`,
76+
to: loaderUtils
77+
.getCurrentRequest(loaderContext)
78+
.split('!')
79+
.pop(),
7780
map: options.sourceMap
7881
? {
79-
prev: inputMap,
82+
prev: map,
8083
sourcesContent: true,
8184
inline: false,
8285
annotation: false,
@@ -86,9 +89,7 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
8689
.then((result) => {
8790
result
8891
.warnings()
89-
.forEach((warning) =>
90-
options.loaderContext.emitWarning(new Warning(warning))
91-
);
92+
.forEach((warning) => loaderContext.emitWarning(new Warning(warning)));
9293

9394
callback(null, {
9495
source: result.css,

0 commit comments

Comments
 (0)