forked from parcel-bundler/parcel
/
loadConfig.js
225 lines (205 loc) Β· 5.79 KB
/
loadConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// @flow
import type {
Config,
FilePath,
PluginOptions,
PluginLogger,
} from '@parcel/types';
import path from 'path';
import {relativePath} from '@parcel/utils';
import {md, generateJSONCodeHighlights} from '@parcel/diagnostic';
import nullthrows from 'nullthrows';
import clone from 'clone';
import {POSTCSS_RANGE} from './constants';
import loadExternalPlugins from './loadPlugins';
type ConfigResult = {|
raw: any,
filePath: string,
hydrated: {|
plugins: Array<any>,
from: FilePath,
to: FilePath,
modules: any,
|},
|};
async function configHydrator(
configFile: any,
config: Config,
resolveFrom: FilePath,
options: PluginOptions,
logger: PluginLogger,
): Promise<?ConfigResult> {
if (configFile == null) {
return;
}
// Load the custom config...
let modulesConfig;
let configFilePlugins = clone(configFile.plugins);
if (
configFilePlugins != null &&
typeof configFilePlugins === 'object' &&
configFilePlugins['postcss-modules'] != null
) {
modulesConfig = configFilePlugins['postcss-modules'];
delete configFilePlugins['postcss-modules'];
}
if (!modulesConfig && configFile.modules) {
modulesConfig = {};
}
let plugins = await loadExternalPlugins(
configFilePlugins,
nullthrows(resolveFrom),
options,
);
// contents is either:
// from JSON: { plugins: { 'postcss-foo': { ...opts } } }
// from JS (v8): { plugins: [ { postcssPlugin: 'postcss-foo', ...visitor callback functions } ]
// from JS (v7): { plugins: [ [Function: ...] ]
let pluginArray = Array.isArray(configFilePlugins)
? configFilePlugins
: Object.keys(configFilePlugins);
for (let p of pluginArray) {
if (typeof p === 'string') {
config.addDevDependency({
specifier: p,
resolveFrom: nullthrows(resolveFrom),
});
}
}
let redundantPlugins = pluginArray.filter(
p => p === 'autoprefixer' || p === 'postcss-preset-env',
);
if (redundantPlugins.length > 0) {
let filename = path.basename(resolveFrom);
let isPackageJson = filename === 'package.json';
let message;
let hints = [];
if (!isPackageJson && redundantPlugins.length === pluginArray.length) {
message = md`Parcel includes CSS transpilation and vendor prefixing by default. PostCSS config __${filename}__ contains only redundant plugins. Deleting it may significantly improve build performance.`;
hints.push(md`Delete __${filename}__`);
} else {
message = md`Parcel includes CSS transpilation and vendor prefixing by default. PostCSS config __${filename}__ contains the following redundant plugins: ${[
...redundantPlugins,
].map(p =>
md.underline(p),
)}. Removing these may improve build performance.`;
hints.push(md`Remove the above plugins from __${filename}__`);
}
let codeFrames;
if (path.extname(filename) !== '.js') {
let contents = await options.inputFS.readFile(resolveFrom, 'utf8');
let prefix = isPackageJson ? '/postcss' : '';
codeFrames = [
{
language: 'json',
filePath: resolveFrom,
code: contents,
codeHighlights: generateJSONCodeHighlights(
contents,
redundantPlugins.map(plugin => ({
key: `${prefix}/plugins/${plugin}`,
type: 'key',
})),
),
},
];
} else {
codeFrames = [
{
filePath: resolveFrom,
codeHighlights: [
{
start: {line: 1, column: 1},
end: {line: 1, column: 1},
},
],
},
];
}
logger.warn({
message,
hints,
documentationURL: 'https://parceljs.org/languages/css/#default-plugins',
codeFrames,
});
}
return {
raw: configFile,
filePath: resolveFrom,
hydrated: {
plugins,
from: config.searchPath,
to: config.searchPath,
modules: modulesConfig,
},
};
}
export async function load({
config,
options,
logger,
}: {|
config: Config,
options: PluginOptions,
logger: PluginLogger,
|}): Promise<?ConfigResult> {
if (!config.isSource) {
return;
}
let configFile: any = await config.getConfig(
[
'.postcssrc',
'.postcssrc.json',
'.postcssrc.js',
'.postcssrc.cjs',
'postcss.config.js',
'postcss.config.cjs',
],
{packageKey: 'postcss'},
);
let contents = null;
if (configFile) {
config.addDevDependency({
specifier: 'postcss',
resolveFrom: config.searchPath,
range: POSTCSS_RANGE,
});
contents = configFile.contents;
let isDynamic =
configFile && path.extname(configFile.filePath).endsWith('js');
if (isDynamic) {
// We have to invalidate on startup in case the config is non-deterministic,
// e.g. using unknown environment variables, reading from the filesystem, etc.
logger.warn({
message:
'WARNING: Using a JavaScript PostCSS config file means losing out on caching features of Parcel. Use a .postcssrc(.json) file whenever possible.',
});
config.invalidateOnStartup();
// Also add the config as a dev dependency so we attempt to reload in watch mode.
config.addDevDependency({
specifier: relativePath(
path.dirname(config.searchPath),
configFile.filePath,
),
resolveFrom: config.searchPath,
});
}
if (typeof contents !== 'object') {
throw new Error('PostCSS config should be an object.');
}
if (
contents.plugins == null ||
typeof contents.plugins !== 'object' ||
Object.keys(contents.plugins).length === 0
) {
throw new Error('PostCSS config must have plugins');
}
}
return configHydrator(
contents,
config,
configFile?.filePath,
options,
logger,
);
}