@parcel/transformer-babel 2.1.0 → 2.3.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.
@@ -5,15 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- function _babelAstUtils() {
9
- const data = require("@parcel/babel-ast-utils");
10
-
11
- _babelAstUtils = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
8
+ var _babelErrorUtils = require("./babelErrorUtils");
17
9
 
18
10
  function _plugin() {
19
11
  const data = require("@parcel/plugin");
@@ -55,16 +47,6 @@ function _semver() {
55
47
  return data;
56
48
  }
57
49
 
58
- function _generator() {
59
- const data = _interopRequireDefault(require("@babel/generator"));
60
-
61
- _generator = function () {
62
- return data;
63
- };
64
-
65
- return data;
66
- }
67
-
68
50
  var _babel = _interopRequireDefault(require("./babel7"));
69
51
 
70
52
  var _config = require("./config");
@@ -111,7 +93,7 @@ var _default = new (_plugin().Transformer)({
111
93
 
112
94
  return [asset];
113
95
  } catch (e) {
114
- throw await (0, _babelAstUtils().babelErrorEnhancer)(e, asset);
96
+ throw await (0, _babelErrorUtils.babelErrorEnhancer)(e, asset);
115
97
  }
116
98
  },
117
99
 
@@ -122,10 +104,18 @@ var _default = new (_plugin().Transformer)({
122
104
  }) {
123
105
  let originalSourceMap = await asset.getMap();
124
106
  let sourceFileName = (0, _utils().relativeUrl)(options.projectRoot, asset.filePath);
107
+ const babelCorePath = await options.packageManager.resolve('@babel/core', asset.filePath, {
108
+ range: '^7.12.0',
109
+ saveDev: true,
110
+ shouldAutoInstall: options.shouldAutoInstall
111
+ });
112
+ const {
113
+ default: generate
114
+ } = await options.packageManager.require('@babel/generator', babelCorePath.resolved);
125
115
  let {
126
116
  code,
127
117
  rawMappings
128
- } = (0, _generator().default)(ast.program, {
118
+ } = generate(ast.program, {
129
119
  sourceFileName,
130
120
  sourceMaps: !!asset.env.sourceMap,
131
121
  comments: true
package/lib/babel7.js CHANGED
@@ -15,16 +15,6 @@ function _assert() {
15
15
  return data;
16
16
  }
17
17
 
18
- function internalBabelCore() {
19
- const data = _interopRequireWildcard(require("@babel/core"));
20
-
21
- internalBabelCore = function () {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
18
  function _utils() {
29
19
  const data = require("@parcel/utils");
30
20
 
@@ -35,22 +25,10 @@ function _utils() {
35
25
  return data;
36
26
  }
37
27
 
38
- function _babelAstUtils() {
39
- const data = require("@parcel/babel-ast-utils");
40
-
41
- _babelAstUtils = function () {
42
- return data;
43
- };
44
-
45
- return data;
46
- }
28
+ var _remapAstLocations = require("./remapAstLocations");
47
29
 
48
30
  var _package = _interopRequireDefault(require("../package.json"));
49
31
 
50
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
51
-
52
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
53
-
54
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
55
33
 
56
34
  const transformerVersion = _package.default.version;
@@ -65,7 +43,7 @@ async function babel7(opts) {
65
43
  babelOptions,
66
44
  additionalPlugins = []
67
45
  } = opts;
68
- const babelCore = babelOptions.internal ? internalBabelCore() : await options.packageManager.require('@babel/core', asset.filePath, {
46
+ const babelCore = await options.packageManager.require('@babel/core', asset.filePath, {
69
47
  range: '^7.12.0',
70
48
  saveDev: true,
71
49
  shouldAutoInstall: options.shouldAutoInstall
@@ -105,7 +83,7 @@ async function babel7(opts) {
105
83
  let map = await asset.getMap();
106
84
 
107
85
  if (map) {
108
- (0, _babelAstUtils().remapAstLocations)(res.ast, map);
86
+ (0, _remapAstLocations.remapAstLocations)(babelCore.types, res.ast, map);
109
87
  }
110
88
  }
111
89
  }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.babelErrorEnhancer = babelErrorEnhancer;
7
+
8
+ async function babelErrorEnhancer(error, asset) {
9
+ if (error.loc) {
10
+ let start = error.message.startsWith(asset.filePath) ? asset.filePath.length + 1 : 0;
11
+ error.message = error.message.slice(start).split('\n')[0].trim();
12
+ }
13
+
14
+ error.source = await asset.getCode();
15
+ error.filePath = asset.filePath;
16
+ return error;
17
+ }
package/lib/config.js CHANGED
@@ -25,16 +25,6 @@ function _path() {
25
25
  return data;
26
26
  }
27
27
 
28
- function internalBabelCore() {
29
- const data = _interopRequireWildcard(require("@babel/core"));
30
-
31
- internalBabelCore = function () {
32
- return data;
33
- };
34
-
35
- return data;
36
- }
37
-
38
28
  function _utils() {
39
29
  const data = require("@parcel/utils");
40
30
 
@@ -55,25 +45,19 @@ function _diagnostic() {
55
45
  return data;
56
46
  }
57
47
 
48
+ var _constants = require("./constants");
49
+
58
50
  var _jsx = _interopRequireDefault(require("./jsx"));
59
51
 
60
52
  var _flow = _interopRequireDefault(require("./flow"));
61
53
 
62
54
  var _utils2 = require("./utils");
63
55
 
64
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
65
-
66
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
67
-
68
56
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
69
57
 
70
58
  const TYPESCRIPT_EXTNAME_RE = /\.tsx?$/;
71
-
72
- const BABEL_TRANSFORMER_DIR = _path().default.dirname(__dirname);
73
-
74
59
  const JS_EXTNAME_RE = /^\.(js|cjs|mjs)$/;
75
60
  const BABEL_CONFIG_FILENAMES = ['.babelrc', '.babelrc.js', '.babelrc.json', '.babelrc.cjs', '.babelrc.mjs', '.babelignore', 'babel.config.js', 'babel.config.json', 'babel.config.mjs', 'babel.config.cjs'];
76
- const BABEL_CORE_RANGE = '^7.12.0';
77
61
 
78
62
  async function load(config, options, logger) {
79
63
  var _ref, _ref2, _options$env$BABEL_EN;
@@ -98,14 +82,14 @@ async function load(config, options, logger) {
98
82
  }
99
83
 
100
84
  const babelCore = await options.packageManager.require('@babel/core', config.searchPath, {
101
- range: BABEL_CORE_RANGE,
85
+ range: _constants.BABEL_CORE_RANGE,
102
86
  saveDev: true,
103
87
  shouldAutoInstall: options.shouldAutoInstall
104
88
  });
105
89
  config.addDevDependency({
106
90
  specifier: '@babel/core',
107
91
  resolveFrom: config.searchPath,
108
- range: BABEL_CORE_RANGE
92
+ range: _constants.BABEL_CORE_RANGE
109
93
  });
110
94
  config.invalidateOnEnvChange('BABEL_ENV');
111
95
  config.invalidateOnEnvChange('NODE_ENV');
@@ -134,7 +118,7 @@ async function load(config, options, logger) {
134
118
  additionalInvalidations: [{
135
119
  specifier: '@babel/core',
136
120
  resolveFrom: config.searchPath,
137
- range: BABEL_CORE_RANGE
121
+ range: _constants.BABEL_CORE_RANGE
138
122
  }]
139
123
  });
140
124
  } else {
@@ -234,14 +218,6 @@ async function buildDefaultBabelConfig(options, config) {
234
218
  syntaxPlugins.push('jsx');
235
219
  }
236
220
 
237
- babelOptions.presets = (babelOptions.presets || []).map(preset => internalBabelCore().createConfigItem(preset, {
238
- type: 'preset',
239
- dirname: BABEL_TRANSFORMER_DIR
240
- }));
241
- babelOptions.plugins = (babelOptions.plugins || []).map(plugin => internalBabelCore().createConfigItem(plugin, {
242
- type: 'plugin',
243
- dirname: BABEL_TRANSFORMER_DIR
244
- }));
245
221
  definePluginDependencies(config, babelOptions, options);
246
222
  return {
247
223
  internal: true,
@@ -274,7 +250,7 @@ function definePluginDependencies(config, babelConfig, options) {
274
250
  additionalInvalidations: [{
275
251
  specifier: '@babel/core',
276
252
  resolveFrom: config.searchPath,
277
- range: BABEL_CORE_RANGE
253
+ range: _constants.BABEL_CORE_RANGE
278
254
  }]
279
255
  });
280
256
  }
package/lib/constants.js CHANGED
@@ -3,6 +3,6 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.BABEL_RANGE = void 0;
7
- const BABEL_RANGE = '^7.0.0';
8
- exports.BABEL_RANGE = BABEL_RANGE;
6
+ exports.BABEL_CORE_RANGE = void 0;
7
+ const BABEL_CORE_RANGE = '^7.12.0';
8
+ exports.BABEL_CORE_RANGE = BABEL_CORE_RANGE;
package/lib/flow.js CHANGED
@@ -5,6 +5,20 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = getFlowOptions;
7
7
 
8
+ var _constants = require("./constants");
9
+
10
+ function _path() {
11
+ const data = _interopRequireDefault(require("path"));
12
+
13
+ _path = function () {
14
+ return data;
15
+ };
16
+
17
+ return data;
18
+ }
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
8
22
  /**
9
23
  * Generates a babel config for stripping away Flow types.
10
24
  */
@@ -21,9 +35,22 @@ async function getFlowOptions(config, options) {
21
35
  return null;
22
36
  }
23
37
 
38
+ const babelCore = await options.packageManager.require('@babel/core', config.searchPath, {
39
+ range: _constants.BABEL_CORE_RANGE,
40
+ saveDev: true,
41
+ shouldAutoInstall: options.shouldAutoInstall
42
+ });
43
+ await options.packageManager.require('@babel/plugin-transform-flow-strip-types', config.searchPath, {
44
+ range: '^7.0.0',
45
+ saveDev: true,
46
+ shouldAutoInstall: options.shouldAutoInstall
47
+ });
24
48
  return {
25
- plugins: [['@babel/plugin-transform-flow-strip-types', {
49
+ plugins: [babelCore.createConfigItem(['@babel/plugin-transform-flow-strip-types', {
26
50
  requireDirective: true
27
- }]]
51
+ }], {
52
+ type: 'plugin',
53
+ dirname: _path().default.dirname(config.searchPath)
54
+ })]
28
55
  };
29
56
  }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.remapAstLocations = remapAstLocations;
7
+
8
+ function remapAstLocations(t, ast, map) {
9
+ // remap ast to original mappings
10
+ // This improves sourcemap accuracy and fixes sourcemaps when scope-hoisting
11
+ traverseAll(t, ast.program, node => {
12
+ if (node.loc) {
13
+ var _node$loc;
14
+
15
+ if ((_node$loc = node.loc) !== null && _node$loc !== void 0 && _node$loc.start) {
16
+ let mapping = map.findClosestMapping(node.loc.start.line, node.loc.start.column);
17
+
18
+ if (mapping !== null && mapping !== void 0 && mapping.original) {
19
+ // $FlowFixMe
20
+ node.loc.start.line = mapping.original.line; // $FlowFixMe
21
+
22
+ node.loc.start.column = mapping.original.column; // $FlowFixMe
23
+
24
+ let length = node.loc.end.column - node.loc.start.column; // $FlowFixMe
25
+
26
+ node.loc.end.line = mapping.original.line; // $FlowFixMe
27
+
28
+ node.loc.end.column = mapping.original.column + length; // $FlowFixMe
29
+
30
+ node.loc.filename = mapping.source;
31
+ } else {
32
+ // Maintain null mappings?
33
+ node.loc = null;
34
+ }
35
+ }
36
+ }
37
+ });
38
+ }
39
+
40
+ function traverseAll(t, node, visitor) {
41
+ if (!node) {
42
+ return;
43
+ }
44
+
45
+ visitor(node);
46
+
47
+ for (let key of t.VISITOR_KEYS[node.type] || []) {
48
+ // $FlowFixMe
49
+ let subNode = node[key];
50
+
51
+ if (Array.isArray(subNode)) {
52
+ for (let i = 0; i < subNode.length; i++) {
53
+ traverseAll(t, subNode[i], visitor);
54
+ }
55
+ } else {
56
+ traverseAll(t, subNode, visitor);
57
+ }
58
+ }
59
+ }
package/lib/utils.js CHANGED
@@ -25,20 +25,25 @@ function _semver() {
25
25
  return data;
26
26
  }
27
27
 
28
- function _options() {
29
- const data = require("@babel/helper-compilation-targets/lib/options");
30
-
31
- _options = function () {
32
- return data;
33
- };
34
-
35
- return data;
36
- }
37
-
38
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
29
 
40
- // List of browsers to exclude when the esmodule target is specified.
30
+ // Copied from @babel/helper-compilation-targets/lib/options.js
31
+ const TargetNames = {
32
+ node: 'node',
33
+ chrome: 'chrome',
34
+ opera: 'opera',
35
+ edge: 'edge',
36
+ firefox: 'firefox',
37
+ safari: 'safari',
38
+ ie: 'ie',
39
+ ios: 'ios',
40
+ android: 'android',
41
+ electron: 'electron',
42
+ samsung: 'samsung',
43
+ rhino: 'rhino'
44
+ }; // List of browsers to exclude when the esmodule target is specified.
41
45
  // Based on https://caniuse.com/#feat=es6-module
46
+
42
47
  const ESMODULE_BROWSERS = ['not ie <= 11', 'not edge < 16', 'not firefox < 60', 'not chrome < 61', 'not safari < 11', 'not opera < 48', 'not ios_saf < 11', 'not op_mini all', 'not android < 76', 'not blackberry > 0', 'not op_mob > 0', 'not and_chr < 76', 'not and_ff < 68', 'not ie_mob > 0', 'not and_uc > 0', 'not samsung < 8.2', 'not and_qq > 0', 'not baidu > 0', 'not kaios > 0'];
43
48
 
44
49
  function enginesToBabelTargets(env) {
@@ -58,7 +63,7 @@ function enginesToBabelTargets(env) {
58
63
  var _semver$minVersion;
59
64
 
60
65
  (0, _assert().default)(typeof engineValue === 'string');
61
- if (!_options().TargetNames.hasOwnProperty(engineName)) continue;
66
+ if (!TargetNames.hasOwnProperty(engineName)) continue;
62
67
  let minVersion = (_semver$minVersion = _semver().default.minVersion(engineValue)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString();
63
68
  targets[engineName] = minVersion !== null && minVersion !== void 0 ? minVersion : engineValue;
64
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-babel",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,29 +17,23 @@
17
17
  "source": "src/BabelTransformer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.1.0"
20
+ "parcel": "^2.3.0"
21
21
  },
22
22
  "dependencies": {
23
- "@babel/core": "^7.12.0",
24
- "@babel/generator": "^7.9.0",
25
- "@babel/helper-compilation-targets": "^7.8.4",
26
- "@babel/plugin-transform-flow-strip-types": "^7.0.0",
27
- "@babel/traverse": "^7.0.0",
28
- "@parcel/babel-ast-utils": "^2.1.0",
29
- "@parcel/diagnostic": "^2.1.0",
30
- "@parcel/plugin": "^2.1.0",
23
+ "@parcel/diagnostic": "^2.3.0",
24
+ "@parcel/plugin": "^2.3.0",
31
25
  "@parcel/source-map": "^2.0.0",
32
- "@parcel/utils": "^2.1.0",
26
+ "@parcel/utils": "^2.3.0",
33
27
  "browserslist": "^4.6.6",
34
- "core-js": "^3.2.1",
35
- "json5": "^2.1.0",
28
+ "json5": "^2.2.0",
36
29
  "nullthrows": "^1.1.1",
37
30
  "semver": "^5.7.0"
38
31
  },
39
32
  "devDependencies": {
40
33
  "@babel/core": "^7.12.0",
41
34
  "@babel/preset-env": "^7.0.0",
42
- "@parcel/types": "^2.1.0"
35
+ "@babel/types": "^7.12.0",
36
+ "@parcel/types": "^2.3.0"
43
37
  },
44
- "gitHead": "5a80fbe4af8797d0aab248a66f5254f42f00e83e"
38
+ "gitHead": "65ffb58e88024258c31e1f480241f293e29b25c5"
45
39
  }
@@ -1,11 +1,10 @@
1
1
  // @flow strict-local
2
2
 
3
- import {babelErrorEnhancer} from '@parcel/babel-ast-utils';
3
+ import {babelErrorEnhancer} from './babelErrorUtils';
4
4
  import {Transformer} from '@parcel/plugin';
5
5
  import {relativeUrl} from '@parcel/utils';
6
6
  import SourceMap from '@parcel/source-map';
7
7
  import semver from 'semver';
8
- import generate from '@babel/generator';
9
8
  import babel7 from './babel7';
10
9
  import {load} from './config';
11
10
 
@@ -48,6 +47,22 @@ export default (new Transformer({
48
47
  options.projectRoot,
49
48
  asset.filePath,
50
49
  );
50
+
51
+ const babelCorePath = await options.packageManager.resolve(
52
+ '@babel/core',
53
+ asset.filePath,
54
+ {
55
+ range: '^7.12.0',
56
+ saveDev: true,
57
+ shouldAutoInstall: options.shouldAutoInstall,
58
+ },
59
+ );
60
+
61
+ const {default: generate} = await options.packageManager.require(
62
+ '@babel/generator',
63
+ babelCorePath.resolved,
64
+ );
65
+
51
66
  let {code, rawMappings} = generate(ast.program, {
52
67
  sourceFileName,
53
68
  sourceMaps: !!asset.env.sourceMap,
package/src/babel7.js CHANGED
@@ -1,11 +1,11 @@
1
1
  // @flow
2
2
 
3
3
  import type {MutableAsset, AST, PluginOptions} from '@parcel/types';
4
+ import typeof * as BabelCore from '@babel/core';
4
5
 
5
6
  import invariant from 'assert';
6
- import * as internalBabelCore from '@babel/core';
7
7
  import {relativeUrl} from '@parcel/utils';
8
- import {remapAstLocations} from '@parcel/babel-ast-utils';
8
+ import {remapAstLocations} from './remapAstLocations';
9
9
 
10
10
  import packageJson from '../package.json';
11
11
 
@@ -23,13 +23,15 @@ export default async function babel7(
23
23
  opts: Babel7TransformOptions,
24
24
  ): Promise<?AST> {
25
25
  let {asset, options, babelOptions, additionalPlugins = []} = opts;
26
- const babelCore = babelOptions.internal
27
- ? internalBabelCore
28
- : await options.packageManager.require('@babel/core', asset.filePath, {
29
- range: '^7.12.0',
30
- saveDev: true,
31
- shouldAutoInstall: options.shouldAutoInstall,
32
- });
26
+ const babelCore: BabelCore = await options.packageManager.require(
27
+ '@babel/core',
28
+ asset.filePath,
29
+ {
30
+ range: '^7.12.0',
31
+ saveDev: true,
32
+ shouldAutoInstall: options.shouldAutoInstall,
33
+ },
34
+ );
33
35
 
34
36
  let config = {
35
37
  ...babelOptions.config,
@@ -77,7 +79,7 @@ export default async function babel7(
77
79
  if (res.ast) {
78
80
  let map = await asset.getMap();
79
81
  if (map) {
80
- remapAstLocations(res.ast, map);
82
+ remapAstLocations(babelCore.types, res.ast, map);
81
83
  }
82
84
  }
83
85
  }
@@ -0,0 +1,30 @@
1
+ // @flow
2
+ import type {BaseAsset} from '@parcel/types';
3
+
4
+ export type BabelError = Error & {
5
+ loc?: {
6
+ line: number,
7
+ column: number,
8
+ ...
9
+ },
10
+ source?: string,
11
+ filePath?: string,
12
+ ...
13
+ };
14
+
15
+ export async function babelErrorEnhancer(
16
+ error: BabelError,
17
+ asset: BaseAsset,
18
+ ): Promise<BabelError> {
19
+ if (error.loc) {
20
+ let start = error.message.startsWith(asset.filePath)
21
+ ? asset.filePath.length + 1
22
+ : 0;
23
+ error.message = error.message.slice(start).split('\n')[0].trim();
24
+ }
25
+
26
+ error.source = await asset.getCode();
27
+ error.filePath = asset.filePath;
28
+
29
+ return error;
30
+ }
package/src/config.js CHANGED
@@ -7,16 +7,15 @@ import type {BabelConfig} from './types';
7
7
 
8
8
  import json5 from 'json5';
9
9
  import path from 'path';
10
- import * as internalBabelCore from '@babel/core';
11
10
  import {hashObject, relativePath, resolveConfig} from '@parcel/utils';
12
11
  import {md, generateJSONCodeHighlights} from '@parcel/diagnostic';
12
+ import {BABEL_CORE_RANGE} from './constants';
13
13
 
14
14
  import isJSX from './jsx';
15
15
  import getFlowOptions from './flow';
16
16
  import {enginesToBabelTargets} from './utils';
17
17
 
18
18
  const TYPESCRIPT_EXTNAME_RE = /\.tsx?$/;
19
- const BABEL_TRANSFORMER_DIR = path.dirname(__dirname);
20
19
  const JS_EXTNAME_RE = /^\.(js|cjs|mjs)$/;
21
20
  const BABEL_CONFIG_FILENAMES = [
22
21
  '.babelrc',
@@ -31,8 +30,6 @@ const BABEL_CONFIG_FILENAMES = [
31
30
  'babel.config.cjs',
32
31
  ];
33
32
 
34
- const BABEL_CORE_RANGE = '^7.12.0';
35
-
36
33
  type BabelConfigResult = {|
37
34
  internal: boolean,
38
35
  config: BabelConfig,
@@ -234,19 +231,6 @@ async function buildDefaultBabelConfig(
234
231
  syntaxPlugins.push('jsx');
235
232
  }
236
233
 
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
234
  definePluginDependencies(config, babelOptions, options);
251
235
  return {
252
236
  internal: true,
package/src/constants.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // @flow strict-local
2
2
 
3
- export const BABEL_RANGE = '^7.0.0';
3
+ export const BABEL_CORE_RANGE = '^7.12.0';
package/src/flow.js CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  import type {Config, PluginOptions, PackageJSON} from '@parcel/types';
4
4
  import type {BabelConfig} from './types';
5
+ import typeof * as BabelCore from '@babel/core';
6
+
7
+ import {BABEL_CORE_RANGE} from './constants';
8
+ import path from 'path';
5
9
 
6
10
  /**
7
11
  * Generates a babel config for stripping away Flow types.
@@ -28,9 +32,35 @@ export default async function getFlowOptions(
28
32
  return null;
29
33
  }
30
34
 
35
+ const babelCore: BabelCore = await options.packageManager.require(
36
+ '@babel/core',
37
+ config.searchPath,
38
+ {
39
+ range: BABEL_CORE_RANGE,
40
+ saveDev: true,
41
+ shouldAutoInstall: options.shouldAutoInstall,
42
+ },
43
+ );
44
+
45
+ await options.packageManager.require(
46
+ '@babel/plugin-transform-flow-strip-types',
47
+ config.searchPath,
48
+ {
49
+ range: '^7.0.0',
50
+ saveDev: true,
51
+ shouldAutoInstall: options.shouldAutoInstall,
52
+ },
53
+ );
54
+
31
55
  return {
32
56
  plugins: [
33
- ['@babel/plugin-transform-flow-strip-types', {requireDirective: true}],
57
+ babelCore.createConfigItem(
58
+ ['@babel/plugin-transform-flow-strip-types', {requireDirective: true}],
59
+ {
60
+ type: 'plugin',
61
+ dirname: path.dirname(config.searchPath),
62
+ },
63
+ ),
34
64
  ],
35
65
  };
36
66
  }
@@ -0,0 +1,70 @@
1
+ // @flow strict-local
2
+
3
+ import type {File as BabelNodeFile} from '@babel/types';
4
+ import type SourceMap from '@parcel/source-map';
5
+ import type {Node} from '@babel/types';
6
+ import typeof * as BabelTypes from '@babel/types';
7
+
8
+ export function remapAstLocations(
9
+ t: BabelTypes,
10
+ ast: BabelNodeFile,
11
+ map: SourceMap,
12
+ ) {
13
+ // remap ast to original mappings
14
+ // This improves sourcemap accuracy and fixes sourcemaps when scope-hoisting
15
+ traverseAll(t, ast.program, node => {
16
+ if (node.loc) {
17
+ if (node.loc?.start) {
18
+ let mapping = map.findClosestMapping(
19
+ node.loc.start.line,
20
+ node.loc.start.column,
21
+ );
22
+
23
+ if (mapping?.original) {
24
+ // $FlowFixMe
25
+ node.loc.start.line = mapping.original.line;
26
+ // $FlowFixMe
27
+ node.loc.start.column = mapping.original.column;
28
+
29
+ // $FlowFixMe
30
+ let length = node.loc.end.column - node.loc.start.column;
31
+
32
+ // $FlowFixMe
33
+ node.loc.end.line = mapping.original.line;
34
+ // $FlowFixMe
35
+ node.loc.end.column = mapping.original.column + length;
36
+
37
+ // $FlowFixMe
38
+ node.loc.filename = mapping.source;
39
+ } else {
40
+ // Maintain null mappings?
41
+ node.loc = null;
42
+ }
43
+ }
44
+ }
45
+ });
46
+ }
47
+
48
+ function traverseAll(
49
+ t: BabelTypes,
50
+ node: Node,
51
+ visitor: (node: Node) => void,
52
+ ): void {
53
+ if (!node) {
54
+ return;
55
+ }
56
+
57
+ visitor(node);
58
+
59
+ for (let key of t.VISITOR_KEYS[node.type] || []) {
60
+ // $FlowFixMe
61
+ let subNode: Node | Array<Node> = node[key];
62
+ if (Array.isArray(subNode)) {
63
+ for (let i = 0; i < subNode.length; i++) {
64
+ traverseAll(t, subNode[i], visitor);
65
+ }
66
+ } else {
67
+ traverseAll(t, subNode, visitor);
68
+ }
69
+ }
70
+ }
package/src/utils.js CHANGED
@@ -5,7 +5,22 @@ import type {Targets as BabelTargets} from '@babel/preset-env';
5
5
 
6
6
  import invariant from 'assert';
7
7
  import semver from 'semver';
8
- import {TargetNames} from '@babel/helper-compilation-targets/lib/options';
8
+
9
+ // Copied from @babel/helper-compilation-targets/lib/options.js
10
+ const TargetNames = {
11
+ node: 'node',
12
+ chrome: 'chrome',
13
+ opera: 'opera',
14
+ edge: 'edge',
15
+ firefox: 'firefox',
16
+ safari: 'safari',
17
+ ie: 'ie',
18
+ ios: 'ios',
19
+ android: 'android',
20
+ electron: 'electron',
21
+ samsung: 'samsung',
22
+ rhino: 'rhino',
23
+ };
9
24
 
10
25
  // List of browsers to exclude when the esmodule target is specified.
11
26
  // Based on https://caniuse.com/#feat=es6-module
@@ -1,114 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.babel7toBabel6 = babel7toBabel6;
7
- exports.babel6toBabel7 = babel6toBabel7;
8
-
9
- function _traverse() {
10
- const data = _interopRequireDefault(require("@babel/traverse"));
11
-
12
- _traverse = function () {
13
- return data;
14
- };
15
-
16
- return data;
17
- }
18
-
19
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
-
21
- // Convert between babel 7 and babel 6 AST
22
- // More info on the AST Changes: https://babeljs.io/docs/en/v7-migration-api#ast-changes
23
- function babel7toBabel6(ast) {
24
- const visitor = {
25
- ArrowFunctionExpression: node => {
26
- node.expression = node.body.type !== 'BlockStatement';
27
- },
28
- ExistsTypeAnnotation: node => {
29
- node.type = 'ExistentialTypeParam';
30
- },
31
- NumberLiteralTypeAnnotation: node => {
32
- node.type = 'NumericLiteralTypeAnnotation';
33
- },
34
- ObjectTypeIndexer: node => {
35
- node.end++;
36
- node.loc.end.column++;
37
- },
38
- ForOfStatement: node => {
39
- node.type = 'ForAwaitStatement';
40
- delete node.await;
41
- },
42
- SpreadElement: (node, path) => {
43
- if (path.parentPath.isObjectExpression() || path.parentPath.isArrayExpression()) {
44
- node.type = 'SpreadProperty';
45
- }
46
- },
47
- RestElement: (node, path) => {
48
- if (path.parentPath.isObjectPattern() || path.parentPath.isArrayPattern()) {
49
- node.type = 'RestProperty';
50
- }
51
- }
52
- };
53
- (0, _traverse().default)(ast, {
54
- enter(path) {
55
- if (path.node.variance && path.node.variance.type === 'Variance') {
56
- path.node.variance = path.node.variance.kind;
57
- }
58
-
59
- let visitorFunc = visitor[path.node.type];
60
-
61
- if (visitorFunc) {
62
- visitorFunc(path.node, path);
63
- }
64
- }
65
-
66
- });
67
- return ast;
68
- }
69
-
70
- function babel6toBabel7(ast) {
71
- const visitor = {
72
- ArrowFunctionExpression: node => {
73
- delete node.expression;
74
- },
75
- ExistentialTypeParam: node => {
76
- node.type = 'ExistsTypeAnnotation';
77
- },
78
- NumericLiteralTypeAnnotation: node => {
79
- node.type = 'NumberLiteralTypeAnnotation';
80
- },
81
- ObjectTypeIndexer: node => {
82
- node.end--;
83
- node.loc.end.column--;
84
- },
85
- ForAwaitStatement: node => {
86
- node.type = 'ForOfStatement';
87
- node.await = true;
88
- },
89
- SpreadProperty: node => {
90
- node.type = 'SpreadElement';
91
- },
92
- RestProperty: node => {
93
- node.type = 'RestElement';
94
- }
95
- };
96
- (0, _traverse().default)(ast, {
97
- enter(path) {
98
- if (path.node.variance && typeof path.node.variance === 'string') {
99
- path.node.variance = {
100
- type: 'VarianceNode',
101
- kind: path.node.variance
102
- };
103
- }
104
-
105
- let visitorFunc = visitor[path.node.type];
106
-
107
- if (visitorFunc) {
108
- visitorFunc(path.node);
109
- }
110
- }
111
-
112
- });
113
- return ast;
114
- }
@@ -1,102 +0,0 @@
1
- import traverse from '@babel/traverse';
2
-
3
- // Convert between babel 7 and babel 6 AST
4
- // More info on the AST Changes: https://babeljs.io/docs/en/v7-migration-api#ast-changes
5
- export function babel7toBabel6(ast) {
6
- const visitor = {
7
- ArrowFunctionExpression: node => {
8
- node.expression = node.body.type !== 'BlockStatement';
9
- },
10
- ExistsTypeAnnotation: node => {
11
- node.type = 'ExistentialTypeParam';
12
- },
13
- NumberLiteralTypeAnnotation: node => {
14
- node.type = 'NumericLiteralTypeAnnotation';
15
- },
16
- ObjectTypeIndexer: node => {
17
- node.end++;
18
- node.loc.end.column++;
19
- },
20
- ForOfStatement: node => {
21
- node.type = 'ForAwaitStatement';
22
- delete node.await;
23
- },
24
- SpreadElement: (node, path) => {
25
- if (
26
- path.parentPath.isObjectExpression() ||
27
- path.parentPath.isArrayExpression()
28
- ) {
29
- node.type = 'SpreadProperty';
30
- }
31
- },
32
- RestElement: (node, path) => {
33
- if (
34
- path.parentPath.isObjectPattern() ||
35
- path.parentPath.isArrayPattern()
36
- ) {
37
- node.type = 'RestProperty';
38
- }
39
- },
40
- };
41
-
42
- traverse(ast, {
43
- enter(path) {
44
- if (path.node.variance && path.node.variance.type === 'Variance') {
45
- path.node.variance = path.node.variance.kind;
46
- }
47
-
48
- let visitorFunc = visitor[path.node.type];
49
- if (visitorFunc) {
50
- visitorFunc(path.node, path);
51
- }
52
- },
53
- });
54
-
55
- return ast;
56
- }
57
-
58
- export function babel6toBabel7(ast) {
59
- const visitor = {
60
- ArrowFunctionExpression: node => {
61
- delete node.expression;
62
- },
63
- ExistentialTypeParam: node => {
64
- node.type = 'ExistsTypeAnnotation';
65
- },
66
- NumericLiteralTypeAnnotation: node => {
67
- node.type = 'NumberLiteralTypeAnnotation';
68
- },
69
- ObjectTypeIndexer: node => {
70
- node.end--;
71
- node.loc.end.column--;
72
- },
73
- ForAwaitStatement: node => {
74
- node.type = 'ForOfStatement';
75
- node.await = true;
76
- },
77
- SpreadProperty: node => {
78
- node.type = 'SpreadElement';
79
- },
80
- RestProperty: node => {
81
- node.type = 'RestElement';
82
- },
83
- };
84
-
85
- traverse(ast, {
86
- enter(path) {
87
- if (path.node.variance && typeof path.node.variance === 'string') {
88
- path.node.variance = {
89
- type: 'VarianceNode',
90
- kind: path.node.variance,
91
- };
92
- }
93
-
94
- let visitorFunc = visitor[path.node.type];
95
- if (visitorFunc) {
96
- visitorFunc(path.node);
97
- }
98
- },
99
- });
100
-
101
- return ast;
102
- }