@parcel/transformer-babel 2.0.0-nightly.142 → 2.0.0-nightly.1421
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/BabelTransformer.js +90 -64
- package/lib/babel7.js +102 -38
- package/lib/babelErrorUtils.js +2 -3
- package/lib/config.js +266 -236
- package/lib/constants.js +3 -3
- package/lib/flow.js +31 -4
- package/lib/jsx.js +25 -46
- package/lib/remapAstLocations.js +55 -0
- package/lib/utils.js +36 -29
- package/package.json +18 -19
- package/src/BabelTransformer.js +68 -41
- package/src/babel7.js +97 -19
- package/src/babelErrorUtils.js +4 -3
- package/src/config.js +343 -218
- package/src/constants.js +1 -1
- package/src/flow.js +50 -3
- package/src/jsx.js +29 -44
- package/src/remapAstLocations.js +70 -0
- package/src/types.js +0 -7
- package/src/utils.js +18 -15
- package/lib/astConverter.js +0 -106
- package/lib/babel6.js +0 -47
- package/lib/env.js +0 -63
- package/lib/getBabelTargets.js +0 -86
- package/lib/typescript.js +0 -18
- package/src/astConverter.js +0 -102
- package/src/babel6.js +0 -52
- package/src/env.js +0 -70
- package/src/getBabelTargets.js +0 -97
- package/src/typescript.js +0 -16
package/lib/jsx.js
CHANGED
|
@@ -3,56 +3,35 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
exports.default = isJSX;
|
|
7
|
+
function _path() {
|
|
8
|
+
const data = _interopRequireDefault(require("path"));
|
|
9
|
+
_path = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
10
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
const JSX_EXTENSIONS = new Set(['.jsx', '.tsx']);
|
|
16
|
+
const JSX_LIBRARIES = ['react', 'preact', 'nervejs', 'hyperapp'];
|
|
11
17
|
|
|
12
|
-
const JSX_EXTENSIONS = {
|
|
13
|
-
'.jsx': true,
|
|
14
|
-
'.tsx': true
|
|
15
|
-
};
|
|
16
|
-
const JSX_PRAGMA = {
|
|
17
|
-
react: {
|
|
18
|
-
pragma: 'React.createElement',
|
|
19
|
-
pragmaFrag: 'React.Fragment'
|
|
20
|
-
},
|
|
21
|
-
preact: {
|
|
22
|
-
pragma: 'h',
|
|
23
|
-
pragmaFrag: 'Fragment'
|
|
24
|
-
},
|
|
25
|
-
nervjs: {
|
|
26
|
-
pragma: 'Nerv.createElement',
|
|
27
|
-
pragmaFrag: undefined
|
|
28
|
-
},
|
|
29
|
-
hyperapp: {
|
|
30
|
-
pragma: 'h',
|
|
31
|
-
pragmaFrag: undefined
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
18
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
19
|
+
* Returns whether an asset is likely JSX. Attempts to detect react or react-like libraries
|
|
20
|
+
* along with
|
|
37
21
|
*/
|
|
38
|
-
|
|
39
|
-
async function getJSXOptions(config) {
|
|
22
|
+
async function isJSX(options, config) {
|
|
40
23
|
if (!config.isSource) {
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
pragma,
|
|
54
|
-
pragmaFrag
|
|
55
|
-
}]]
|
|
56
|
-
};
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (JSX_EXTENSIONS.has(_path().default.extname(config.searchPath))) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
let pkg = await config.getPackage();
|
|
30
|
+
if (pkg !== null && pkg !== void 0 && pkg.alias && pkg.alias['react']) {
|
|
31
|
+
// e.g.: `{ alias: { "react": "preact/compat" } }`
|
|
32
|
+
return true;
|
|
33
|
+
} else {
|
|
34
|
+
// Find a dependency that implies JSX syntax.
|
|
35
|
+
return JSX_LIBRARIES.some(libName => pkg && (pkg.dependencies && pkg.dependencies[libName] || pkg.devDependencies && pkg.devDependencies[libName] || pkg.peerDependencies && pkg.peerDependencies[libName]));
|
|
57
36
|
}
|
|
58
37
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.remapAstLocations = remapAstLocations;
|
|
7
|
+
function remapAstLocations(t, ast, map) {
|
|
8
|
+
// remap ast to original mappings
|
|
9
|
+
// This improves sourcemap accuracy and fixes sourcemaps when scope-hoisting
|
|
10
|
+
traverseAll(t, ast.program, node => {
|
|
11
|
+
if (node.loc) {
|
|
12
|
+
var _node$loc;
|
|
13
|
+
if ((_node$loc = node.loc) !== null && _node$loc !== void 0 && _node$loc.start) {
|
|
14
|
+
let mapping = map.findClosestMapping(node.loc.start.line, node.loc.start.column);
|
|
15
|
+
if (mapping !== null && mapping !== void 0 && mapping.original) {
|
|
16
|
+
// $FlowFixMe
|
|
17
|
+
node.loc.start.line = mapping.original.line;
|
|
18
|
+
// $FlowFixMe
|
|
19
|
+
node.loc.start.column = mapping.original.column;
|
|
20
|
+
|
|
21
|
+
// $FlowFixMe
|
|
22
|
+
let length = node.loc.end.column - node.loc.start.column;
|
|
23
|
+
|
|
24
|
+
// $FlowFixMe
|
|
25
|
+
node.loc.end.line = mapping.original.line;
|
|
26
|
+
// $FlowFixMe
|
|
27
|
+
node.loc.end.column = mapping.original.column + length;
|
|
28
|
+
|
|
29
|
+
// $FlowFixMe
|
|
30
|
+
node.loc.filename = mapping.source;
|
|
31
|
+
} else {
|
|
32
|
+
// Maintain null mappings?
|
|
33
|
+
node.loc = null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function traverseAll(t, node, visitor) {
|
|
40
|
+
if (!node) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
visitor(node);
|
|
44
|
+
for (let key of t.VISITOR_KEYS[node.type] || []) {
|
|
45
|
+
// $FlowFixMe
|
|
46
|
+
let subNode = node[key];
|
|
47
|
+
if (Array.isArray(subNode)) {
|
|
48
|
+
for (let i = 0; i < subNode.length; i++) {
|
|
49
|
+
traverseAll(t, subNode[i], visitor);
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
traverseAll(t, subNode, visitor);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/lib/utils.js
CHANGED
|
@@ -4,41 +4,61 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.enginesToBabelTargets = enginesToBabelTargets;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
function _assert() {
|
|
8
|
+
const data = _interopRequireDefault(require("assert"));
|
|
9
|
+
_assert = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _semver() {
|
|
15
|
+
const data = _interopRequireDefault(require("semver"));
|
|
16
|
+
_semver = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
15
21
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
// Copied from @babel/helper-compilation-targets/lib/options.js
|
|
23
|
+
const TargetNames = {
|
|
24
|
+
node: 'node',
|
|
25
|
+
chrome: 'chrome',
|
|
26
|
+
opera: 'opera',
|
|
27
|
+
edge: 'edge',
|
|
28
|
+
firefox: 'firefox',
|
|
29
|
+
safari: 'safari',
|
|
30
|
+
ie: 'ie',
|
|
31
|
+
ios: 'ios',
|
|
32
|
+
android: 'android',
|
|
33
|
+
electron: 'electron',
|
|
34
|
+
samsung: 'samsung',
|
|
35
|
+
rhino: 'rhino'
|
|
36
|
+
};
|
|
16
37
|
|
|
17
38
|
// List of browsers to exclude when the esmodule target is specified.
|
|
18
39
|
// Based on https://caniuse.com/#feat=es6-module
|
|
19
40
|
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'];
|
|
20
|
-
|
|
21
41
|
function enginesToBabelTargets(env) {
|
|
22
42
|
// "Targets" is the name @babel/preset-env uses for what Parcel calls engines.
|
|
23
43
|
// This should not be confused with Parcel's own targets.
|
|
24
44
|
// Unlike Parcel's engines, @babel/preset-env expects to work with minimum
|
|
25
45
|
// versions, not semver ranges, of its targets.
|
|
26
46
|
let targets = {};
|
|
27
|
-
|
|
28
47
|
for (let engineName of Object.keys(env.engines)) {
|
|
29
|
-
let engineValue = env.engines[engineName];
|
|
30
|
-
// possible version instead.
|
|
48
|
+
let engineValue = env.engines[engineName];
|
|
31
49
|
|
|
50
|
+
// if the engineValue is a string, it might be a semver range. Use the minimum
|
|
51
|
+
// possible version instead.
|
|
32
52
|
if (engineName === 'browsers') {
|
|
33
53
|
targets[engineName] = engineValue;
|
|
34
54
|
} else {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
55
|
+
var _semver$minVersion;
|
|
56
|
+
(0, _assert().default)(typeof engineValue === 'string');
|
|
57
|
+
if (!TargetNames.hasOwnProperty(engineName)) continue;
|
|
58
|
+
let minVersion = (_semver$minVersion = _semver().default.minVersion(engineValue)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString();
|
|
38
59
|
targets[engineName] = minVersion !== null && minVersion !== void 0 ? minVersion : engineValue;
|
|
39
60
|
}
|
|
40
61
|
}
|
|
41
|
-
|
|
42
62
|
if (env.outputFormat === 'esmodule' && env.isBrowser()) {
|
|
43
63
|
// If there is already a browsers target, add a blacklist to exclude
|
|
44
64
|
// instead of using babel's esmodules target. This allows specifying
|
|
@@ -51,18 +71,5 @@ function enginesToBabelTargets(env) {
|
|
|
51
71
|
targets.esmodules = true;
|
|
52
72
|
}
|
|
53
73
|
}
|
|
54
|
-
|
|
55
74
|
return targets;
|
|
56
|
-
} // TODO: Replace with `minVersion` (https://github.com/npm/node-semver#ranges-1)
|
|
57
|
-
// once semver has been upgraded across Parcel.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
function getMinSemver(version) {
|
|
61
|
-
try {
|
|
62
|
-
let range = new _semver.default.Range(version);
|
|
63
|
-
let sorted = range.set.sort((a, b) => a[0].semver.compare(b[0].semver));
|
|
64
|
-
return sorted[0][0].semver.version;
|
|
65
|
-
} catch (err) {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
75
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/transformer-babel",
|
|
3
|
-
"version": "2.0.0-nightly.
|
|
3
|
+
"version": "2.0.0-nightly.1421+a1391ed8a",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
+
"funding": {
|
|
9
|
+
"type": "opencollective",
|
|
10
|
+
"url": "https://opencollective.com/parcel"
|
|
11
|
+
},
|
|
8
12
|
"repository": {
|
|
9
13
|
"type": "git",
|
|
10
14
|
"url": "https://github.com/parcel-bundler/parcel.git"
|
|
@@ -12,29 +16,24 @@
|
|
|
12
16
|
"main": "lib/BabelTransformer.js",
|
|
13
17
|
"source": "src/BabelTransformer.js",
|
|
14
18
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
16
|
-
"parcel": "
|
|
19
|
+
"node": ">= 12.0.0",
|
|
20
|
+
"parcel": "2.0.0-nightly.1419+a1391ed8a"
|
|
17
21
|
},
|
|
18
22
|
"dependencies": {
|
|
19
|
-
"@
|
|
20
|
-
"@
|
|
21
|
-
"@
|
|
22
|
-
"@
|
|
23
|
-
"@babel/plugin-transform-react-jsx": "^7.0.0",
|
|
24
|
-
"@babel/plugin-transform-typescript": "^7.4.5",
|
|
25
|
-
"@babel/preset-env": "^7.0.0",
|
|
26
|
-
"@babel/traverse": "^7.0.0",
|
|
27
|
-
"@parcel/babel-preset-env": "2.0.0-nightly.142+c87c29dd",
|
|
28
|
-
"@parcel/plugin": "2.0.0-nightly.142+c87c29dd",
|
|
29
|
-
"@parcel/source-map": "2.0.0-nightly.142+c87c29dd",
|
|
30
|
-
"@parcel/utils": "2.0.0-nightly.142+c87c29dd",
|
|
23
|
+
"@parcel/diagnostic": "2.0.0-nightly.1421+a1391ed8a",
|
|
24
|
+
"@parcel/plugin": "2.0.0-nightly.1421+a1391ed8a",
|
|
25
|
+
"@parcel/source-map": "^2.1.1",
|
|
26
|
+
"@parcel/utils": "2.0.0-nightly.1421+a1391ed8a",
|
|
31
27
|
"browserslist": "^4.6.6",
|
|
32
|
-
"
|
|
28
|
+
"json5": "^2.2.0",
|
|
33
29
|
"nullthrows": "^1.1.1",
|
|
34
|
-
"semver": "^5.
|
|
30
|
+
"semver": "^7.5.2"
|
|
35
31
|
},
|
|
36
32
|
"devDependencies": {
|
|
37
|
-
"@
|
|
33
|
+
"@babel/core": "^7.22.11",
|
|
34
|
+
"@babel/preset-env": "^7.22.14",
|
|
35
|
+
"@babel/types": "^7.22.11",
|
|
36
|
+
"@parcel/types": "2.0.0-nightly.1421+a1391ed8a"
|
|
38
37
|
},
|
|
39
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "a1391ed8a719fc2f976dbadb528ca2dcffa7da12"
|
|
40
39
|
}
|
package/src/BabelTransformer.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
// @flow
|
|
1
|
+
// @flow strict-local
|
|
2
2
|
|
|
3
|
+
import {babelErrorEnhancer} from './babelErrorUtils';
|
|
3
4
|
import {Transformer} from '@parcel/plugin';
|
|
5
|
+
import {relativeUrl} from '@parcel/utils';
|
|
4
6
|
import SourceMap from '@parcel/source-map';
|
|
5
|
-
import generate from '@babel/generator';
|
|
6
7
|
import semver from 'semver';
|
|
7
8
|
import babel7 from './babel7';
|
|
8
|
-
import {
|
|
9
|
-
import {load, preSerialize, postDeserialize} from './config';
|
|
10
|
-
import {babelErrorEnhancer} from './babelErrorUtils';
|
|
11
|
-
|
|
12
|
-
export default new Transformer({
|
|
13
|
-
async loadConfig({config, options, logger}) {
|
|
14
|
-
await load(config, options, logger);
|
|
15
|
-
},
|
|
9
|
+
import {load} from './config';
|
|
16
10
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
postDeserializeConfig({config, options}) {
|
|
22
|
-
return postDeserialize(config, options);
|
|
11
|
+
export default (new Transformer({
|
|
12
|
+
loadConfig({config, options, logger}) {
|
|
13
|
+
return load(config, options, logger);
|
|
23
14
|
},
|
|
24
15
|
|
|
25
16
|
canReuseAST({ast}) {
|
|
26
17
|
return ast.type === 'babel' && semver.satisfies(ast.version, '^7.0.0');
|
|
27
18
|
},
|
|
28
19
|
|
|
29
|
-
async transform({asset, config, options}) {
|
|
30
|
-
// TODO: come up with a better name
|
|
20
|
+
async transform({asset, config, logger, options, tracer}) {
|
|
31
21
|
try {
|
|
32
22
|
if (config?.config) {
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
asset.
|
|
23
|
+
if (
|
|
24
|
+
asset.meta.babelPlugins != null &&
|
|
25
|
+
Array.isArray(asset.meta.babelPlugins)
|
|
26
|
+
) {
|
|
27
|
+
await babel7({
|
|
36
28
|
asset,
|
|
37
29
|
options,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
30
|
+
logger,
|
|
31
|
+
babelOptions: config,
|
|
32
|
+
additionalPlugins: asset.meta.babelPlugins,
|
|
33
|
+
tracer,
|
|
34
|
+
});
|
|
41
35
|
} else {
|
|
42
|
-
|
|
36
|
+
await babel7({
|
|
37
|
+
asset,
|
|
38
|
+
options,
|
|
39
|
+
logger,
|
|
40
|
+
babelOptions: config,
|
|
41
|
+
tracer,
|
|
42
|
+
});
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -49,27 +49,54 @@ export default new Transformer({
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
|
|
52
|
-
async generate({asset, options}) {
|
|
52
|
+
async generate({asset, ast, options}) {
|
|
53
|
+
let originalSourceMap = await asset.getMap();
|
|
53
54
|
let sourceFileName: string = relativeUrl(
|
|
54
55
|
options.projectRoot,
|
|
55
56
|
asset.filePath,
|
|
56
57
|
);
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
const babelCorePath = await options.packageManager.resolve(
|
|
60
|
+
'@babel/core',
|
|
61
|
+
asset.filePath,
|
|
62
|
+
{
|
|
63
|
+
range: '^7.12.0',
|
|
64
|
+
saveDev: true,
|
|
65
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
66
|
+
},
|
|
67
|
+
);
|
|
64
68
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
const {default: generate} = await options.packageManager.require(
|
|
70
|
+
'@babel/generator',
|
|
71
|
+
babelCorePath.resolved,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
let {code, rawMappings} = generate(ast.program, {
|
|
75
|
+
sourceFileName,
|
|
76
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
77
|
+
comments: true,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
let map = new SourceMap(options.projectRoot);
|
|
81
|
+
if (rawMappings) {
|
|
82
|
+
map.addIndexedMappings(rawMappings);
|
|
73
83
|
}
|
|
84
|
+
|
|
85
|
+
if (originalSourceMap) {
|
|
86
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
87
|
+
// We need to copy over the source contents from the original map.
|
|
88
|
+
let sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
89
|
+
for (let filePath in sourcesContent) {
|
|
90
|
+
let content = sourcesContent[filePath];
|
|
91
|
+
if (content != null) {
|
|
92
|
+
map.setSourceContent(filePath, content);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
content: code,
|
|
99
|
+
map,
|
|
100
|
+
};
|
|
74
101
|
},
|
|
75
|
-
});
|
|
102
|
+
}): Transformer);
|
package/src/babel7.js
CHANGED
|
@@ -1,28 +1,47 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
MutableAsset,
|
|
5
|
+
AST,
|
|
6
|
+
PluginOptions,
|
|
7
|
+
PluginTracer,
|
|
8
|
+
PluginLogger,
|
|
9
|
+
} from '@parcel/types';
|
|
10
|
+
import typeof * as BabelCore from '@babel/core';
|
|
4
11
|
|
|
5
12
|
import invariant from 'assert';
|
|
13
|
+
import path from 'path';
|
|
14
|
+
import {md} from '@parcel/diagnostic';
|
|
15
|
+
import {relativeUrl} from '@parcel/utils';
|
|
16
|
+
import {remapAstLocations} from './remapAstLocations';
|
|
6
17
|
|
|
7
|
-
import {BABEL_RANGE} from './constants';
|
|
8
18
|
import packageJson from '../package.json';
|
|
9
19
|
|
|
10
20
|
const transformerVersion: mixed = packageJson.version;
|
|
11
21
|
invariant(typeof transformerVersion === 'string');
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
type Babel7TransformOptions = {|
|
|
14
24
|
asset: MutableAsset,
|
|
15
25
|
options: PluginOptions,
|
|
26
|
+
logger: PluginLogger,
|
|
16
27
|
babelOptions: any,
|
|
17
|
-
additionalPlugins
|
|
28
|
+
additionalPlugins?: Array<any>,
|
|
29
|
+
tracer: PluginTracer,
|
|
30
|
+
|};
|
|
31
|
+
|
|
32
|
+
export default async function babel7(
|
|
33
|
+
opts: Babel7TransformOptions,
|
|
18
34
|
): Promise<?AST> {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
let {asset, options, babelOptions, additionalPlugins = [], tracer} = opts;
|
|
36
|
+
const babelCore: BabelCore = await options.packageManager.require(
|
|
37
|
+
'@babel/core',
|
|
38
|
+
asset.filePath,
|
|
39
|
+
{
|
|
40
|
+
range: '^7.12.0',
|
|
41
|
+
saveDev: true,
|
|
42
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
43
|
+
},
|
|
44
|
+
);
|
|
26
45
|
|
|
27
46
|
let config = {
|
|
28
47
|
...babelOptions.config,
|
|
@@ -34,33 +53,92 @@ export default async function babel7(
|
|
|
34
53
|
configFile: false,
|
|
35
54
|
parserOpts: {
|
|
36
55
|
...babelOptions.config.parserOpts,
|
|
56
|
+
sourceFilename: relativeUrl(options.projectRoot, asset.filePath),
|
|
37
57
|
allowReturnOutsideFunction: true,
|
|
38
58
|
strictMode: false,
|
|
39
59
|
sourceType: 'module',
|
|
40
|
-
plugins: [
|
|
60
|
+
plugins: [
|
|
61
|
+
...(babelOptions.config.parserOpts?.plugins ?? []),
|
|
62
|
+
...(babelOptions.syntaxPlugins ?? []),
|
|
63
|
+
// Applied by preset-env
|
|
64
|
+
'classProperties',
|
|
65
|
+
'classPrivateProperties',
|
|
66
|
+
'classPrivateMethods',
|
|
67
|
+
'exportDefaultFrom',
|
|
68
|
+
// 'topLevelAwait'
|
|
69
|
+
],
|
|
41
70
|
},
|
|
42
71
|
caller: {
|
|
43
72
|
name: 'parcel',
|
|
44
73
|
version: transformerVersion,
|
|
45
74
|
targets: JSON.stringify(babelOptions.targets),
|
|
75
|
+
outputFormat: asset.env.outputFormat,
|
|
46
76
|
},
|
|
47
77
|
};
|
|
48
78
|
|
|
49
|
-
|
|
79
|
+
if (tracer.enabled) {
|
|
80
|
+
config.wrapPluginVisitorMethod = (
|
|
81
|
+
key: string,
|
|
82
|
+
nodeType: string,
|
|
83
|
+
fn: Function,
|
|
84
|
+
) => {
|
|
85
|
+
return function () {
|
|
86
|
+
let pluginKey = key;
|
|
87
|
+
if (pluginKey.startsWith(options.projectRoot)) {
|
|
88
|
+
pluginKey = path.relative(options.projectRoot, pluginKey);
|
|
89
|
+
}
|
|
90
|
+
const measurement = tracer.createMeasurement(
|
|
91
|
+
pluginKey,
|
|
92
|
+
nodeType,
|
|
93
|
+
path.relative(options.projectRoot, asset.filePath),
|
|
94
|
+
);
|
|
95
|
+
fn.apply(this, arguments);
|
|
96
|
+
measurement && measurement.end();
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
}
|
|
50
100
|
|
|
101
|
+
let ast = await asset.getAST();
|
|
51
102
|
let res;
|
|
52
|
-
if (
|
|
53
|
-
res =
|
|
103
|
+
if (ast) {
|
|
104
|
+
res = await babelCore.transformFromAstAsync(
|
|
105
|
+
ast.program,
|
|
106
|
+
asset.isASTDirty() ? undefined : await asset.getCode(),
|
|
107
|
+
config,
|
|
108
|
+
);
|
|
54
109
|
} else {
|
|
55
|
-
res =
|
|
110
|
+
res = await babelCore.transformAsync(await asset.getCode(), config);
|
|
111
|
+
if (res.ast) {
|
|
112
|
+
let map = await asset.getMap();
|
|
113
|
+
if (map) {
|
|
114
|
+
remapAstLocations(babelCore.types, res.ast, map);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (res.externalDependencies) {
|
|
118
|
+
for (let f of res.externalDependencies) {
|
|
119
|
+
if (!path.isAbsolute(f)) {
|
|
120
|
+
opts.logger.warn({
|
|
121
|
+
message: md`Ignoring non-absolute Babel external dependency: ${f}`,
|
|
122
|
+
hints: [
|
|
123
|
+
'Please report this to the corresponding Babel plugin and/or to Parcel.',
|
|
124
|
+
],
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
if (await options.inputFS.exists(f)) {
|
|
128
|
+
asset.invalidateOnFileChange(f);
|
|
129
|
+
} else {
|
|
130
|
+
asset.invalidateOnFileCreate({filePath: f});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
56
135
|
}
|
|
57
136
|
|
|
58
137
|
if (res.ast) {
|
|
59
|
-
|
|
138
|
+
asset.setAST({
|
|
60
139
|
type: 'babel',
|
|
61
140
|
version: '7.0.0',
|
|
62
141
|
program: res.ast,
|
|
63
|
-
|
|
64
|
-
};
|
|
142
|
+
});
|
|
65
143
|
}
|
|
66
144
|
}
|
package/src/babelErrorUtils.js
CHANGED
|
@@ -17,9 +17,10 @@ export async function babelErrorEnhancer(
|
|
|
17
17
|
asset: BaseAsset,
|
|
18
18
|
): Promise<BabelError> {
|
|
19
19
|
if (error.loc) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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();
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
error.source = await asset.getCode();
|