@parcel/transformer-babel 2.0.0-beta.3 → 2.0.0-dev.1510

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.
@@ -4,137 +4,84 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
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
- }
17
-
7
+ var _babelErrorUtils = require("./babelErrorUtils");
18
8
  function _plugin() {
19
9
  const data = require("@parcel/plugin");
20
-
21
10
  _plugin = function () {
22
11
  return data;
23
12
  };
24
-
25
13
  return data;
26
14
  }
27
-
28
15
  function _utils() {
29
16
  const data = require("@parcel/utils");
30
-
31
17
  _utils = function () {
32
18
  return data;
33
19
  };
34
-
35
20
  return data;
36
21
  }
37
-
38
22
  function _sourceMap() {
39
23
  const data = _interopRequireDefault(require("@parcel/source-map"));
40
-
41
24
  _sourceMap = function () {
42
25
  return data;
43
26
  };
44
-
45
27
  return data;
46
28
  }
47
-
48
29
  function _semver() {
49
30
  const data = _interopRequireDefault(require("semver"));
50
-
51
31
  _semver = function () {
52
32
  return data;
53
33
  };
54
-
55
34
  return data;
56
35
  }
57
-
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
- function _babel() {
69
- const data = _interopRequireDefault(require("./babel7"));
70
-
71
- _babel = function () {
72
- return data;
73
- };
74
-
75
- return data;
76
- }
77
-
78
- function _config() {
79
- const data = require("./config");
80
-
81
- _config = function () {
82
- return data;
83
- };
84
-
85
- return data;
86
- }
87
-
36
+ var _babel = _interopRequireDefault(require("./babel7"));
37
+ var _config = require("./config");
88
38
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
89
-
90
- var _default = new (_plugin().Transformer)({
91
- async loadConfig({
39
+ var _default = exports.default = new (_plugin().Transformer)({
40
+ loadConfig({
92
41
  config,
93
42
  options,
94
43
  logger
95
44
  }) {
96
- await (0, _config().load)(config, options, logger);
45
+ return (0, _config.load)(config, options, logger);
97
46
  },
98
-
99
47
  canReuseAST({
100
48
  ast
101
49
  }) {
102
50
  return ast.type === 'babel' && _semver().default.satisfies(ast.version, '^7.0.0');
103
51
  },
104
-
105
52
  async transform({
106
53
  asset,
107
54
  config,
108
- options
55
+ logger,
56
+ options,
57
+ tracer
109
58
  }) {
110
- // TODO: Provide invalidateOnEnvChange on config?
111
- asset.invalidateOnEnvChange('BABEL_ENV');
112
- asset.invalidateOnEnvChange('NODE_ENV'); // TODO: come up with a better name
113
-
114
59
  try {
115
60
  if (config !== null && config !== void 0 && config.config) {
116
61
  if (asset.meta.babelPlugins != null && Array.isArray(asset.meta.babelPlugins)) {
117
- await (0, _babel().default)({
62
+ await (0, _babel.default)({
118
63
  asset,
119
64
  options,
65
+ logger,
120
66
  babelOptions: config,
121
- additionalPlugins: asset.meta.babelPlugins
67
+ additionalPlugins: asset.meta.babelPlugins,
68
+ tracer
122
69
  });
123
70
  } else {
124
- await (0, _babel().default)({
71
+ await (0, _babel.default)({
125
72
  asset,
126
73
  options,
127
- babelOptions: config
74
+ logger,
75
+ babelOptions: config,
76
+ tracer
128
77
  });
129
78
  }
130
79
  }
131
-
132
80
  return [asset];
133
81
  } catch (e) {
134
- throw await (0, _babelAstUtils().babelErrorEnhancer)(e, asset);
82
+ throw await (0, _babelErrorUtils.babelErrorEnhancer)(e, asset);
135
83
  }
136
84
  },
137
-
138
85
  async generate({
139
86
  asset,
140
87
  ast,
@@ -142,40 +89,40 @@ var _default = new (_plugin().Transformer)({
142
89
  }) {
143
90
  let originalSourceMap = await asset.getMap();
144
91
  let sourceFileName = (0, _utils().relativeUrl)(options.projectRoot, asset.filePath);
92
+ const babelCorePath = await options.packageManager.resolve('@babel/core', asset.filePath, {
93
+ range: '^7.12.0',
94
+ saveDev: true,
95
+ shouldAutoInstall: options.shouldAutoInstall
96
+ });
97
+ const {
98
+ default: generate
99
+ } = await options.packageManager.require('@babel/generator', babelCorePath.resolved);
145
100
  let {
146
101
  code,
147
102
  rawMappings
148
- } = (0, _generator().default)(ast.program, {
103
+ } = generate(ast.program, {
149
104
  sourceFileName,
150
105
  sourceMaps: !!asset.env.sourceMap,
151
106
  comments: true
152
107
  });
153
108
  let map = new (_sourceMap().default)(options.projectRoot);
154
-
155
109
  if (rawMappings) {
156
110
  map.addIndexedMappings(rawMappings);
157
111
  }
158
-
159
112
  if (originalSourceMap) {
160
113
  // The babel AST already contains the correct mappings, but not the source contents.
161
114
  // We need to copy over the source contents from the original map.
162
115
  let sourcesContent = originalSourceMap.getSourcesContentMap();
163
-
164
116
  for (let filePath in sourcesContent) {
165
117
  let content = sourcesContent[filePath];
166
-
167
118
  if (content != null) {
168
119
  map.setSourceContent(filePath, content);
169
120
  }
170
121
  }
171
122
  }
172
-
173
123
  return {
174
124
  content: code,
175
125
  map
176
126
  };
177
127
  }
178
-
179
- });
180
-
181
- exports.default = _default;
128
+ });
package/lib/babel7.js CHANGED
@@ -4,97 +4,74 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = babel7;
7
-
8
7
  function _assert() {
9
8
  const data = _interopRequireDefault(require("assert"));
10
-
11
9
  _assert = function () {
12
10
  return data;
13
11
  };
14
-
15
- return data;
16
- }
17
-
18
- function internalBabelCore() {
19
- const data = _interopRequireWildcard(require("@babel/core"));
20
-
21
- internalBabelCore = function () {
22
- return data;
23
- };
24
-
25
12
  return data;
26
13
  }
27
-
28
- function _utils() {
29
- const data = require("@parcel/utils");
30
-
31
- _utils = function () {
14
+ function _path() {
15
+ const data = _interopRequireDefault(require("path"));
16
+ _path = function () {
32
17
  return data;
33
18
  };
34
-
35
19
  return data;
36
20
  }
37
-
38
- function _babelAstUtils() {
39
- const data = require("@parcel/babel-ast-utils");
40
-
41
- _babelAstUtils = function () {
21
+ function _diagnostic() {
22
+ const data = require("@parcel/diagnostic");
23
+ _diagnostic = function () {
42
24
  return data;
43
25
  };
44
-
45
26
  return data;
46
27
  }
47
-
48
- function _package() {
49
- const data = _interopRequireDefault(require("../package.json"));
50
-
51
- _package = function () {
28
+ function _utils() {
29
+ const data = require("@parcel/utils");
30
+ _utils = function () {
52
31
  return data;
53
32
  };
54
-
55
33
  return data;
56
34
  }
57
-
58
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
59
-
60
- 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; }
61
-
35
+ var _remapAstLocations = require("./remapAstLocations");
36
+ var _package = _interopRequireDefault(require("../package.json"));
62
37
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
63
-
64
- const transformerVersion = _package().default.version;
65
-
38
+ const transformerVersion = _package.default.version;
66
39
  (0, _assert().default)(typeof transformerVersion === 'string');
67
-
68
40
  async function babel7(opts) {
69
41
  var _babelOptions$config$, _babelOptions$config$2, _babelOptions$syntaxP;
70
-
71
42
  let {
72
43
  asset,
73
44
  options,
74
45
  babelOptions,
75
- additionalPlugins = []
46
+ additionalPlugins = [],
47
+ tracer
76
48
  } = opts;
77
- const babelCore = babelOptions.internal ? internalBabelCore() : await options.packageManager.require('@babel/core', asset.filePath, {
49
+ const babelCore = await options.packageManager.require('@babel/core', asset.filePath, {
78
50
  range: '^7.12.0',
79
51
  saveDev: true,
80
52
  shouldAutoInstall: options.shouldAutoInstall
81
53
  });
82
- let config = { ...babelOptions.config,
54
+ let config = {
55
+ ...babelOptions.config,
83
56
  plugins: additionalPlugins.concat(babelOptions.config.plugins),
84
57
  code: false,
85
58
  ast: true,
86
59
  filename: asset.filePath,
87
60
  babelrc: false,
88
61
  configFile: false,
89
- parserOpts: { ...babelOptions.config.parserOpts,
62
+ parserOpts: {
63
+ ...babelOptions.config.parserOpts,
90
64
  sourceFilename: (0, _utils().relativeUrl)(options.projectRoot, asset.filePath),
91
65
  allowReturnOutsideFunction: true,
92
66
  strictMode: false,
93
67
  sourceType: 'module',
94
- 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
95
- 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom' // 'topLevelAwait'
68
+ 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 : []),
69
+ // Applied by preset-env
70
+ 'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom'
71
+ // 'topLevelAwait'
96
72
  ]
97
73
  },
74
+
98
75
  caller: {
99
76
  name: 'parcel',
100
77
  version: transformerVersion,
@@ -102,23 +79,50 @@ async function babel7(opts) {
102
79
  outputFormat: asset.env.outputFormat
103
80
  }
104
81
  };
82
+ if (tracer.enabled) {
83
+ config.wrapPluginVisitorMethod = (key, nodeType, fn) => {
84
+ return function () {
85
+ let pluginKey = key;
86
+ if (pluginKey.startsWith(options.projectRoot)) {
87
+ pluginKey = _path().default.relative(options.projectRoot, pluginKey);
88
+ }
89
+ const measurement = tracer.createMeasurement(pluginKey, nodeType, _path().default.relative(options.projectRoot, asset.filePath));
90
+ fn.apply(this, arguments);
91
+ measurement && measurement.end();
92
+ };
93
+ };
94
+ }
105
95
  let ast = await asset.getAST();
106
96
  let res;
107
-
108
97
  if (ast) {
109
98
  res = await babelCore.transformFromAstAsync(ast.program, asset.isASTDirty() ? undefined : await asset.getCode(), config);
110
99
  } else {
111
100
  res = await babelCore.transformAsync(await asset.getCode(), config);
112
-
113
101
  if (res.ast) {
114
102
  let map = await asset.getMap();
115
-
116
103
  if (map) {
117
- (0, _babelAstUtils().remapAstLocations)(res.ast, map);
104
+ (0, _remapAstLocations.remapAstLocations)(babelCore.types, res.ast, map);
105
+ }
106
+ }
107
+ if (res.externalDependencies) {
108
+ for (let f of res.externalDependencies) {
109
+ if (!_path().default.isAbsolute(f)) {
110
+ opts.logger.warn({
111
+ message: (0, _diagnostic().md)`Ignoring non-absolute Babel external dependency: ${f}`,
112
+ hints: ['Please report this to the corresponding Babel plugin and/or to Parcel.']
113
+ });
114
+ } else {
115
+ if (await options.inputFS.exists(f)) {
116
+ asset.invalidateOnFileChange(f);
117
+ } else {
118
+ asset.invalidateOnFileCreate({
119
+ filePath: f
120
+ });
121
+ }
122
+ }
118
123
  }
119
124
  }
120
125
  }
121
-
122
126
  if (res.ast) {
123
127
  asset.setAST({
124
128
  type: 'babel',
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.babelErrorEnhancer = babelErrorEnhancer;
7
+ async function babelErrorEnhancer(error, asset) {
8
+ if (error.loc) {
9
+ let start = error.message.startsWith(asset.filePath) ? asset.filePath.length + 1 : 0;
10
+ error.message = error.message.slice(start).split('\n')[0].trim();
11
+ }
12
+ error.source = await asset.getCode();
13
+ error.filePath = asset.filePath;
14
+ return error;
15
+ }