@parcel/transformer-babel 2.0.0-nightly.92 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/BabelTransformer.js +107 -47
- package/lib/astConverter.js +11 -3
- package/lib/babel7.js +81 -26
- package/lib/config.js +319 -213
- package/lib/constants.js +8 -0
- package/lib/flow.js +9 -1
- package/lib/jsx.js +30 -43
- package/lib/utils.js +31 -4
- package/package.json +20 -14
- package/src/BabelTransformer.js +44 -40
- package/src/babel7.js +47 -17
- package/src/config.js +364 -224
- package/src/constants.js +3 -0
- package/src/flow.js +19 -2
- package/src/jsx.js +29 -44
- package/src/types.js +0 -7
- package/src/utils.js +4 -2
- package/lib/babel6.js +0 -47
- package/lib/babelErrorUtils.js +0 -16
- package/lib/env.js +0 -63
- package/lib/getBabelTargets.js +0 -86
- package/lib/typescript.js +0 -18
- package/src/babel6.js +0 -52
- package/src/babelErrorUtils.js +0 -29
- package/src/env.js +0 -70
- package/src/getBabelTargets.js +0 -97
- package/src/typescript.js +0 -16
package/src/config.js
CHANGED
|
@@ -1,294 +1,434 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
|
|
3
|
-
import type {Config, PluginOptions} from '@parcel/types';
|
|
3
|
+
import type {Config, PluginOptions, PluginLogger} from '@parcel/types';
|
|
4
|
+
import typeof * as BabelCore from '@babel/core';
|
|
5
|
+
import type {Diagnostic} from '@parcel/diagnostic';
|
|
4
6
|
import type {BabelConfig} from './types';
|
|
5
|
-
import type {PluginLogger} from '@parcel/logger';
|
|
6
7
|
|
|
7
|
-
import
|
|
8
|
+
import json5 from 'json5';
|
|
8
9
|
import path from 'path';
|
|
9
|
-
import * as
|
|
10
|
-
import {
|
|
10
|
+
import * as internalBabelCore from '@babel/core';
|
|
11
|
+
import {hashObject, relativePath, resolveConfig} from '@parcel/utils';
|
|
12
|
+
import {md, generateJSONCodeHighlights} from '@parcel/diagnostic';
|
|
11
13
|
|
|
12
|
-
import
|
|
13
|
-
import getJSXOptions from './jsx';
|
|
14
|
+
import isJSX from './jsx';
|
|
14
15
|
import getFlowOptions from './flow';
|
|
15
|
-
import getTypescriptOptions from './typescript';
|
|
16
16
|
import {enginesToBabelTargets} from './utils';
|
|
17
17
|
|
|
18
|
-
const TYPESCRIPT_EXTNAME_RE =
|
|
18
|
+
const TYPESCRIPT_EXTNAME_RE = /\.tsx?$/;
|
|
19
19
|
const BABEL_TRANSFORMER_DIR = path.dirname(__dirname);
|
|
20
|
+
const JS_EXTNAME_RE = /^\.(js|cjs|mjs)$/;
|
|
21
|
+
const BABEL_CONFIG_FILENAMES = [
|
|
22
|
+
'.babelrc',
|
|
23
|
+
'.babelrc.js',
|
|
24
|
+
'.babelrc.json',
|
|
25
|
+
'.babelrc.cjs',
|
|
26
|
+
'.babelrc.mjs',
|
|
27
|
+
'.babelignore',
|
|
28
|
+
'babel.config.js',
|
|
29
|
+
'babel.config.json',
|
|
30
|
+
'babel.config.mjs',
|
|
31
|
+
'babel.config.cjs',
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const BABEL_CORE_RANGE = '^7.12.0';
|
|
35
|
+
|
|
36
|
+
type BabelConfigResult = {|
|
|
37
|
+
internal: boolean,
|
|
38
|
+
config: BabelConfig,
|
|
39
|
+
targets?: mixed,
|
|
40
|
+
syntaxPlugins?: mixed,
|
|
41
|
+
|};
|
|
20
42
|
|
|
21
43
|
export async function load(
|
|
22
44
|
config: Config,
|
|
23
45
|
options: PluginOptions,
|
|
24
46
|
logger: PluginLogger,
|
|
25
|
-
) {
|
|
26
|
-
|
|
27
|
-
|
|
47
|
+
): Promise<?BabelConfigResult> {
|
|
48
|
+
// Don't transpile inside node_modules
|
|
49
|
+
if (!config.isSource) {
|
|
50
|
+
return;
|
|
28
51
|
}
|
|
29
52
|
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
53
|
+
// Invalidate when any babel config file is added.
|
|
54
|
+
for (let fileName of BABEL_CONFIG_FILENAMES) {
|
|
55
|
+
config.invalidateOnFileCreate({
|
|
56
|
+
fileName,
|
|
57
|
+
aboveFilePath: config.searchPath,
|
|
58
|
+
});
|
|
33
59
|
}
|
|
34
60
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
61
|
+
// Do nothing if we cannot resolve any babel config filenames. Checking using our own
|
|
62
|
+
// config resolution (which is cached) is much faster than relying on babel.
|
|
63
|
+
if (
|
|
64
|
+
!(await resolveConfig(
|
|
65
|
+
options.inputFS,
|
|
66
|
+
config.searchPath,
|
|
67
|
+
BABEL_CONFIG_FILENAMES,
|
|
68
|
+
options.projectRoot,
|
|
69
|
+
))
|
|
70
|
+
) {
|
|
71
|
+
return buildDefaultBabelConfig(options, config);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const babelCore: BabelCore = await options.packageManager.require(
|
|
75
|
+
'@babel/core',
|
|
76
|
+
config.searchPath,
|
|
77
|
+
{
|
|
78
|
+
range: BABEL_CORE_RANGE,
|
|
79
|
+
saveDev: true,
|
|
80
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
config.addDevDependency({
|
|
84
|
+
specifier: '@babel/core',
|
|
85
|
+
resolveFrom: config.searchPath,
|
|
86
|
+
range: BABEL_CORE_RANGE,
|
|
40
87
|
});
|
|
41
88
|
|
|
42
|
-
|
|
89
|
+
config.invalidateOnEnvChange('BABEL_ENV');
|
|
90
|
+
config.invalidateOnEnvChange('NODE_ENV');
|
|
91
|
+
let babelOptions = {
|
|
92
|
+
filename: config.searchPath,
|
|
93
|
+
cwd: options.projectRoot,
|
|
94
|
+
envName:
|
|
95
|
+
options.env.BABEL_ENV ??
|
|
96
|
+
options.env.NODE_ENV ??
|
|
97
|
+
(options.mode === 'production' || options.mode === 'development'
|
|
98
|
+
? options.mode
|
|
99
|
+
: null) ??
|
|
100
|
+
'development',
|
|
101
|
+
showIgnoredFiles: true,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
let partialConfig: ?{|
|
|
105
|
+
[string]: any,
|
|
106
|
+
|} = await babelCore.loadPartialConfigAsync(babelOptions);
|
|
107
|
+
|
|
108
|
+
let addIncludedFile = file => {
|
|
109
|
+
if (JS_EXTNAME_RE.test(path.extname(file))) {
|
|
110
|
+
// We need to invalidate on startup in case the config is non-static,
|
|
111
|
+
// e.g. uses unknown environment variables, reads from the filesystem, etc.
|
|
112
|
+
logger.warn({
|
|
113
|
+
message: `It looks like you're using a JavaScript Babel config file. This means the config cannot be watched for changes, and Babel transformations cannot be cached. You'll need to restart Parcel for changes to this config to take effect. Try using a ${
|
|
114
|
+
path.basename(file, path.extname(file)) + '.json'
|
|
115
|
+
} file instead.`,
|
|
116
|
+
});
|
|
117
|
+
config.invalidateOnStartup();
|
|
118
|
+
|
|
119
|
+
// But also add the config as a dev dependency so we can at least attempt invalidation in watch mode.
|
|
120
|
+
config.addDevDependency({
|
|
121
|
+
specifier: relativePath(options.projectRoot, file),
|
|
122
|
+
resolveFrom: path.join(options.projectRoot, 'index'),
|
|
123
|
+
// Also invalidate @babel/core when the config or a dependency updates.
|
|
124
|
+
// This ensures that the caches in @babel/core are also invalidated.
|
|
125
|
+
additionalInvalidations: [
|
|
126
|
+
{
|
|
127
|
+
specifier: '@babel/core',
|
|
128
|
+
resolveFrom: config.searchPath,
|
|
129
|
+
range: BABEL_CORE_RANGE,
|
|
130
|
+
},
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
} else {
|
|
134
|
+
config.invalidateOnFileChange(file);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
let warnOldVersion = () => {
|
|
139
|
+
logger.warn({
|
|
140
|
+
message:
|
|
141
|
+
'You are using an old version of @babel/core which does not support the necessary features for Parcel to cache and watch babel config files safely. You may need to restart Parcel for config changes to take effect. Please upgrade to @babel/core 7.12.0 or later to resolve this issue.',
|
|
142
|
+
});
|
|
143
|
+
config.invalidateOnStartup();
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Old versions of @babel/core return null from loadPartialConfig when the file should explicitly not be run through babel (ignore/exclude)
|
|
43
147
|
if (partialConfig == null) {
|
|
148
|
+
warnOldVersion();
|
|
44
149
|
return;
|
|
45
|
-
}
|
|
46
|
-
config.setResult({
|
|
47
|
-
internal: false,
|
|
48
|
-
config: partialConfig.options,
|
|
49
|
-
targets: enginesToBabelTargets(config.env),
|
|
50
|
-
});
|
|
150
|
+
}
|
|
51
151
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
)
|
|
152
|
+
if (partialConfig.files == null) {
|
|
153
|
+
// If the files property is missing, we're on an old version of @babel/core.
|
|
154
|
+
// We need to invalidate on startup because we can't properly track dependencies.
|
|
155
|
+
if (partialConfig.hasFilesystemConfig()) {
|
|
156
|
+
warnOldVersion();
|
|
56
157
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
(typeof configPath === 'string' && path.extname(configPath) === '.js');
|
|
158
|
+
if (typeof partialConfig.babelrcPath === 'string') {
|
|
159
|
+
addIncludedFile(partialConfig.babelrcPath);
|
|
160
|
+
}
|
|
61
161
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
} else {
|
|
70
|
-
config.setResolvedPath(
|
|
71
|
-
typeof babelrcPath === 'string' ? babelrcPath : configPath,
|
|
72
|
-
);
|
|
162
|
+
if (typeof partialConfig.configPath === 'string') {
|
|
163
|
+
addIncludedFile(partialConfig.configPath);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
for (let file of partialConfig.files) {
|
|
168
|
+
addIncludedFile(file);
|
|
73
169
|
}
|
|
170
|
+
}
|
|
74
171
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
172
|
+
if (
|
|
173
|
+
partialConfig.fileHandling != null &&
|
|
174
|
+
partialConfig.fileHandling !== 'transpile'
|
|
175
|
+
) {
|
|
176
|
+
return;
|
|
177
|
+
} else if (partialConfig.hasFilesystemConfig()) {
|
|
178
|
+
// Determine what syntax plugins we need to enable
|
|
179
|
+
let syntaxPlugins = [];
|
|
180
|
+
if (TYPESCRIPT_EXTNAME_RE.test(config.searchPath)) {
|
|
181
|
+
syntaxPlugins.push('typescript');
|
|
182
|
+
if (config.searchPath.endsWith('.tsx')) {
|
|
183
|
+
syntaxPlugins.push('jsx');
|
|
184
|
+
}
|
|
185
|
+
} else if (await isJSX(options, config)) {
|
|
186
|
+
syntaxPlugins.push('jsx');
|
|
81
187
|
}
|
|
82
188
|
|
|
83
|
-
|
|
84
|
-
|
|
189
|
+
// If the config has plugins loaded with require(), or inline plugins in the config,
|
|
190
|
+
// we can't cache the result of the compilation because we don't know where they came from.
|
|
191
|
+
if (hasRequire(partialConfig.options)) {
|
|
192
|
+
logger.warn({
|
|
85
193
|
message:
|
|
86
|
-
'
|
|
194
|
+
'It looks like 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.',
|
|
87
195
|
});
|
|
88
|
-
}
|
|
89
196
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
config.setResultHash(md5FromObject(partialConfig.options));
|
|
197
|
+
config.setCacheKey(JSON.stringify(Date.now()));
|
|
198
|
+
config.invalidateOnStartup();
|
|
93
199
|
} else {
|
|
94
|
-
logger
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
});
|
|
98
|
-
config.setResultHash(JSON.stringify(Date.now()));
|
|
99
|
-
config.shouldInvalidateOnStartup();
|
|
200
|
+
await warnOnRedundantPlugins(options.inputFS, partialConfig, logger);
|
|
201
|
+
definePluginDependencies(config, partialConfig.options, options);
|
|
202
|
+
config.setCacheKey(hashObject(partialConfig.options));
|
|
100
203
|
}
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
internal: false,
|
|
207
|
+
config: partialConfig.options,
|
|
208
|
+
targets: enginesToBabelTargets(config.env),
|
|
209
|
+
syntaxPlugins,
|
|
210
|
+
};
|
|
101
211
|
} else {
|
|
102
|
-
|
|
212
|
+
return buildDefaultBabelConfig(options, config);
|
|
103
213
|
}
|
|
104
214
|
}
|
|
105
215
|
|
|
106
|
-
async function buildDefaultBabelConfig(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
216
|
+
async function buildDefaultBabelConfig(
|
|
217
|
+
options: PluginOptions,
|
|
218
|
+
config: Config,
|
|
219
|
+
): Promise<?BabelConfigResult> {
|
|
220
|
+
// If this is a .ts or .tsx file, we don't need to enable flow.
|
|
221
|
+
if (TYPESCRIPT_EXTNAME_RE.test(config.searchPath)) {
|
|
222
|
+
return;
|
|
112
223
|
}
|
|
113
224
|
|
|
114
|
-
|
|
115
|
-
let
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
-
babelOptions = mergeOptions(babelOptions, {presets: envOptions.presets});
|
|
225
|
+
// Detect flow. If not enabled, babel doesn't need to run at all.
|
|
226
|
+
let babelOptions = await getFlowOptions(config, options);
|
|
227
|
+
if (babelOptions == null) {
|
|
228
|
+
return;
|
|
119
229
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
type: 'preset',
|
|
126
|
-
dirname: BABEL_TRANSFORMER_DIR,
|
|
127
|
-
}),
|
|
128
|
-
);
|
|
129
|
-
babelOptions.plugins = (babelOptions.plugins || []).map(plugin =>
|
|
130
|
-
bundledBabelCore.createConfigItem(plugin, {
|
|
131
|
-
type: 'plugin',
|
|
132
|
-
dirname: BABEL_TRANSFORMER_DIR,
|
|
133
|
-
}),
|
|
134
|
-
);
|
|
230
|
+
|
|
231
|
+
// When flow is enabled, we may also need to enable JSX so it parses properly.
|
|
232
|
+
let syntaxPlugins = [];
|
|
233
|
+
if (await isJSX(options, config)) {
|
|
234
|
+
syntaxPlugins.push('jsx');
|
|
135
235
|
}
|
|
136
236
|
|
|
137
|
-
|
|
237
|
+
babelOptions.presets = (babelOptions.presets || []).map(preset =>
|
|
238
|
+
internalBabelCore.createConfigItem(preset, {
|
|
239
|
+
type: 'preset',
|
|
240
|
+
dirname: BABEL_TRANSFORMER_DIR,
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
babelOptions.plugins = (babelOptions.plugins || []).map(plugin =>
|
|
244
|
+
internalBabelCore.createConfigItem(plugin, {
|
|
245
|
+
type: 'plugin',
|
|
246
|
+
dirname: BABEL_TRANSFORMER_DIR,
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
definePluginDependencies(config, babelOptions, options);
|
|
251
|
+
return {
|
|
138
252
|
internal: true,
|
|
139
253
|
config: babelOptions,
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
await definePluginDependencies(config);
|
|
254
|
+
syntaxPlugins,
|
|
255
|
+
};
|
|
143
256
|
}
|
|
144
257
|
|
|
145
|
-
function
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
((!config.presets || config.presets.length === 0) &&
|
|
149
|
-
(!config.plugins || config.plugins.length === 0))
|
|
150
|
-
) {
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
let merged = result;
|
|
155
|
-
if (merged) {
|
|
156
|
-
merged.presets = (merged.presets || []).concat(config.presets || []);
|
|
157
|
-
merged.plugins = (merged.plugins || []).concat(config.plugins || []);
|
|
158
|
-
} else {
|
|
159
|
-
result = config;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return result;
|
|
258
|
+
function hasRequire(options) {
|
|
259
|
+
let configItems = [...options.presets, ...options.plugins];
|
|
260
|
+
return configItems.some(item => !item.file);
|
|
163
261
|
}
|
|
164
262
|
|
|
165
|
-
function
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
let configItems = [...options.presets, ...options.plugins];
|
|
263
|
+
function definePluginDependencies(config, babelConfig: ?BabelConfig, options) {
|
|
264
|
+
if (babelConfig == null) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
171
267
|
|
|
268
|
+
let configItems = [
|
|
269
|
+
...(babelConfig.presets || []),
|
|
270
|
+
...(babelConfig.plugins || []),
|
|
271
|
+
];
|
|
172
272
|
for (let configItem of configItems) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
273
|
+
// FIXME: this uses a relative path from the project root rather than resolving
|
|
274
|
+
// from the config location because configItem.file.request can be a shorthand
|
|
275
|
+
// rather than a full package name.
|
|
276
|
+
config.addDevDependency({
|
|
277
|
+
specifier: relativePath(options.projectRoot, configItem.file.resolved),
|
|
278
|
+
resolveFrom: path.join(options.projectRoot, 'index'),
|
|
279
|
+
// Also invalidate @babel/core when the plugin or a dependency updates.
|
|
280
|
+
// This ensures that the caches in @babel/core are also invalidated.
|
|
281
|
+
additionalInvalidations: [
|
|
282
|
+
{
|
|
283
|
+
specifier: '@babel/core',
|
|
284
|
+
resolveFrom: config.searchPath,
|
|
285
|
+
range: BABEL_CORE_RANGE,
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
});
|
|
180
289
|
}
|
|
181
|
-
|
|
182
|
-
return {canBeRehydrated, dependsOnRelative, dependsOnLocal};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function isExtended(/* babelrcPath */) {
|
|
186
|
-
// TODO: read and parse babelrc and check to see if extends property exists
|
|
187
|
-
// need access to fs in case of memory filesystem
|
|
188
|
-
return false;
|
|
189
290
|
}
|
|
190
291
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
292
|
+
const redundantPresets = new Set([
|
|
293
|
+
'@babel/preset-env',
|
|
294
|
+
'@babel/preset-react',
|
|
295
|
+
'@babel/preset-typescript',
|
|
296
|
+
'@parcel/babel-preset-env',
|
|
297
|
+
]);
|
|
195
298
|
|
|
196
|
-
|
|
197
|
-
let babelConfig = config.result.config;
|
|
299
|
+
async function warnOnRedundantPlugins(fs, babelConfig, logger) {
|
|
198
300
|
if (babelConfig == null) {
|
|
199
301
|
return;
|
|
200
302
|
}
|
|
201
303
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
babelConfig.presets = (babelConfig.presets || []).map(
|
|
205
|
-
({options, dirname, name, file}) => ({
|
|
206
|
-
options,
|
|
207
|
-
dirname,
|
|
208
|
-
name,
|
|
209
|
-
file,
|
|
210
|
-
}),
|
|
211
|
-
);
|
|
212
|
-
babelConfig.plugins = (babelConfig.plugins || []).map(
|
|
213
|
-
({options, dirname, name, file}) => ({
|
|
214
|
-
options,
|
|
215
|
-
dirname,
|
|
216
|
-
name,
|
|
217
|
-
file,
|
|
218
|
-
}),
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
async function definePluginDependencies(config) {
|
|
223
|
-
let babelConfig = config.result.config;
|
|
224
|
-
if (babelConfig == null) {
|
|
304
|
+
let configPath = babelConfig.config ?? babelConfig.babelrc;
|
|
305
|
+
if (!configPath) {
|
|
225
306
|
return;
|
|
226
307
|
}
|
|
227
308
|
|
|
228
|
-
let
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
let pkg = nullthrows(
|
|
232
|
-
await config.getConfigFrom(configItem.file.resolved, ['package.json'], {
|
|
233
|
-
parse: true,
|
|
234
|
-
}),
|
|
235
|
-
);
|
|
236
|
-
config.addDevDependency(pkg.name, pkg.version);
|
|
237
|
-
}),
|
|
238
|
-
);
|
|
239
|
-
}
|
|
309
|
+
let presets = babelConfig.options.presets || [];
|
|
310
|
+
let plugins = babelConfig.options.plugins || [];
|
|
311
|
+
let foundRedundantPresets = new Set();
|
|
240
312
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
config.result.config.presets = await Promise.all(
|
|
247
|
-
config.result.config.presets.map(async configItem => {
|
|
248
|
-
let value = await options.packageManager.require(
|
|
249
|
-
configItem.file.resolved,
|
|
250
|
-
config.searchPath,
|
|
251
|
-
);
|
|
252
|
-
value = value.default ? value.default : value;
|
|
253
|
-
return babelCore.createConfigItem([value, configItem.options], {
|
|
254
|
-
type: 'preset',
|
|
255
|
-
dirname: configItem.dirname,
|
|
256
|
-
});
|
|
257
|
-
}),
|
|
258
|
-
);
|
|
259
|
-
config.result.config.plugins = await Promise.all(
|
|
260
|
-
config.result.config.plugins.map(async configItem => {
|
|
261
|
-
let value = await options.packageManager.require(
|
|
262
|
-
configItem.file.resolved,
|
|
263
|
-
config.searchPath,
|
|
264
|
-
);
|
|
265
|
-
value = value.default ? value.default : value;
|
|
266
|
-
return babelCore.createConfigItem([value, configItem.options], {
|
|
267
|
-
type: 'plugin',
|
|
268
|
-
dirname: configItem.dirname,
|
|
269
|
-
});
|
|
270
|
-
}),
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
async function reload(config: Config, options: PluginOptions) {
|
|
275
|
-
let {loadPartialConfig} = await loadBabelCore(config, options);
|
|
313
|
+
let filteredPresets = presets.filter(preset => {
|
|
314
|
+
if (redundantPresets.has(preset.file.request)) {
|
|
315
|
+
foundRedundantPresets.add(preset.file.request);
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
276
318
|
|
|
277
|
-
|
|
278
|
-
filename: config.searchPath,
|
|
279
|
-
cwd: path.dirname(config.searchPath),
|
|
280
|
-
root: options.projectRoot,
|
|
319
|
+
return true;
|
|
281
320
|
});
|
|
282
321
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
322
|
+
let filePath = path.relative(process.cwd(), configPath);
|
|
323
|
+
let diagnostics: Array<Diagnostic> = [];
|
|
324
|
+
|
|
325
|
+
if (
|
|
326
|
+
filteredPresets.length === 0 &&
|
|
327
|
+
foundRedundantPresets.size > 0 &&
|
|
328
|
+
plugins.length === 0
|
|
329
|
+
) {
|
|
330
|
+
diagnostics.push({
|
|
331
|
+
message: md`Parcel includes transpilation by default. Babel config __${filePath}__ contains only redundant presets. Deleting it may significantly improve build performance.`,
|
|
332
|
+
codeFrames: [
|
|
333
|
+
{
|
|
334
|
+
filePath: configPath,
|
|
335
|
+
codeHighlights: await getCodeHighlights(
|
|
336
|
+
fs,
|
|
337
|
+
configPath,
|
|
338
|
+
foundRedundantPresets,
|
|
339
|
+
),
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
hints: [md`Delete __${filePath}__`],
|
|
343
|
+
documentationURL:
|
|
344
|
+
'https://parceljs.org/languages/javascript/#default-presets',
|
|
345
|
+
});
|
|
346
|
+
} else if (foundRedundantPresets.size > 0) {
|
|
347
|
+
diagnostics.push({
|
|
348
|
+
message: md`Parcel includes transpilation by default. Babel config __${filePath}__ includes the following redundant presets: ${[
|
|
349
|
+
...foundRedundantPresets,
|
|
350
|
+
].map(p =>
|
|
351
|
+
md.underline(p),
|
|
352
|
+
)}. Removing these may improve build performance.`,
|
|
353
|
+
codeFrames: [
|
|
354
|
+
{
|
|
355
|
+
filePath: configPath,
|
|
356
|
+
codeHighlights: await getCodeHighlights(
|
|
357
|
+
fs,
|
|
358
|
+
configPath,
|
|
359
|
+
foundRedundantPresets,
|
|
360
|
+
),
|
|
361
|
+
},
|
|
362
|
+
],
|
|
363
|
+
hints: [md`Remove the above presets from __${filePath}__`],
|
|
364
|
+
documentationURL:
|
|
365
|
+
'https://parceljs.org/languages/javascript/#default-presets',
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (foundRedundantPresets.has('@babel/preset-env')) {
|
|
370
|
+
diagnostics.push({
|
|
371
|
+
message:
|
|
372
|
+
"@babel/preset-env does not support Parcel's targets, which will likely result in unnecessary transpilation and larger bundle sizes.",
|
|
373
|
+
codeFrames: [
|
|
374
|
+
{
|
|
375
|
+
filePath: babelConfig.config ?? babelConfig.babelrc,
|
|
376
|
+
codeHighlights: await getCodeHighlights(
|
|
377
|
+
fs,
|
|
378
|
+
babelConfig.config ?? babelConfig.babelrc,
|
|
379
|
+
new Set(['@babel/preset-env']),
|
|
380
|
+
),
|
|
381
|
+
},
|
|
382
|
+
],
|
|
383
|
+
hints: [
|
|
384
|
+
`Either remove __@babel/preset-env__ to use Parcel's builtin transpilation, or replace with __@parcel/babel-preset-env__`,
|
|
385
|
+
],
|
|
386
|
+
documentationURL:
|
|
387
|
+
'https://parceljs.org/languages/javascript/#custom-plugins',
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (diagnostics.length > 0) {
|
|
392
|
+
logger.warn(diagnostics);
|
|
393
|
+
}
|
|
288
394
|
}
|
|
289
395
|
|
|
290
|
-
function
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
396
|
+
async function getCodeHighlights(fs, filePath, redundantPresets) {
|
|
397
|
+
let ext = path.extname(filePath);
|
|
398
|
+
if (ext !== '.js' && ext !== '.cjs' && ext !== '.mjs') {
|
|
399
|
+
let contents = await fs.readFile(filePath, 'utf8');
|
|
400
|
+
let json = json5.parse(contents);
|
|
401
|
+
|
|
402
|
+
let presets = json.presets || [];
|
|
403
|
+
let pointers = [];
|
|
404
|
+
for (let i = 0; i < presets.length; i++) {
|
|
405
|
+
if (Array.isArray(presets[i]) && redundantPresets.has(presets[i][0])) {
|
|
406
|
+
pointers.push({type: 'value', key: `/presets/${i}/0`});
|
|
407
|
+
} else if (redundantPresets.has(presets[i])) {
|
|
408
|
+
pointers.push({type: 'value', key: `/presets/${i}`});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (pointers.length > 0) {
|
|
413
|
+
try {
|
|
414
|
+
return generateJSONCodeHighlights(contents, pointers);
|
|
415
|
+
} catch {
|
|
416
|
+
// TODO: support code highlights for json5 sources.
|
|
417
|
+
// Babel supports json5 syntax, but json-source-map does not.
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return [
|
|
423
|
+
{
|
|
424
|
+
start: {
|
|
425
|
+
line: 1,
|
|
426
|
+
column: 1,
|
|
427
|
+
},
|
|
428
|
+
end: {
|
|
429
|
+
line: 1,
|
|
430
|
+
column: 1,
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
];
|
|
294
434
|
}
|
package/src/constants.js
ADDED