-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
loadConfig.js
150 lines (127 loc) Β· 3.86 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
// @flow
import type {Config, PluginOptions} from '@parcel/types';
import type {PluginLogger} from '@parcel/logger';
import path from 'path';
import loadExternalPlugins from './loadPlugins';
const MODULE_BY_NAME_RE = /\.module\./;
async function configHydrator(
configFile: any,
config: Config,
options: PluginOptions,
) {
// Use a basic, modules-only PostCSS config if the file opts in by a name
// like foo.module.css
if (configFile == null && config.searchPath.match(MODULE_BY_NAME_RE)) {
configFile = {
plugins: {
'postcss-modules': {},
},
};
}
if (configFile == null) {
return;
}
// Load the custom config...
let modulesConfig;
let configFilePlugins = 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,
config.searchPath,
options,
);
config.setResult({
raw: configFile,
hydrated: {
plugins,
from: config.searchPath,
to: config.searchPath,
modules: modulesConfig,
},
});
}
export async function load({
config,
options,
logger,
}: {|
config: Config,
options: PluginOptions,
logger: PluginLogger,
|}): Promise<void> {
let configFile: any = await config.getConfig(
['.postcssrc', '.postcssrc.json', '.postcssrc.js', 'postcss.config.js'],
{packageKey: 'postcss'},
);
let contents = null;
if (configFile) {
contents = configFile.contents;
let isDynamic = configFile && path.extname(configFile.filePath) === '.js';
if (isDynamic) {
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.shouldInvalidateOnStartup();
}
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');
}
let configFilePlugins = Array.isArray(contents.plugins)
? contents.plugins
: Object.keys(contents.plugins);
for (let p of configFilePlugins) {
// JavaScript configs can use an array of functions... opt out of all caching...
if (typeof p === 'function') {
contents.__contains_functions = true;
// This should enforce the config to be revalidated as it can contain functions and is JS
config.shouldInvalidateOnStartup();
config.shouldReload();
}
if (typeof p === 'string') {
if (p.startsWith('.')) {
logger.warn({
message:
'WARNING: Using relative PostCSS plugins means losing out on caching features of Parcel. Bundle this plugin up in a package or use a monorepo to resolve this issue.',
});
config.shouldInvalidateOnStartup();
}
config.addDevDependency(p);
}
}
}
return configHydrator(contents, config, options);
}
export function preSerialize(config: Config) {
if (!config.result) return;
// Ensure we dont pass functions to the serialiser
if (config.result.raw.__contains_functions) {
config.result.raw = {};
}
// This gets re-hydrated in Deserialize, so never store this.
// It also usually contains a bunch of functions so bad idea anyway...
config.result.hydrated = {};
}
export function postDeserialize(
config: Config,
options: PluginOptions,
): Promise<void> {
return configHydrator(config.result.raw, config, options);
}