@parcel/transformer-babel 2.0.0-beta.2 → 2.0.0-nightly.1004

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");
@@ -25,45 +17,49 @@ function _plugin() {
25
17
  return data;
26
18
  }
27
19
 
28
- function _semver() {
29
- const data = _interopRequireDefault(require("semver"));
20
+ function _utils() {
21
+ const data = require("@parcel/utils");
30
22
 
31
- _semver = function () {
23
+ _utils = function () {
32
24
  return data;
33
25
  };
34
26
 
35
27
  return data;
36
28
  }
37
29
 
38
- function _babel() {
39
- const data = _interopRequireDefault(require("./babel7"));
30
+ function _sourceMap() {
31
+ const data = _interopRequireDefault(require("@parcel/source-map"));
40
32
 
41
- _babel = function () {
33
+ _sourceMap = function () {
42
34
  return data;
43
35
  };
44
36
 
45
37
  return data;
46
38
  }
47
39
 
48
- function _config() {
49
- const data = require("./config");
40
+ function _semver() {
41
+ const data = _interopRequireDefault(require("semver"));
50
42
 
51
- _config = function () {
43
+ _semver = function () {
52
44
  return data;
53
45
  };
54
46
 
55
47
  return data;
56
48
  }
57
49
 
50
+ var _babel = _interopRequireDefault(require("./babel7"));
51
+
52
+ var _config = require("./config");
53
+
58
54
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
55
 
60
56
  var _default = new (_plugin().Transformer)({
61
- async loadConfig({
57
+ loadConfig({
62
58
  config,
63
59
  options,
64
60
  logger
65
61
  }) {
66
- await (0, _config().load)(config, options, logger);
62
+ return (0, _config.load)(config, options, logger);
67
63
  },
68
64
 
69
65
  canReuseAST({
@@ -77,35 +73,77 @@ var _default = new (_plugin().Transformer)({
77
73
  config,
78
74
  options
79
75
  }) {
80
- // TODO: Provide invalidateOnEnvChange on config?
81
- asset.invalidateOnEnvChange('BABEL_ENV');
82
- asset.invalidateOnEnvChange('NODE_ENV'); // TODO: come up with a better name
83
-
84
76
  try {
85
77
  if (config !== null && config !== void 0 && config.config) {
86
78
  if (asset.meta.babelPlugins != null && Array.isArray(asset.meta.babelPlugins)) {
87
- await (0, _babel().default)(asset, options, config, asset.meta.babelPlugins);
79
+ await (0, _babel.default)({
80
+ asset,
81
+ options,
82
+ babelOptions: config,
83
+ additionalPlugins: asset.meta.babelPlugins
84
+ });
88
85
  } else {
89
- await (0, _babel().default)(asset, options, config);
86
+ await (0, _babel.default)({
87
+ asset,
88
+ options,
89
+ babelOptions: config
90
+ });
90
91
  }
91
92
  }
92
93
 
93
94
  return [asset];
94
95
  } catch (e) {
95
- throw await (0, _babelAstUtils().babelErrorEnhancer)(e, asset);
96
+ throw await (0, _babelErrorUtils.babelErrorEnhancer)(e, asset);
96
97
  }
97
98
  },
98
99
 
99
- generate({
100
+ async generate({
100
101
  asset,
101
102
  ast,
102
103
  options
103
104
  }) {
104
- return (0, _babelAstUtils().generate)({
105
- asset,
106
- ast,
107
- options
105
+ let originalSourceMap = await asset.getMap();
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);
115
+ let {
116
+ code,
117
+ rawMappings
118
+ } = generate(ast.program, {
119
+ sourceFileName,
120
+ sourceMaps: !!asset.env.sourceMap,
121
+ comments: true
108
122
  });
123
+ let map = new (_sourceMap().default)(options.projectRoot);
124
+
125
+ if (rawMappings) {
126
+ map.addIndexedMappings(rawMappings);
127
+ }
128
+
129
+ if (originalSourceMap) {
130
+ // The babel AST already contains the correct mappings, but not the source contents.
131
+ // We need to copy over the source contents from the original map.
132
+ let sourcesContent = originalSourceMap.getSourcesContentMap();
133
+
134
+ for (let filePath in sourcesContent) {
135
+ let content = sourcesContent[filePath];
136
+
137
+ if (content != null) {
138
+ map.setSourceContent(filePath, content);
139
+ }
140
+ }
141
+ }
142
+
143
+ return {
144
+ content: code,
145
+ map
146
+ };
109
147
  }
110
148
 
111
149
  });
package/lib/babel7.js CHANGED
@@ -15,16 +15,6 @@ function _assert() {
15
15
  return data;
16
16
  }
17
17
 
18
- function babel() {
19
- const data = _interopRequireWildcard(require("@babel/core"));
20
-
21
- babel = 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,27 +25,29 @@ function _utils() {
35
25
  return data;
36
26
  }
37
27
 
38
- function _package() {
39
- const data = _interopRequireDefault(require("../package.json"));
40
-
41
- _package = function () {
42
- return data;
43
- };
44
-
45
- return data;
46
- }
47
-
48
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
28
+ var _remapAstLocations = require("./remapAstLocations");
49
29
 
50
- 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; }
30
+ var _package = _interopRequireDefault(require("../package.json"));
51
31
 
52
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
53
33
 
54
- const transformerVersion = _package().default.version;
55
-
34
+ const transformerVersion = _package.default.version;
56
35
  (0, _assert().default)(typeof transformerVersion === 'string');
57
36
 
58
- async function babel7(asset, options, babelOptions, additionalPlugins = []) {
37
+ async function babel7(opts) {
38
+ var _babelOptions$config$, _babelOptions$config$2, _babelOptions$syntaxP;
39
+
40
+ let {
41
+ asset,
42
+ options,
43
+ babelOptions,
44
+ additionalPlugins = []
45
+ } = opts;
46
+ const babelCore = await options.packageManager.require('@babel/core', asset.filePath, {
47
+ range: '^7.12.0',
48
+ saveDev: true,
49
+ shouldAutoInstall: options.shouldAutoInstall
50
+ });
59
51
  let config = { ...babelOptions.config,
60
52
  plugins: additionalPlugins.concat(babelOptions.config.plugins),
61
53
  code: false,
@@ -67,7 +59,10 @@ async function babel7(asset, options, babelOptions, additionalPlugins = []) {
67
59
  sourceFilename: (0, _utils().relativeUrl)(options.projectRoot, asset.filePath),
68
60
  allowReturnOutsideFunction: true,
69
61
  strictMode: false,
70
- sourceType: 'module'
62
+ sourceType: 'module',
63
+ plugins: [...((_babelOptions$config$ = (_babelOptions$config$2 = babelOptions.config.parserOpts) === null || _babelOptions$config$2 === void 0 ? void 0 : _babelOptions$config$2.plugins) !== null && _babelOptions$config$ !== void 0 ? _babelOptions$config$ : []), ...((_babelOptions$syntaxP = babelOptions.syntaxPlugins) !== null && _babelOptions$syntaxP !== void 0 ? _babelOptions$syntaxP : []), // Applied by preset-env
64
+ 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom' // 'topLevelAwait'
65
+ ]
71
66
  },
72
67
  caller: {
73
68
  name: 'parcel',
@@ -80,9 +75,17 @@ async function babel7(asset, options, babelOptions, additionalPlugins = []) {
80
75
  let res;
81
76
 
82
77
  if (ast) {
83
- res = await babel().transformFromAstAsync(ast.program, asset.isASTDirty() ? undefined : await asset.getCode(), config);
78
+ res = await babelCore.transformFromAstAsync(ast.program, asset.isASTDirty() ? undefined : await asset.getCode(), config);
84
79
  } else {
85
- res = await babel().transformAsync(await asset.getCode(), config);
80
+ res = await babelCore.transformAsync(await asset.getCode(), config);
81
+
82
+ if (res.ast) {
83
+ let map = await asset.getMap();
84
+
85
+ if (map) {
86
+ (0, _remapAstLocations.remapAstLocations)(babelCore.types, res.ast, map);
87
+ }
88
+ }
86
89
  }
87
90
 
88
91
  if (res.ast) {
@@ -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
+ }