1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _path = _interopRequireDefault(require("path"));
|
---|
9 |
|
---|
10 | var _postcss = _interopRequireDefault(require("postcss"));
|
---|
11 |
|
---|
12 | var _yaml = _interopRequireDefault(require("yaml"));
|
---|
13 |
|
---|
14 | var _lilconfig = require("lilconfig");
|
---|
15 |
|
---|
16 | var _isResolvable = _interopRequireDefault(require("is-resolvable"));
|
---|
17 |
|
---|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
19 |
|
---|
20 | const cssnano = 'cssnano';
|
---|
21 | /*
|
---|
22 | * preset can be one of four possibilities:
|
---|
23 | * preset = 'default'
|
---|
24 | * preset = ['default', {}]
|
---|
25 | * preset = function <- to be invoked
|
---|
26 | * preset = {plugins: []} <- already invoked function
|
---|
27 | */
|
---|
28 |
|
---|
29 | function resolvePreset(preset) {
|
---|
30 | let fn, options;
|
---|
31 |
|
---|
32 | if (Array.isArray(preset)) {
|
---|
33 | fn = preset[0];
|
---|
34 | options = preset[1];
|
---|
35 | } else {
|
---|
36 | fn = preset;
|
---|
37 | options = {};
|
---|
38 | } // For JS setups where we invoked the preset already
|
---|
39 |
|
---|
40 |
|
---|
41 | if (preset.plugins) {
|
---|
42 | return preset.plugins;
|
---|
43 | } // Provide an alias for the default preset, as it is built-in.
|
---|
44 |
|
---|
45 |
|
---|
46 | if (fn === 'default') {
|
---|
47 | return require('cssnano-preset-default')(options).plugins;
|
---|
48 | } // For non-JS setups; we'll need to invoke the preset ourselves.
|
---|
49 |
|
---|
50 |
|
---|
51 | if (typeof fn === 'function') {
|
---|
52 | return fn(options).plugins;
|
---|
53 | } // Try loading a preset from node_modules
|
---|
54 |
|
---|
55 |
|
---|
56 | if ((0, _isResolvable.default)(fn)) {
|
---|
57 | return require(fn)(options).plugins;
|
---|
58 | }
|
---|
59 |
|
---|
60 | const sugar = `cssnano-preset-${fn}`; // Try loading a preset from node_modules (sugar)
|
---|
61 |
|
---|
62 | if ((0, _isResolvable.default)(sugar)) {
|
---|
63 | return require(sugar)(options).plugins;
|
---|
64 | } // If all else fails, we probably have a typo in the config somewhere
|
---|
65 |
|
---|
66 |
|
---|
67 | throw new Error(`Cannot load preset "${fn}". Please check your configuration for errors and try again.`);
|
---|
68 | }
|
---|
69 | /*
|
---|
70 | * cssnano will look for configuration firstly as options passed
|
---|
71 | * directly to it, and failing this it will use lilconfig to
|
---|
72 | * load an external file.
|
---|
73 | */
|
---|
74 |
|
---|
75 |
|
---|
76 | function resolveConfig(options) {
|
---|
77 | if (options.preset) {
|
---|
78 | return resolvePreset(options.preset);
|
---|
79 | }
|
---|
80 |
|
---|
81 | let searchPath = process.cwd();
|
---|
82 | let configPath = null;
|
---|
83 |
|
---|
84 | if (options.configFile) {
|
---|
85 | searchPath = null;
|
---|
86 | configPath = _path.default.resolve(process.cwd(), options.configFile);
|
---|
87 | }
|
---|
88 |
|
---|
89 | const configExplorer = (0, _lilconfig.lilconfigSync)(cssnano, {
|
---|
90 | searchPlaces: ['package.json', '.cssnanorc', '.cssnanorc.json', '.cssnanorc.yaml', '.cssnanorc.yml', '.cssnanorc.js', 'cssnano.config.js'],
|
---|
91 | loaders: {
|
---|
92 | '.yaml': (filepath, content) => _yaml.default.parse(content),
|
---|
93 | '.yml': (filepath, content) => _yaml.default.parse(content)
|
---|
94 | }
|
---|
95 | });
|
---|
96 | const config = configPath ? configExplorer.load(configPath) : configExplorer.search(searchPath);
|
---|
97 |
|
---|
98 | if (config === null) {
|
---|
99 | return resolvePreset('default');
|
---|
100 | }
|
---|
101 |
|
---|
102 | return resolvePreset(config.config.preset || config.config);
|
---|
103 | }
|
---|
104 |
|
---|
105 | const cssnanoPlugin = (options = {}) => {
|
---|
106 | if (Array.isArray(options.plugins)) {
|
---|
107 | if (!options.preset || !options.preset.plugins) {
|
---|
108 | options.preset = {
|
---|
109 | plugins: []
|
---|
110 | };
|
---|
111 | }
|
---|
112 |
|
---|
113 | options.plugins.forEach(plugin => {
|
---|
114 | if (Array.isArray(plugin)) {
|
---|
115 | const [pluginDef, opts = {}] = plugin;
|
---|
116 |
|
---|
117 | if (typeof pluginDef === 'string' && (0, _isResolvable.default)(pluginDef)) {
|
---|
118 | options.preset.plugins.push([require(pluginDef), opts]);
|
---|
119 | } else {
|
---|
120 | options.preset.plugins.push([pluginDef, opts]);
|
---|
121 | }
|
---|
122 | } else if (typeof plugin === 'string' && (0, _isResolvable.default)(plugin)) {
|
---|
123 | options.preset.plugins.push([require(plugin), {}]);
|
---|
124 | } else {
|
---|
125 | options.preset.plugins.push([plugin, {}]);
|
---|
126 | }
|
---|
127 | });
|
---|
128 | }
|
---|
129 |
|
---|
130 | const plugins = [];
|
---|
131 | const nanoPlugins = resolveConfig(options);
|
---|
132 |
|
---|
133 | for (const nanoPlugin of nanoPlugins) {
|
---|
134 | if (Array.isArray(nanoPlugin)) {
|
---|
135 | const [processor, opts] = nanoPlugin;
|
---|
136 |
|
---|
137 | if (typeof opts === 'undefined' || typeof opts === 'object' && !opts.exclude || typeof opts === 'boolean' && opts === true) {
|
---|
138 | plugins.push(processor(opts));
|
---|
139 | }
|
---|
140 | } else {
|
---|
141 | plugins.push(nanoPlugin);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | return (0, _postcss.default)(plugins);
|
---|
146 | };
|
---|
147 |
|
---|
148 | cssnanoPlugin.postcss = true;
|
---|
149 | var _default = cssnanoPlugin;
|
---|
150 | exports.default = _default;
|
---|
151 | module.exports = exports.default; |
---|