Skip to content

Commit fed0ab9

Browse files
refactor: process plugins
1 parent 8de6d55 commit fed0ab9

File tree

5 files changed

+46
-128
lines changed

5 files changed

+46
-128
lines changed

src/index.js

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,8 @@ import postcss from 'postcss';
77

88
import Warning from './Warning';
99
import SyntaxError from './Error';
10-
import parseOptions from './options';
1110
import schema from './options.json';
12-
import { exec, loadConfig, createPostCssPlugins } from './utils';
13-
14-
function pluginsToArray(plugins) {
15-
if (typeof plugins === 'undefined') {
16-
return [];
17-
}
18-
19-
if (Array.isArray(plugins)) {
20-
return plugins;
21-
}
22-
23-
return [plugins];
24-
}
11+
import { exec, loadConfig, getArrayPlugins } from './utils';
2512

2613
/**
2714
* **PostCSS Loader**
@@ -92,52 +79,26 @@ export default async function loader(content, sourceMap, meta = {}) {
9279
...loadedConfig,
9380
...options,
9481
plugins: [
95-
...pluginsToArray(loadedConfig.plugins),
96-
...pluginsToArray(options.plugins),
82+
...getArrayPlugins(loadedConfig.plugins, file),
83+
...getArrayPlugins(options.plugins, file),
9784
],
9885
};
9986

100-
let config;
101-
102-
const { length } = Object.keys(mergedOptions).filter((option) => {
103-
switch (option) {
104-
// case 'exec':
105-
// case 'ident':
106-
case 'config':
107-
case 'sourceMap':
108-
return false;
109-
default:
110-
return option;
111-
}
112-
});
87+
const resultPlugins = mergedOptions.plugins;
11388

114-
if (length) {
115-
config = parseOptions.call(this, mergedOptions);
116-
}
89+
const { parser, syntax, stringifier } = mergedOptions;
11790

118-
if (typeof config.options !== 'undefined') {
119-
if (typeof config.options.to !== 'undefined') {
120-
delete config.options.to;
121-
}
122-
123-
if (typeof config.options.from !== 'undefined') {
124-
delete config.options.from;
125-
}
126-
}
127-
128-
const plugins = config.plugins || [];
129-
130-
const postcssOptions = Object.assign(
131-
{
132-
from: file,
133-
map: options.sourceMap
134-
? options.sourceMap === 'inline'
135-
? { inline: true, annotation: false }
136-
: { inline: false, annotation: false }
137-
: false,
138-
},
139-
config.options
140-
);
91+
const postcssOptions = {
92+
from: file,
93+
map: options.sourceMap
94+
? options.sourceMap === 'inline'
95+
? { inline: true, annotation: false }
96+
: { inline: false, annotation: false }
97+
: false,
98+
parser,
99+
syntax,
100+
stringifier,
101+
};
141102

142103
// Loader Exec (Deprecated)
143104
// https://webpack.js.org/api/loaders/#deprecated-context-properties
@@ -181,7 +142,7 @@ export default async function loader(content, sourceMap, meta = {}) {
181142

182143
// Loader API Exec (Deprecated)
183144
// https://webpack.js.org/api/loaders/#deprecated-context-properties
184-
if (config.exec) {
145+
if (mergedOptions.exec) {
185146
// eslint-disable-next-line no-param-reassign
186147
content = exec(content, this);
187148
}
@@ -195,8 +156,6 @@ export default async function loader(content, sourceMap, meta = {}) {
195156
postcssOptions.map.prev = sourceMap;
196157
}
197158

198-
const resultPlugins = createPostCssPlugins(plugins, file);
199-
200159
let result;
201160

202161
try {
@@ -268,9 +227,7 @@ export default async function loader(content, sourceMap, meta = {}) {
268227
* @requires schema-utils
269228
*
270229
* @requires postcss
271-
* @requires postcss-load-config
272230
*
273-
* @requires ./options.js
274231
* @requires ./Warning.js
275232
* @requires ./SyntaxError.js
276233
*/

src/options.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/utils.js

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,7 @@ async function loadConfig(config, context, configPath, loaderContext) {
151151
if (typeof resultConfig === 'function') {
152152
resultConfig = resultConfig(patchedContext);
153153
} else {
154-
resultConfig = Object.assign({}, resultConfig, patchedContext);
155-
}
156-
157-
if (!resultConfig.plugins) {
158-
resultConfig.plugins = [];
154+
resultConfig = { ...resultConfig, ...patchedContext };
159155
}
160156

161157
if (result.filepath) {
@@ -166,45 +162,39 @@ async function loadConfig(config, context, configPath, loaderContext) {
166162
return resultConfig;
167163
}
168164

169-
function createPostCssPlugins(items, file) {
170-
function iterator(plugins, plugin, acc) {
171-
if (typeof plugin === 'undefined') {
172-
return acc;
173-
}
165+
function getPlugin(pluginEntry) {
166+
if (!pluginEntry) {
167+
return [];
168+
}
174169

175-
if (plugin === false) {
176-
return iterator(plugins, plugins.pop(), acc);
177-
}
170+
if (pluginEntry.postcssVersion === postcssPkg.version) {
171+
return [pluginEntry];
172+
}
178173

179-
if (plugin.postcssVersion === postcssPkg.version) {
180-
acc.push(plugin);
181-
return iterator(plugins, plugins.pop(), acc);
182-
}
174+
const result = pluginEntry.call(this, this);
183175

184-
if (typeof plugin === 'function') {
185-
const postcssPlugin = plugin.call(this, this);
176+
return Array.isArray(result) ? result : [result];
177+
}
186178

187-
if (Array.isArray(postcssPlugin)) {
188-
acc.concat(postcssPlugin);
189-
} else {
190-
acc.push(postcssPlugin);
191-
}
179+
function getArrayPlugins(plugins, file) {
180+
if (Array.isArray(plugins)) {
181+
return plugins.reduce((accumulator, plugin) => {
182+
// eslint-disable-next-line no-param-reassign
183+
accumulator = accumulator.concat(getArrayPlugins(plugin));
192184

193-
return iterator(plugins, plugins.pop(), acc);
194-
}
185+
return accumulator;
186+
}, []);
187+
}
195188

196-
if (Object.keys(plugin).length === 0) {
197-
return iterator(plugins, plugins.pop(), acc);
189+
if (typeof plugins === 'object' && typeof plugins !== 'function') {
190+
if (Object.keys(plugins).length === 0) {
191+
return [];
198192
}
199193

200-
const concatPlugins = [...plugins, ...loadPlugins(plugin, file)];
201-
202-
return iterator(concatPlugins, concatPlugins.pop(), acc);
194+
return getArrayPlugins(loadPlugins(plugins, file), file);
203195
}
204196

205-
const pl = [...items];
206-
207-
return iterator(pl, pl.pop(), []);
197+
return getPlugin(plugins);
208198
}
209199

210-
export { exec, loadConfig, createPostCssPlugins };
200+
export { exec, loadConfig, getArrayPlugins };

test/options/__snapshots__/config.test.js.snap

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
exports[`Config Options should emit error when invalid config : errors 1`] = `
44
Array [
55
"ModuleBuildError: Module build failed (from \`replaced original path\`):
6-
Error: No PostCSS Config found in: /test/fixtures/css/invalid.config.js
7-
at loadConfig (/src/utils.js:144:11)",
6+
Error: No PostCSS Config found in: /test/fixtures/css/invalid.config.js",
87
]
98
`;
109

@@ -13,9 +12,7 @@ exports[`Config Options should emit error when invalid config : warnings 1`] = `
1312
exports[`Config Options should emit error when unresolved config : errors 1`] = `
1413
Array [
1514
"ModuleBuildError: Module build failed (from \`replaced original path\`):
16-
Error: No PostCSS Config found in: /test/fixtures/css/unresolve.js
17-
at loadConfig (/src/utils.js:130:11)
18-
at processTicksAndRejections (internal/process/task_queues.js:97:5)",
15+
Error: No PostCSS Config found in: /test/fixtures/css/unresolve.js",
1916
]
2017
`;
2118

test/options/config.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe('Config Options', () => {
220220
const stats = await compile(compiler);
221221

222222
expect(getWarnings(stats)).toMatchSnapshot('warnings');
223-
expect(getErrors(stats)).toMatchSnapshot('errors');
223+
expect(getErrors(stats, true)).toMatchSnapshot('errors');
224224
});
225225

226226
it('should emit error when invalid config ', async () => {
@@ -230,6 +230,6 @@ describe('Config Options', () => {
230230
const stats = await compile(compiler);
231231

232232
expect(getWarnings(stats)).toMatchSnapshot('warnings');
233-
expect(getErrors(stats)).toMatchSnapshot('errors');
233+
expect(getErrors(stats, true)).toMatchSnapshot('errors');
234234
});
235235
});

0 commit comments

Comments
 (0)