/
config.js
294 lines (257 loc) Β· 8.8 KB
/
config.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// @flow
import type {Config, PluginOptions} from '@parcel/types';
import type {BabelConfig} from './types';
import type {PluginLogger} from '@parcel/logger';
import nullthrows from 'nullthrows';
import path from 'path';
import * as bundledBabelCore from '@babel/core';
import {md5FromObject} from '@parcel/utils';
import getEnvOptions from './env';
import getJSXOptions from './jsx';
import getFlowOptions from './flow';
import getTypescriptOptions from './typescript';
import {enginesToBabelTargets} from './utils';
const TYPESCRIPT_EXTNAME_RE = /^\.tsx?/;
const BABEL_TRANSFORMER_DIR = path.dirname(__dirname);
export async function load(
config: Config,
options: PluginOptions,
logger: PluginLogger,
) {
if (config.result != null) {
return reload(config, options);
}
// Don't look for a custom babel config if inside node_modules
if (!config.isSource) {
return buildDefaultBabelConfig(config);
}
let babelCore = await loadBabelCore(config, options);
let partialConfig = babelCore.loadPartialConfig({
filename: config.searchPath,
cwd: path.dirname(config.searchPath),
root: options.projectRoot,
});
// loadPartialConfig returns null when the file should explicitly not be run through babel (ignore/exclude)
if (partialConfig == null) {
return;
} else if (partialConfig.hasFilesystemConfig()) {
config.setResult({
internal: false,
config: partialConfig.options,
targets: enginesToBabelTargets(config.env),
});
let {babelrc: babelrcPath, config: configPath} = partialConfig;
let {canBeRehydrated, dependsOnRelative, dependsOnLocal} = getStats(
partialConfig.options,
);
let configIsJS =
(typeof babelrcPath === 'string' &&
path.extname(babelrcPath) === '.js') ||
(typeof configPath === 'string' && path.extname(configPath) === '.js');
if (configIsJS) {
logger.verbose({
message:
'WARNING: Using a JavaScript Babel config file means losing out on some caching features of Parcel. Try using a .babelrc file instead.',
});
config.shouldInvalidateOnStartup();
// babel.config.js files get required by @babel/core so there's no use in setting resolved path for watch mode invalidation
} else {
config.setResolvedPath(
typeof babelrcPath === 'string' ? babelrcPath : configPath,
);
}
if (babelrcPath && (await isExtended(/* babelrcPath */))) {
logger.verbose({
message:
'WARNING: You are using `extends` in your Babel config, which means you are losing out on some of the caching features of Parcel. Maybe try using a reusable preset instead.',
});
config.shouldInvalidateOnStartup();
}
if (dependsOnRelative || dependsOnLocal) {
logger.verbose({
message:
'WARNING: It looks like you are using local Babel plugins or presets. You will need to run with the `--no-cache` option in order to pick up changes to these until their containing package versions are bumped.',
});
}
if (canBeRehydrated) {
await definePluginDependencies(config);
config.setResultHash(md5FromObject(partialConfig.options));
} else {
logger.verbose({
message:
'WARNING: You are using `require` to configure Babel plugins or presets. This means Babel transformations cannot be cached and will run on each build. Please use strings to configure Babel instead.',
});
config.setResultHash(JSON.stringify(Date.now()));
config.shouldInvalidateOnStartup();
}
} else {
await buildDefaultBabelConfig(config);
}
}
async function buildDefaultBabelConfig(config: Config) {
let babelOptions;
if (path.extname(config.searchPath).match(TYPESCRIPT_EXTNAME_RE)) {
babelOptions = getTypescriptOptions(config);
} else {
babelOptions = getFlowOptions(config);
}
let babelTargets;
let envOptions = await getEnvOptions(config);
if (envOptions != null) {
babelTargets = envOptions.targets;
babelOptions = mergeOptions(babelOptions, {presets: envOptions.presets});
}
babelOptions = mergeOptions(babelOptions, await getJSXOptions(config));
if (babelOptions != null) {
babelOptions.presets = (babelOptions.presets || []).map(preset =>
bundledBabelCore.createConfigItem(preset, {
type: 'preset',
dirname: BABEL_TRANSFORMER_DIR,
}),
);
babelOptions.plugins = (babelOptions.plugins || []).map(plugin =>
bundledBabelCore.createConfigItem(plugin, {
type: 'plugin',
dirname: BABEL_TRANSFORMER_DIR,
}),
);
}
config.setResult({
internal: true,
config: babelOptions,
targets: babelTargets,
});
await definePluginDependencies(config);
}
function mergeOptions(result, config?: null | BabelConfig) {
if (
!config ||
((!config.presets || config.presets.length === 0) &&
(!config.plugins || config.plugins.length === 0))
) {
return result;
}
let merged = result;
if (merged) {
merged.presets = (merged.presets || []).concat(config.presets || []);
merged.plugins = (merged.plugins || []).concat(config.plugins || []);
} else {
result = config;
}
return result;
}
function getStats(options) {
let canBeRehydrated = true;
let dependsOnRelative = false;
let dependsOnLocal = false;
let configItems = [...options.presets, ...options.plugins];
for (let configItem of configItems) {
if (!configItem.file) {
canBeRehydrated = false;
} else if (configItem.file.request.startsWith('.')) {
dependsOnRelative = true;
} else if (isLocal(/*configItem.file.resolved*/)) {
dependsOnLocal = true;
}
}
return {canBeRehydrated, dependsOnRelative, dependsOnLocal};
}
function isExtended(/* babelrcPath */) {
// TODO: read and parse babelrc and check to see if extends property exists
// need access to fs in case of memory filesystem
return false;
}
function isLocal(/* configItemPath */) {
// TODO: check if realpath is different, need access to fs in case of memory filesystem
return false;
}
export function preSerialize(config: Config) {
let babelConfig = config.result.config;
if (babelConfig == null) {
return;
}
// ConfigItem.value is a function which the v8 serializer chokes on
// It is being ommited here and will be rehydrated later using the path provided by ConfigItem.file
babelConfig.presets = (babelConfig.presets || []).map(
({options, dirname, name, file}) => ({
options,
dirname,
name,
file,
}),
);
babelConfig.plugins = (babelConfig.plugins || []).map(
({options, dirname, name, file}) => ({
options,
dirname,
name,
file,
}),
);
}
async function definePluginDependencies(config) {
let babelConfig = config.result.config;
if (babelConfig == null) {
return;
}
let configItems = [...babelConfig.presets, ...babelConfig.plugins];
await Promise.all(
configItems.map(async configItem => {
let pkg = nullthrows(
await config.getConfigFrom(configItem.file.resolved, ['package.json'], {
parse: true,
}),
);
config.addDevDependency(pkg.name, pkg.version);
}),
);
}
export async function postDeserialize(config: Config, options: PluginOptions) {
let babelCore = config.result.internal
? bundledBabelCore
: await options.packageManager.require('@babel/core', config.searchPath);
config.result.config.presets = await Promise.all(
config.result.config.presets.map(async configItem => {
let value = await options.packageManager.require(
configItem.file.resolved,
config.searchPath,
);
value = value.default ? value.default : value;
return babelCore.createConfigItem([value, configItem.options], {
type: 'preset',
dirname: configItem.dirname,
});
}),
);
config.result.config.plugins = await Promise.all(
config.result.config.plugins.map(async configItem => {
let value = await options.packageManager.require(
configItem.file.resolved,
config.searchPath,
);
value = value.default ? value.default : value;
return babelCore.createConfigItem([value, configItem.options], {
type: 'plugin',
dirname: configItem.dirname,
});
}),
);
}
async function reload(config: Config, options: PluginOptions) {
let {loadPartialConfig} = await loadBabelCore(config, options);
let partialConfig = loadPartialConfig({
filename: config.searchPath,
cwd: path.dirname(config.searchPath),
root: options.projectRoot,
});
config.setResult({
internal: false,
config: partialConfig.options,
targets: enginesToBabelTargets(config.env),
});
}
function loadBabelCore(config: Config, options: PluginOptions): Promise<any> {
return !config.isSource || config.result?.internal
? bundledBabelCore
: options.packageManager.require('@babel/core', config.searchPath);
}