@parcel/transformer-babel 2.0.0-rc.0 → 2.0.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.
Files changed (3) hide show
  1. package/lib/config.js +139 -0
  2. package/package.json +10 -8
  3. package/src/config.js +148 -0
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/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.0.0",
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.0.0"
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.0.0",
29
+ "@parcel/diagnostic": "^2.0.0",
30
+ "@parcel/plugin": "^2.0.0",
31
+ "@parcel/source-map": "^2.0.0",
32
+ "@parcel/utils": "^2.0.0",
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.0.0"
41
43
  },
42
- "gitHead": "8fc248f1f8eeb4428f7abd768bd23111451b1903"
44
+ "gitHead": "84da50ae6943bff9741e2fc76d2f4968074cbbd6"
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';
@@ -195,6 +198,7 @@ export async function load(
195
198
  config.setCacheKey(JSON.stringify(Date.now()));
196
199
  config.invalidateOnStartup();
197
200
  } else {
201
+ await warnOnRedundantPlugins(options.inputFS, partialConfig, logger);
198
202
  definePluginDependencies(config, partialConfig.options, options);
199
203
  config.setCacheKey(hashObject(partialConfig.options));
200
204
  }
@@ -285,3 +289,147 @@ function definePluginDependencies(config, babelConfig: ?BabelConfig, options) {
285
289
  });
286
290
  }
287
291
  }
292
+
293
+ const redundantPresets = new Set([
294
+ '@babel/preset-env',
295
+ '@babel/preset-react',
296
+ '@babel/preset-typescript',
297
+ '@parcel/babel-preset-env',
298
+ ]);
299
+
300
+ async function warnOnRedundantPlugins(fs, babelConfig, logger) {
301
+ if (babelConfig == null) {
302
+ return;
303
+ }
304
+
305
+ let configPath = babelConfig.config ?? babelConfig.babelrc;
306
+ if (!configPath) {
307
+ return;
308
+ }
309
+
310
+ let presets = babelConfig.options.presets || [];
311
+ let plugins = babelConfig.options.plugins || [];
312
+ let foundRedundantPresets = new Set();
313
+
314
+ let filteredPresets = presets.filter(preset => {
315
+ if (redundantPresets.has(preset.file.request)) {
316
+ foundRedundantPresets.add(preset.file.request);
317
+ return false;
318
+ }
319
+
320
+ return true;
321
+ });
322
+
323
+ let filePath = path.relative(process.cwd(), configPath);
324
+ let diagnostics: Array<Diagnostic> = [];
325
+
326
+ if (
327
+ filteredPresets.length === 0 &&
328
+ foundRedundantPresets.size > 0 &&
329
+ plugins.length === 0
330
+ ) {
331
+ diagnostics.push({
332
+ message: md`Parcel includes transpilation by default. Babel config __${filePath}__ contains only redundant presets. Deleting it may significantly improve build performance.`,
333
+ codeFrames: [
334
+ {
335
+ filePath: configPath,
336
+ codeHighlights: await getCodeHighlights(
337
+ fs,
338
+ configPath,
339
+ foundRedundantPresets,
340
+ ),
341
+ },
342
+ ],
343
+ hints: [md`Delete __${filePath}__`],
344
+ documentationURL:
345
+ 'https://parceljs.org/languages/javascript/#default-presets',
346
+ });
347
+ } else if (foundRedundantPresets.size > 0) {
348
+ diagnostics.push({
349
+ message: md`Parcel includes transpilation by default. Babel config __${filePath}__ includes the following redundant presets: ${[
350
+ ...foundRedundantPresets,
351
+ ].map(p =>
352
+ md.underline(p),
353
+ )}. Removing these may improve build performance.`,
354
+ codeFrames: [
355
+ {
356
+ filePath: configPath,
357
+ codeHighlights: await getCodeHighlights(
358
+ fs,
359
+ configPath,
360
+ foundRedundantPresets,
361
+ ),
362
+ },
363
+ ],
364
+ hints: [md`Remove the above presets from __${filePath}__`],
365
+ documentationURL:
366
+ 'https://parceljs.org/languages/javascript/#default-presets',
367
+ });
368
+ }
369
+
370
+ if (foundRedundantPresets.has('@babel/preset-env')) {
371
+ diagnostics.push({
372
+ message:
373
+ "@babel/preset-env does not support Parcel's targets, which will likely result in unnecessary transpilation and larger bundle sizes.",
374
+ codeFrames: [
375
+ {
376
+ filePath: babelConfig.config ?? babelConfig.babelrc,
377
+ codeHighlights: await getCodeHighlights(
378
+ fs,
379
+ babelConfig.config ?? babelConfig.babelrc,
380
+ new Set(['@babel/preset-env']),
381
+ ),
382
+ },
383
+ ],
384
+ hints: [
385
+ `Either remove __@babel/preset-env__ to use Parcel's builtin transpilation, or replace with __@parcel/babel-preset-env__`,
386
+ ],
387
+ documentationURL:
388
+ 'https://parceljs.org/languages/javascript/#custom-plugins',
389
+ });
390
+ }
391
+
392
+ if (diagnostics.length > 0) {
393
+ logger.warn(diagnostics);
394
+ }
395
+ }
396
+
397
+ async function getCodeHighlights(fs, filePath, redundantPresets) {
398
+ let ext = path.extname(filePath);
399
+ if (ext !== '.js' && ext !== '.cjs' && ext !== '.mjs') {
400
+ let contents = await fs.readFile(filePath, 'utf8');
401
+ let json = json5.parse(contents);
402
+
403
+ let presets = json.presets || [];
404
+ let pointers = [];
405
+ for (let i = 0; i < presets.length; i++) {
406
+ if (Array.isArray(presets[i]) && redundantPresets.has(presets[i][0])) {
407
+ pointers.push({type: 'value', key: `/presets/${i}/0`});
408
+ } else if (redundantPresets.has(presets[i])) {
409
+ pointers.push({type: 'value', key: `/presets/${i}`});
410
+ }
411
+ }
412
+
413
+ if (pointers.length > 0) {
414
+ try {
415
+ return generateJSONCodeHighlights(contents, pointers);
416
+ } catch {
417
+ // TODO: support code highlights for json5 sources.
418
+ // Babel supports json5 syntax, but json-source-map does not.
419
+ }
420
+ }
421
+ }
422
+
423
+ return [
424
+ {
425
+ start: {
426
+ line: 1,
427
+ column: 1,
428
+ },
429
+ end: {
430
+ line: 1,
431
+ column: 1,
432
+ },
433
+ },
434
+ ];
435
+ }