@parcel/transformer-babel 2.0.0-nightly.97 → 2.1.0

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/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 nullthrows from 'nullthrows';
8
+ import json5 from 'json5';
8
9
  import path from 'path';
9
- import * as bundledBabelCore from '@babel/core';
10
- import {md5FromObject} from '@parcel/utils';
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 getEnvOptions from './env';
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 = /^\.tsx?/;
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
- if (config.result != null) {
27
- return reload(config, options);
47
+ ): Promise<?BabelConfigResult> {
48
+ // Don't transpile inside node_modules
49
+ if (!config.isSource) {
50
+ return;
28
51
  }
29
52
 
30
- // Don't look for a custom babel config if inside node_modules
31
- if (!config.isSource) {
32
- return buildDefaultBabelConfig(config);
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
- let babelCore = await loadBabelCore(config, options);
36
- let partialConfig = babelCore.loadPartialConfig({
37
- filename: config.searchPath,
38
- cwd: path.dirname(config.searchPath),
39
- root: options.projectRoot,
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
- // loadPartialConfig returns null when the file should explicitly not be run through babel (ignore/exclude)
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
- } else if (partialConfig.hasFilesystemConfig()) {
46
- config.setResult({
47
- internal: false,
48
- config: partialConfig.options,
49
- targets: enginesToBabelTargets(config.env),
50
- });
150
+ }
51
151
 
52
- let {babelrc: babelrcPath, config: configPath} = partialConfig;
53
- let {canBeRehydrated, dependsOnRelative, dependsOnLocal} = getStats(
54
- partialConfig.options,
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
- let configIsJS =
58
- (typeof babelrcPath === 'string' &&
59
- path.extname(babelrcPath) === '.js') ||
60
- (typeof configPath === 'string' && path.extname(configPath) === '.js');
158
+ if (typeof partialConfig.babelrcPath === 'string') {
159
+ addIncludedFile(partialConfig.babelrcPath);
160
+ }
61
161
 
62
- if (configIsJS) {
63
- logger.verbose({
64
- message:
65
- 'WARNING: Using a JavaScript Babel config file means losing out on some caching features of Parcel. Try using a .babelrc file instead.',
66
- });
67
- config.shouldInvalidateOnStartup();
68
- // babel.config.js files get required by @babel/core so there's no use in setting resolved path for watch mode invalidation
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
- if (babelrcPath && (await isExtended(/* babelrcPath */))) {
76
- logger.verbose({
77
- message:
78
- '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.',
79
- });
80
- config.shouldInvalidateOnStartup();
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
- if (dependsOnRelative || dependsOnLocal) {
84
- logger.verbose({
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
- '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.',
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
- if (canBeRehydrated) {
91
- await definePluginDependencies(config);
92
- config.setResultHash(md5FromObject(partialConfig.options));
197
+ config.setCacheKey(JSON.stringify(Date.now()));
198
+ config.invalidateOnStartup();
93
199
  } else {
94
- logger.verbose({
95
- message:
96
- '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.',
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
- await buildDefaultBabelConfig(config);
212
+ return buildDefaultBabelConfig(options, config);
103
213
  }
104
214
  }
105
215
 
106
- async function buildDefaultBabelConfig(config: Config) {
107
- let babelOptions;
108
- if (path.extname(config.searchPath).match(TYPESCRIPT_EXTNAME_RE)) {
109
- babelOptions = getTypescriptOptions(config);
110
- } else {
111
- babelOptions = getFlowOptions(config);
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
- let babelTargets;
115
- let envOptions = await getEnvOptions(config);
116
- if (envOptions != null) {
117
- babelTargets = envOptions.targets;
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
- babelOptions = mergeOptions(babelOptions, await getJSXOptions(config));
121
-
122
- if (babelOptions != null) {
123
- babelOptions.presets = (babelOptions.presets || []).map(preset =>
124
- bundledBabelCore.createConfigItem(preset, {
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
- config.setResult({
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
- targets: babelTargets,
141
- });
142
- await definePluginDependencies(config);
254
+ syntaxPlugins,
255
+ };
143
256
  }
144
257
 
145
- function mergeOptions(result, config?: null | BabelConfig) {
146
- if (
147
- !config ||
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 getStats(options) {
166
- let canBeRehydrated = true;
167
- let dependsOnRelative = false;
168
- let dependsOnLocal = false;
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
- if (!configItem.file) {
174
- canBeRehydrated = false;
175
- } else if (configItem.file.request.startsWith('.')) {
176
- dependsOnRelative = true;
177
- } else if (isLocal(/*configItem.file.resolved*/)) {
178
- dependsOnLocal = true;
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
- function isLocal(/* configItemPath */) {
192
- // TODO: check if realpath is different, need access to fs in case of memory filesystem
193
- return false;
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
- export function preSerialize(config: Config) {
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
- // ConfigItem.value is a function which the v8 serializer chokes on
203
- // It is being ommited here and will be rehydrated later using the path provided by ConfigItem.file
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 configItems = [...babelConfig.presets, ...babelConfig.plugins];
229
- await Promise.all(
230
- configItems.map(async configItem => {
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
- export async function postDeserialize(config: Config, options: PluginOptions) {
242
- let babelCore = config.result.internal
243
- ? bundledBabelCore
244
- : await options.packageManager.require('@babel/core', config.searchPath);
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
- let partialConfig = loadPartialConfig({
278
- filename: config.searchPath,
279
- cwd: path.dirname(config.searchPath),
280
- root: options.projectRoot,
319
+ return true;
281
320
  });
282
321
 
283
- config.setResult({
284
- internal: false,
285
- config: partialConfig.options,
286
- targets: enginesToBabelTargets(config.env),
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 loadBabelCore(config: Config, options: PluginOptions): Promise<any> {
291
- return !config.isSource || config.result?.internal
292
- ? bundledBabelCore
293
- : options.packageManager.require('@babel/core', config.searchPath);
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
  }
@@ -0,0 +1,3 @@
1
+ // @flow strict-local
2
+
3
+ export const BABEL_RANGE = '^7.0.0';