1 | 'use strict'
|
2 |
|
3 | const req = require('import-cwd')
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const load = (plugin, options, file) => {
|
17 | try {
|
18 | if (
|
19 | options === null ||
|
20 | options === undefined ||
|
21 | Object.keys(options).length === 0
|
22 | ) {
|
23 | return req(plugin)
|
24 | } else {
|
25 | return req(plugin)(options)
|
26 | }
|
27 | } catch (err) {
|
28 | throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
|
29 | }
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | const plugins = (config, file) => {
|
43 | let plugins = []
|
44 |
|
45 | if (Array.isArray(config.plugins)) {
|
46 | plugins = config.plugins.filter(Boolean)
|
47 | } else {
|
48 | plugins = Object.keys(config.plugins)
|
49 | .filter((plugin) => {
|
50 | return config.plugins[plugin] !== false ? plugin : ''
|
51 | })
|
52 | .map((plugin) => {
|
53 | return load(plugin, config.plugins[plugin], file)
|
54 | })
|
55 | }
|
56 |
|
57 | if (plugins.length && plugins.length > 0) {
|
58 | plugins.forEach((plugin, i) => {
|
59 | if (plugin.default) {
|
60 | plugin = plugin.default
|
61 | }
|
62 |
|
63 | if (plugin.postcss === true) {
|
64 | plugin = plugin()
|
65 | } else if (plugin.postcss) {
|
66 | plugin = plugin.postcss
|
67 | }
|
68 |
|
69 | if (
|
70 |
|
71 | !(
|
72 | (typeof plugin === 'object' && Array.isArray(plugin.plugins)) ||
|
73 | (typeof plugin === 'object' && plugin.postcssPlugin) ||
|
74 | (typeof plugin === 'function')
|
75 | )
|
76 | ) {
|
77 | throw new TypeError(`Invalid PostCSS Plugin found at: plugins[${i}]\n\n(@${file})`)
|
78 | }
|
79 | })
|
80 | }
|
81 |
|
82 | return plugins
|
83 | }
|
84 |
|
85 | module.exports = plugins
|