@parcel/transformer-babel 2.0.0-rc.0 → 2.1.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/config.js CHANGED
@@ -5,6 +5,16 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.load = load;
7
7
 
8
+ function _json() {
9
+ const data = _interopRequireDefault(require("json5"));
10
+
11
+ _json = function () {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
8
18
  function _path() {
9
19
  const data = _interopRequireDefault(require("path"));
10
20
 
@@ -35,6 +45,16 @@ function _utils() {
35
45
  return data;
36
46
  }
37
47
 
48
+ function _diagnostic() {
49
+ const data = require("@parcel/diagnostic");
50
+
51
+ _diagnostic = function () {
52
+ return data;
53
+ };
54
+
55
+ return data;
56
+ }
57
+
38
58
  var _jsx = _interopRequireDefault(require("./jsx"));
39
59
 
40
60
  var _flow = _interopRequireDefault(require("./flow"));
@@ -178,6 +198,7 @@ async function load(config, options, logger) {
178
198
  config.setCacheKey(JSON.stringify(Date.now()));
179
199
  config.invalidateOnStartup();
180
200
  } else {
201
+ await warnOnRedundantPlugins(options.inputFS, partialConfig, logger);
181
202
  definePluginDependencies(config, partialConfig.options, options);
182
203
  config.setCacheKey((0, _utils().hashObject)(partialConfig.options));
183
204
  }
@@ -257,4 +278,122 @@ function definePluginDependencies(config, babelConfig, options) {
257
278
  }]
258
279
  });
259
280
  }
281
+ }
282
+
283
+ const redundantPresets = new Set(['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript', '@parcel/babel-preset-env']);
284
+
285
+ async function warnOnRedundantPlugins(fs, babelConfig, logger) {
286
+ var _babelConfig$config;
287
+
288
+ if (babelConfig == null) {
289
+ return;
290
+ }
291
+
292
+ let configPath = (_babelConfig$config = babelConfig.config) !== null && _babelConfig$config !== void 0 ? _babelConfig$config : babelConfig.babelrc;
293
+
294
+ if (!configPath) {
295
+ return;
296
+ }
297
+
298
+ let presets = babelConfig.options.presets || [];
299
+ let plugins = babelConfig.options.plugins || [];
300
+ let foundRedundantPresets = new Set();
301
+ let filteredPresets = presets.filter(preset => {
302
+ if (redundantPresets.has(preset.file.request)) {
303
+ foundRedundantPresets.add(preset.file.request);
304
+ return false;
305
+ }
306
+
307
+ return true;
308
+ });
309
+
310
+ let filePath = _path().default.relative(process.cwd(), configPath);
311
+
312
+ let diagnostics = [];
313
+
314
+ if (filteredPresets.length === 0 && foundRedundantPresets.size > 0 && plugins.length === 0) {
315
+ diagnostics.push({
316
+ message: (0, _diagnostic().md)`Parcel includes transpilation by default. Babel config __${filePath}__ contains only redundant presets. Deleting it may significantly improve build performance.`,
317
+ codeFrames: [{
318
+ filePath: configPath,
319
+ codeHighlights: await getCodeHighlights(fs, configPath, foundRedundantPresets)
320
+ }],
321
+ hints: [(0, _diagnostic().md)`Delete __${filePath}__`],
322
+ documentationURL: 'https://parceljs.org/languages/javascript/#default-presets'
323
+ });
324
+ } else if (foundRedundantPresets.size > 0) {
325
+ diagnostics.push({
326
+ message: (0, _diagnostic().md)`Parcel includes transpilation by default. Babel config __${filePath}__ includes the following redundant presets: ${[...foundRedundantPresets].map(p => _diagnostic().md.underline(p))}. Removing these may improve build performance.`,
327
+ codeFrames: [{
328
+ filePath: configPath,
329
+ codeHighlights: await getCodeHighlights(fs, configPath, foundRedundantPresets)
330
+ }],
331
+ hints: [(0, _diagnostic().md)`Remove the above presets from __${filePath}__`],
332
+ documentationURL: 'https://parceljs.org/languages/javascript/#default-presets'
333
+ });
334
+ }
335
+
336
+ if (foundRedundantPresets.has('@babel/preset-env')) {
337
+ var _babelConfig$config2, _babelConfig$config3;
338
+
339
+ diagnostics.push({
340
+ message: "@babel/preset-env does not support Parcel's targets, which will likely result in unnecessary transpilation and larger bundle sizes.",
341
+ codeFrames: [{
342
+ filePath: (_babelConfig$config2 = babelConfig.config) !== null && _babelConfig$config2 !== void 0 ? _babelConfig$config2 : babelConfig.babelrc,
343
+ codeHighlights: await getCodeHighlights(fs, (_babelConfig$config3 = babelConfig.config) !== null && _babelConfig$config3 !== void 0 ? _babelConfig$config3 : babelConfig.babelrc, new Set(['@babel/preset-env']))
344
+ }],
345
+ hints: [`Either remove __@babel/preset-env__ to use Parcel's builtin transpilation, or replace with __@parcel/babel-preset-env__`],
346
+ documentationURL: 'https://parceljs.org/languages/javascript/#custom-plugins'
347
+ });
348
+ }
349
+
350
+ if (diagnostics.length > 0) {
351
+ logger.warn(diagnostics);
352
+ }
353
+ }
354
+
355
+ async function getCodeHighlights(fs, filePath, redundantPresets) {
356
+ let ext = _path().default.extname(filePath);
357
+
358
+ if (ext !== '.js' && ext !== '.cjs' && ext !== '.mjs') {
359
+ let contents = await fs.readFile(filePath, 'utf8');
360
+
361
+ let json = _json().default.parse(contents);
362
+
363
+ let presets = json.presets || [];
364
+ let pointers = [];
365
+
366
+ for (let i = 0; i < presets.length; i++) {
367
+ if (Array.isArray(presets[i]) && redundantPresets.has(presets[i][0])) {
368
+ pointers.push({
369
+ type: 'value',
370
+ key: `/presets/${i}/0`
371
+ });
372
+ } else if (redundantPresets.has(presets[i])) {
373
+ pointers.push({
374
+ type: 'value',
375
+ key: `/presets/${i}`
376
+ });
377
+ }
378
+ }
379
+
380
+ if (pointers.length > 0) {
381
+ try {
382
+ return (0, _diagnostic().generateJSONCodeHighlights)(contents, pointers);
383
+ } catch {// TODO: support code highlights for json5 sources.
384
+ // Babel supports json5 syntax, but json-source-map does not.
385
+ }
386
+ }
387
+ }
388
+
389
+ return [{
390
+ start: {
391
+ line: 1,
392
+ column: 1
393
+ },
394
+ end: {
395
+ line: 1,
396
+ column: 1
397
+ }
398
+ }];
260
399
  }
package/lib/utils.js CHANGED
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.enginesToBabelTargets = enginesToBabelTargets;
7
- exports.getMinSemver = getMinSemver;
8
7
 
9
8
  function _assert() {
10
9
  const data = _interopRequireDefault(require("assert"));
@@ -56,9 +55,11 @@ function enginesToBabelTargets(env) {
56
55
  if (engineName === 'browsers') {
57
56
  targets[engineName] = engineValue;
58
57
  } else {
58
+ var _semver$minVersion;
59
+
59
60
  (0, _assert().default)(typeof engineValue === 'string');
60
61
  if (!_options().TargetNames.hasOwnProperty(engineName)) continue;
61
- let minVersion = getMinSemver(engineValue);
62
+ let minVersion = (_semver$minVersion = _semver().default.minVersion(engineValue)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString();
62
63
  targets[engineName] = minVersion !== null && minVersion !== void 0 ? minVersion : engineValue;
63
64
  }
64
65
  }
@@ -77,16 +78,4 @@ function enginesToBabelTargets(env) {
77
78
  }
78
79
 
79
80
  return targets;
80
- } // TODO: Replace with `minVersion` (https://github.com/npm/node-semver#ranges-1)
81
- // once semver has been upgraded across Parcel.
82
-
83
-
84
- function getMinSemver(version) {
85
- try {
86
- let range = new (_semver().default.Range)(version);
87
- let sorted = range.set.sort((a, b) => a[0].semver.compare(b[0].semver));
88
- return sorted[0][0].semver.version;
89
- } catch (err) {
90
- return null;
91
- }
92
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-babel",
3
- "version": "2.0.0-rc.0",
3
+ "version": "2.1.1",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,7 +17,7 @@
17
17
  "source": "src/BabelTransformer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.0.0-beta.1"
20
+ "parcel": "^2.1.1"
21
21
  },
22
22
  "dependencies": {
23
23
  "@babel/core": "^7.12.0",
@@ -25,19 +25,21 @@
25
25
  "@babel/helper-compilation-targets": "^7.8.4",
26
26
  "@babel/plugin-transform-flow-strip-types": "^7.0.0",
27
27
  "@babel/traverse": "^7.0.0",
28
- "@parcel/babel-ast-utils": "2.0.0-rc.0",
29
- "@parcel/plugin": "2.0.0-rc.0",
30
- "@parcel/source-map": "2.0.0-rc.6",
31
- "@parcel/utils": "2.0.0-rc.0",
28
+ "@parcel/babel-ast-utils": "^2.1.1",
29
+ "@parcel/diagnostic": "^2.1.1",
30
+ "@parcel/plugin": "^2.1.1",
31
+ "@parcel/source-map": "^2.0.0",
32
+ "@parcel/utils": "^2.1.1",
32
33
  "browserslist": "^4.6.6",
33
34
  "core-js": "^3.2.1",
35
+ "json5": "^2.1.0",
34
36
  "nullthrows": "^1.1.1",
35
37
  "semver": "^5.7.0"
36
38
  },
37
39
  "devDependencies": {
38
40
  "@babel/core": "^7.12.0",
39
41
  "@babel/preset-env": "^7.0.0",
40
- "@parcel/types": "2.0.0-rc.0"
42
+ "@parcel/types": "^2.1.1"
41
43
  },
42
- "gitHead": "8fc248f1f8eeb4428f7abd768bd23111451b1903"
44
+ "gitHead": "f53ffe772a8259d94ca2937d02fde149454112f3"
43
45
  }
package/src/config.js CHANGED
@@ -2,11 +2,14 @@
2
2
 
3
3
  import type {Config, PluginOptions, PluginLogger} from '@parcel/types';
4
4
  import typeof * as BabelCore from '@babel/core';
5
+ import type {Diagnostic} from '@parcel/diagnostic';
5
6
  import type {BabelConfig} from './types';
6
7
 
8
+ import json5 from 'json5';
7
9
  import path from 'path';
8
10
  import * as internalBabelCore from '@babel/core';
9
11
  import {hashObject, relativePath, resolveConfig} from '@parcel/utils';
12
+ import {md, generateJSONCodeHighlights} from '@parcel/diagnostic';
10
13
 
11
14
  import isJSX from './jsx';
12
15
  import getFlowOptions from './flow';
@@ -107,10 +110,9 @@ export async function load(
107
110
  // We need to invalidate on startup in case the config is non-static,
108
111
  // e.g. uses unknown environment variables, reads from the filesystem, etc.
109
112
  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 ${path.basename(
111
- file,
112
- path.extname(file),
113
- ) + '.json'} file instead.`,
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.`,
114
116
  });
115
117
  config.invalidateOnStartup();
116
118
 
@@ -195,6 +197,7 @@ export async function load(
195
197
  config.setCacheKey(JSON.stringify(Date.now()));
196
198
  config.invalidateOnStartup();
197
199
  } else {
200
+ await warnOnRedundantPlugins(options.inputFS, partialConfig, logger);
198
201
  definePluginDependencies(config, partialConfig.options, options);
199
202
  config.setCacheKey(hashObject(partialConfig.options));
200
203
  }
@@ -285,3 +288,147 @@ function definePluginDependencies(config, babelConfig: ?BabelConfig, options) {
285
288
  });
286
289
  }
287
290
  }
291
+
292
+ const redundantPresets = new Set([
293
+ '@babel/preset-env',
294
+ '@babel/preset-react',
295
+ '@babel/preset-typescript',
296
+ '@parcel/babel-preset-env',
297
+ ]);
298
+
299
+ async function warnOnRedundantPlugins(fs, babelConfig, logger) {
300
+ if (babelConfig == null) {
301
+ return;
302
+ }
303
+
304
+ let configPath = babelConfig.config ?? babelConfig.babelrc;
305
+ if (!configPath) {
306
+ return;
307
+ }
308
+
309
+ let presets = babelConfig.options.presets || [];
310
+ let plugins = babelConfig.options.plugins || [];
311
+ let foundRedundantPresets = new Set();
312
+
313
+ let filteredPresets = presets.filter(preset => {
314
+ if (redundantPresets.has(preset.file.request)) {
315
+ foundRedundantPresets.add(preset.file.request);
316
+ return false;
317
+ }
318
+
319
+ return true;
320
+ });
321
+
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
+ }
394
+ }
395
+
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
+ ];
434
+ }
package/src/utils.js CHANGED
@@ -47,7 +47,7 @@ export function enginesToBabelTargets(env: Environment): BabelTargets {
47
47
  } else {
48
48
  invariant(typeof engineValue === 'string');
49
49
  if (!TargetNames.hasOwnProperty(engineName)) continue;
50
- let minVersion = getMinSemver(engineValue);
50
+ let minVersion = semver.minVersion(engineValue)?.toString();
51
51
  targets[engineName] = minVersion ?? engineValue;
52
52
  }
53
53
  }
@@ -69,15 +69,3 @@ export function enginesToBabelTargets(env: Environment): BabelTargets {
69
69
 
70
70
  return targets;
71
71
  }
72
-
73
- // TODO: Replace with `minVersion` (https://github.com/npm/node-semver#ranges-1)
74
- // once semver has been upgraded across Parcel.
75
- export function getMinSemver(version: string): ?string {
76
- try {
77
- let range = new semver.Range(version);
78
- let sorted = range.set.sort((a, b) => a[0].semver.compare(b[0].semver));
79
- return sorted[0][0].semver.version;
80
- } catch (err) {
81
- return null;
82
- }
83
- }