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