Skip to content

Commit e662b61

Browse files
refactor: code (#1003)
1 parent 77e705c commit e662b61

5 files changed

+213
-277
lines changed

src/index.js

+39-48
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
getImportCode,
2020
getModuleCode,
2121
getExportCode,
22-
prepareCode,
2322
} from './utils';
2423
import Warning from './Warning';
2524
import CssSyntaxError from './CssSyntaxError';
@@ -58,19 +57,11 @@ export default function loader(content, map, meta) {
5857
// Run other loader (`postcss-loader`, `sass-loader` and etc) for importing CSS
5958
const importPrefix = getImportPrefix(this, options.importLoaders);
6059

61-
plugins.push(
62-
icssParser({
63-
loaderContext: this,
64-
importPrefix,
65-
localsConvention: options.localsConvention,
66-
})
67-
);
60+
plugins.push(icssParser());
6861

6962
if (options.import !== false) {
7063
plugins.push(
7164
importParser({
72-
loaderContext: this,
73-
importPrefix,
7465
filter: getFilter(options.import, this.resourcePath),
7566
})
7667
);
@@ -79,7 +70,6 @@ export default function loader(content, map, meta) {
7970
if (options.url !== false) {
8071
plugins.push(
8172
urlParser({
82-
loaderContext: this,
8373
filter: getFilter(options.url, this.resourcePath, (value) =>
8474
isUrlRequest(value)
8575
),
@@ -92,54 +82,55 @@ export default function loader(content, map, meta) {
9282
from: this.remainingRequest.split('!').pop(),
9383
to: this.currentRequest.split('!').pop(),
9484
map: options.sourceMap
95-
? {
96-
prev: map,
97-
inline: false,
98-
annotation: false,
99-
}
100-
: null,
85+
? { prev: map, inline: false, annotation: false }
86+
: false,
10187
})
10288
.then((result) => {
10389
result
10490
.warnings()
10591
.forEach((warning) => this.emitWarning(new Warning(warning)));
10692

107-
if (!result.messages) {
108-
// eslint-disable-next-line no-param-reassign
109-
result.messages = [];
93+
const imports = [];
94+
const exports = [];
95+
const replacers = [];
96+
97+
for (const message of result.messages) {
98+
// eslint-disable-next-line default-case
99+
switch (message.type) {
100+
case 'import':
101+
imports.push(message.value);
102+
break;
103+
case 'export':
104+
exports.push(message.value);
105+
break;
106+
case 'replacer':
107+
replacers.push(message.value);
108+
break;
109+
}
110110
}
111111

112-
const { onlyLocals } = options;
113-
114-
const importItems = result.messages
115-
.filter((message) => (message.type === 'import' ? message : false))
116-
.reduce((accumulator, currentValue) => {
117-
accumulator.push(currentValue.import);
118-
119-
return accumulator;
120-
}, []);
121-
const exportItems = result.messages
122-
.filter((message) => (message.type === 'export' ? message : false))
123-
.reduce((accumulator, currentValue) => {
124-
accumulator.push(currentValue.export);
125-
126-
return accumulator;
127-
}, []);
128-
129-
const importCode = getImportCode(importItems, onlyLocals);
130-
const moduleCode = getModuleCode(result, sourceMap, onlyLocals);
131-
const exportCode = getExportCode(exportItems, onlyLocals);
132-
const apiCode = getApiCode(this, sourceMap, onlyLocals);
112+
const isNormalMode = !options.onlyLocals;
113+
114+
const apiCode = isNormalMode ? getApiCode(this, sourceMap) : '';
115+
const importCode =
116+
isNormalMode && imports.length > 0
117+
? getImportCode(this, imports, { importPrefix })
118+
: '';
119+
const moduleCode = isNormalMode
120+
? getModuleCode(this, result, replacers, { sourceMap, importPrefix })
121+
: '';
122+
const exportCode =
123+
exports.length > 0
124+
? getExportCode(this, exports, replacers, {
125+
importPrefix,
126+
localsConvention: options.localsConvention,
127+
onlyLocals: options.onlyLocals,
128+
})
129+
: '';
133130

134131
return callback(
135132
null,
136-
prepareCode(
137-
{ apiCode, importCode, moduleCode, exportCode },
138-
result.messages,
139-
this,
140-
importPrefix,
141-
onlyLocals
142-
)
133+
[apiCode, importCode, moduleCode, exportCode].join('')
143134
);
144135
})
145136
.catch((error) => {

src/plugins/postcss-icss-parser.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ import postcss from 'postcss';
22
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
33
import loaderUtils from 'loader-utils';
44

5-
import { getExportItemCode, getImportItemCode } from '../utils';
6-
75
const pluginName = 'postcss-icss-parser';
86

97
function hasImportMessage(messages, url) {
108
return messages.find(
119
(message) =>
1210
message.pluginName === pluginName &&
1311
message.type === 'import' &&
14-
message.item.url === url &&
15-
message.item.media === ''
12+
message.value &&
13+
message.value.url === url &&
14+
message.value.media === ''
1615
);
1716
}
1817

1918
export default postcss.plugin(
2019
pluginName,
21-
(options = {}) =>
20+
() =>
2221
function process(css, result) {
2322
const importReplacements = Object.create(null);
2423
const { icssImports, icssExports } = extractICSS(css);
@@ -29,28 +28,27 @@ export default postcss.plugin(
2928
const url = loaderUtils.parseString(importUrl);
3029

3130
for (const token of Object.keys(icssImports[importUrl])) {
31+
const name = `___CSS_LOADER_IMPORT___${index}___`;
32+
3233
index += 1;
33-
importReplacements[token] = `___CSS_LOADER_IMPORT___${index}___`;
34+
importReplacements[token] = name;
3435

3536
result.messages.push({
3637
pluginName,
37-
type: 'icss-import',
38-
item: { url, export: icssImports[importUrl][token], index },
38+
type: 'replacer',
39+
value: {
40+
type: 'icss-import',
41+
name,
42+
url,
43+
export: icssImports[importUrl][token],
44+
},
3945
});
4046

4147
if (!hasImportMessage(result.messages, url)) {
42-
const media = '';
43-
const { loaderContext, importPrefix } = options;
44-
4548
result.messages.push({
4649
pluginName,
4750
type: 'import',
48-
import: getImportItemCode(
49-
{ url, media },
50-
loaderContext,
51-
importPrefix
52-
),
53-
item: { url, media },
51+
value: { type: 'icss-import', url, media: '', name },
5452
});
5553
}
5654
}
@@ -67,9 +65,8 @@ export default postcss.plugin(
6765

6866
result.messages.push({
6967
pluginName,
70-
export: getExportItemCode(name, value, options.localsConvention),
7168
type: 'export',
72-
item: { name, value },
69+
value: { name, value },
7370
});
7471
}
7572
}

src/plugins/postcss-import-parser.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import postcss from 'postcss';
22
import valueParser from 'postcss-value-parser';
33

4-
import { uniqWith, getImportItemCode } from '../utils';
4+
import { uniqWith } from '../utils';
55

66
const pluginName = 'postcss-import-parser';
77

@@ -88,7 +88,7 @@ function walkAtRules(css, result, filter) {
8888

8989
export default postcss.plugin(
9090
pluginName,
91-
(options = {}) =>
91+
(options) =>
9292
function process(css, result) {
9393
const traversed = walkAtRules(css, result, options.filter);
9494
const paths = uniqWith(
@@ -100,11 +100,7 @@ export default postcss.plugin(
100100
result.messages.push({
101101
pluginName,
102102
type: 'import',
103-
import: getImportItemCode(
104-
item,
105-
options.loaderContext,
106-
options.importPrefix
107-
),
103+
value: { type: '@import', url: item.url, media: item.media },
108104
});
109105
});
110106
}

src/plugins/postcss-url-parser.js

+16-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import postcss from 'postcss';
22
import valueParser from 'postcss-value-parser';
33

4-
import { uniqWith, flatten, getUrlHelperCode, getUrlItemCode } from '../utils';
4+
import { uniqWith, flatten } from '../utils';
55

66
const pluginName = 'postcss-url-parser';
77

@@ -106,7 +106,7 @@ function walkDeclsWithUrl(css, result, filter) {
106106

107107
export default postcss.plugin(
108108
pluginName,
109-
(options = {}) =>
109+
(options) =>
110110
function process(css, result) {
111111
const traversed = walkDeclsWithUrl(css, result, options.filter);
112112
const paths = uniqWith(
@@ -121,36 +121,24 @@ export default postcss.plugin(
121121

122122
const placeholders = [];
123123

124-
let hasUrlHelper = false;
125-
126124
paths.forEach((path, index) => {
127-
const { loaderContext } = options;
128-
const placeholder = `___CSS_LOADER_URL___${index}___`;
125+
const name = `___CSS_LOADER_URL___${index}___`;
129126
const { url, needQuotes } = path;
130127

131-
placeholders.push({ placeholder, path });
128+
placeholders.push({ name, path });
132129

133-
if (!hasUrlHelper) {
134-
result.messages.push({
130+
result.messages.push(
131+
{
135132
pluginName,
136133
type: 'import',
137-
import: getUrlHelperCode(loaderContext),
138-
});
139-
140-
// eslint-disable-next-line no-param-reassign
141-
hasUrlHelper = true;
142-
}
143-
144-
result.messages.push({
145-
pluginName,
146-
type: 'import',
147-
import: getUrlItemCode(
148-
{ url, placeholder, needQuotes },
149-
loaderContext
150-
),
151-
importType: 'url',
152-
placeholder,
153-
});
134+
value: { type: 'url', url, name, needQuotes },
135+
},
136+
{
137+
pluginName,
138+
type: 'replacer',
139+
value: { type: 'url', name },
140+
}
141+
);
154142
});
155143

156144
traversed.forEach((item) => {
@@ -165,12 +153,12 @@ export default postcss.plugin(
165153
return;
166154
}
167155

168-
const { placeholder } = value;
156+
const { name } = value;
169157

170158
// eslint-disable-next-line no-param-reassign
171159
node.type = 'word';
172160
// eslint-disable-next-line no-param-reassign
173-
node.value = placeholder;
161+
node.value = name;
174162
});
175163

176164
// eslint-disable-next-line no-param-reassign

0 commit comments

Comments
 (0)