@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.
- package/lib/BabelTransformer.js +70 -32
- package/lib/babel7.js +31 -28
- package/lib/babelErrorUtils.js +17 -0
- package/lib/config.js +219 -129
- package/lib/constants.js +3 -3
- package/lib/flow.js +29 -2
- package/lib/jsx.js +14 -48
- package/lib/remapAstLocations.js +59 -0
- package/lib/utils.js +20 -26
- package/package.json +11 -18
- package/src/BabelTransformer.js +61 -12
- package/src/babel7.js +37 -5
- package/src/babelErrorUtils.js +30 -0
- package/src/config.js +273 -106
- package/src/constants.js +1 -1
- package/src/flow.js +36 -5
- package/src/jsx.js +16 -52
- package/src/remapAstLocations.js +70 -0
- package/src/utils.js +17 -14
- package/lib/astConverter.js +0 -114
- package/lib/env.js +0 -88
- package/lib/getBabelTargets.js +0 -104
- package/lib/typescript.js +0 -29
- package/src/astConverter.js +0 -102
- package/src/env.js +0 -69
- package/src/getBabelTargets.js +0 -97
- package/src/typescript.js +0 -25
|
@@ -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
|
-
|
|
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
|
|
@@ -47,7 +62,7 @@ export function enginesToBabelTargets(env: Environment): BabelTargets {
|
|
|
47
62
|
} else {
|
|
48
63
|
invariant(typeof engineValue === 'string');
|
|
49
64
|
if (!TargetNames.hasOwnProperty(engineName)) continue;
|
|
50
|
-
let minVersion =
|
|
65
|
+
let minVersion = semver.minVersion(engineValue)?.toString();
|
|
51
66
|
targets[engineName] = minVersion ?? engineValue;
|
|
52
67
|
}
|
|
53
68
|
}
|
|
@@ -69,15 +84,3 @@ export function enginesToBabelTargets(env: Environment): BabelTargets {
|
|
|
69
84
|
|
|
70
85
|
return targets;
|
|
71
86
|
}
|
|
72
|
-
|
|
73
|
-
// TODO: Replace with `minVersion` (https://github.com/npm/node-semver#ranges-1)
|
|
74
|
-
// once semver has been upgraded across Parcel.
|
|
75
|
-
export function getMinSemver(version: string): ?string {
|
|
76
|
-
try {
|
|
77
|
-
let range = new semver.Range(version);
|
|
78
|
-
let sorted = range.set.sort((a, b) => a[0].semver.compare(b[0].semver));
|
|
79
|
-
return sorted[0][0].semver.version;
|
|
80
|
-
} catch (err) {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
}
|
package/lib/astConverter.js
DELETED
|
@@ -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
|
-
}
|
package/lib/env.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = getEnvOptions;
|
|
7
|
-
|
|
8
|
-
function _presetEnv() {
|
|
9
|
-
const data = _interopRequireDefault(require("@babel/preset-env"));
|
|
10
|
-
|
|
11
|
-
_presetEnv = function () {
|
|
12
|
-
return data;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
return data;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function _getBabelTargets() {
|
|
19
|
-
const data = _interopRequireDefault(require("./getBabelTargets"));
|
|
20
|
-
|
|
21
|
-
_getBabelTargets = function () {
|
|
22
|
-
return data;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
return data;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function _utils() {
|
|
29
|
-
const data = require("./utils");
|
|
30
|
-
|
|
31
|
-
_utils = function () {
|
|
32
|
-
return data;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
return data;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Generates a @babel/preset-env config for an asset.
|
|
42
|
-
* This is done by finding the source module's target engines, and the app's
|
|
43
|
-
* target engines, and doing a diff to include only the necessary plugins.
|
|
44
|
-
*/
|
|
45
|
-
async function getEnvOptions(config) {
|
|
46
|
-
// Only compile if there are engines defined in the environment.
|
|
47
|
-
if (Object.keys(config.env.engines).length === 0) {
|
|
48
|
-
return null;
|
|
49
|
-
} // Load the target engines for the app and generate a @babel/preset-env config
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
let appBabelTargets = (0, _utils().enginesToBabelTargets)(config.env); // If this is the app module, the source and target will be the same, so just compile everything.
|
|
53
|
-
// Otherwise, load the source engines and generate a babel-present-env config.
|
|
54
|
-
|
|
55
|
-
if (!config.isSource) {
|
|
56
|
-
let sourceBabelTargets = await (0, _getBabelTargets().default)(config);
|
|
57
|
-
|
|
58
|
-
if (!sourceBabelTargets || !shouldCompileFurther(sourceBabelTargets, appBabelTargets)) {
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return {
|
|
64
|
-
targets: appBabelTargets,
|
|
65
|
-
config: {
|
|
66
|
-
presets: ['@parcel/babel-preset-env']
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function getNeededPlugins(targets) {
|
|
72
|
-
return (0, _presetEnv().default)({
|
|
73
|
-
assertVersion: () => true
|
|
74
|
-
}, {
|
|
75
|
-
targets: targets
|
|
76
|
-
}).plugins.filter(p => p[0]);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function shouldCompileFurther(sourceBabelTargets, appBabelTargets) {
|
|
80
|
-
let sourcePlugins = new Set(getNeededPlugins(sourceBabelTargets));
|
|
81
|
-
let appPlugins = getNeededPlugins(appBabelTargets); // If there is any app plugin present that was not used to compile the source,
|
|
82
|
-
// then the asset was built to a higher target and will need to be compiled
|
|
83
|
-
// further
|
|
84
|
-
|
|
85
|
-
return appPlugins.some(plugin => {
|
|
86
|
-
return !sourcePlugins.has(plugin);
|
|
87
|
-
});
|
|
88
|
-
}
|
package/lib/getBabelTargets.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = getBabelTargets;
|
|
7
|
-
|
|
8
|
-
function _browserslist() {
|
|
9
|
-
const data = _interopRequireDefault(require("browserslist"));
|
|
10
|
-
|
|
11
|
-
_browserslist = function () {
|
|
12
|
-
return data;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
return data;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function _semver() {
|
|
19
|
-
const data = _interopRequireDefault(require("semver"));
|
|
20
|
-
|
|
21
|
-
_semver = function () {
|
|
22
|
-
return data;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
return data;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
-
|
|
30
|
-
const BROWSER_CONTEXT = new Set(['browser', 'web-worker', 'service-worker']); // https://github.com/browserslist/browserslist/blob/04d7b3cc6ebeb62baf6d62aeb90f1bfd0c11117c/test/config.test.js#L23
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Loads target node and browser versions from the following locations:
|
|
34
|
-
* - package.json engines field
|
|
35
|
-
* - package.json browserslist field
|
|
36
|
-
* - browserslist or .browserslistrc files
|
|
37
|
-
*
|
|
38
|
-
* "Targets" is the name @babel/preset-env uses for what Parcel calls engines.
|
|
39
|
-
* This should not be confused with Parcel's own targets.
|
|
40
|
-
* Unlike Parcel's engines, @babel/preset-env expects to work with minimum
|
|
41
|
-
* versions, not semver ranges, of its targets.
|
|
42
|
-
*/
|
|
43
|
-
async function getBabelTargets(config) {
|
|
44
|
-
let targets = {};
|
|
45
|
-
let compileTarget = BROWSER_CONTEXT.has(config.env.context) ? 'browsers' : config.env.context;
|
|
46
|
-
let pkg = await config.getPackage();
|
|
47
|
-
let packageEngines = pkg === null || pkg === void 0 ? void 0 : pkg.engines;
|
|
48
|
-
|
|
49
|
-
if (compileTarget === 'node') {
|
|
50
|
-
let nodeVersion = packageEngines === null || packageEngines === void 0 ? void 0 : packageEngines.node; // Use package.engines.node by default if we are compiling for node.
|
|
51
|
-
|
|
52
|
-
if (typeof nodeVersion === 'string') {
|
|
53
|
-
try {
|
|
54
|
-
//$FlowFixMe catch error when minVersion() returned null
|
|
55
|
-
targets.node = _semver().default.minVersion(nodeVersion).version;
|
|
56
|
-
} catch (e) {
|
|
57
|
-
throw new Error("Expected 'node' engine to be a valid Semver Range");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
} else {
|
|
61
|
-
let browsers;
|
|
62
|
-
|
|
63
|
-
if (packageEngines && (typeof packageEngines.browsers === 'string' || Array.isArray(packageEngines.browsers))) {
|
|
64
|
-
browsers = packageEngines.browsers;
|
|
65
|
-
} else if (pkg && pkg.browserslist) {
|
|
66
|
-
browsers = pkg.browserslist;
|
|
67
|
-
} else {
|
|
68
|
-
let browserslist = await loadBrowserslist(config);
|
|
69
|
-
|
|
70
|
-
if (browserslist) {
|
|
71
|
-
browsers = browserslist;
|
|
72
|
-
}
|
|
73
|
-
} // Parse browser targets
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (typeof browsers === 'object' && browsers != null && !Array.isArray(browsers)) {
|
|
77
|
-
var _process$env$NODE_ENV;
|
|
78
|
-
|
|
79
|
-
let env = (_process$env$NODE_ENV = process.env.NODE_ENV) !== null && _process$env$NODE_ENV !== void 0 ? _process$env$NODE_ENV : 'development';
|
|
80
|
-
browsers = browsers[env] || browsers.defaults;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (browsers != null) {
|
|
84
|
-
targets.browsers = (0, _browserslist().default)(browsers).sort();
|
|
85
|
-
}
|
|
86
|
-
} // Dont compile if we couldn't find any targets
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (Object.keys(targets).length === 0) {
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return targets;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function loadBrowserslist(config) {
|
|
97
|
-
let browserslistConfig = await config.getConfig(['browserslist', '.browserslistrc'], {
|
|
98
|
-
parse: false
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
if (browserslistConfig) {
|
|
102
|
-
return _browserslist().default.parseConfig(browserslistConfig.contents);
|
|
103
|
-
}
|
|
104
|
-
}
|
package/lib/typescript.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = getTypescriptOptions;
|
|
7
|
-
|
|
8
|
-
function _path() {
|
|
9
|
-
const data = _interopRequireDefault(require("path"));
|
|
10
|
-
|
|
11
|
-
_path = function () {
|
|
12
|
-
return data;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
return data;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
|
-
function getTypescriptOptions(config, pragma, pragmaFrag) {
|
|
21
|
-
return {
|
|
22
|
-
plugins: [['@babel/plugin-transform-typescript', {
|
|
23
|
-
isTSX: _path().default.extname(config.searchPath) === '.tsx',
|
|
24
|
-
// Needed because of https://github.com/babel/babel/issues/12585
|
|
25
|
-
jsxPragma: pragma,
|
|
26
|
-
jsxPragmaFrag: pragmaFrag
|
|
27
|
-
}]]
|
|
28
|
-
};
|
|
29
|
-
}
|
package/src/astConverter.js
DELETED
|
@@ -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
|
-
}
|
package/src/env.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
// @flow strict-local
|
|
2
|
-
|
|
3
|
-
import type {Config} from '@parcel/types';
|
|
4
|
-
import presetEnv from '@babel/preset-env';
|
|
5
|
-
import type {BabelConfig} from './types';
|
|
6
|
-
import type {Targets as BabelTargets, PresetEnvPlugin} from '@babel/preset-env';
|
|
7
|
-
|
|
8
|
-
import getBabelTargets from './getBabelTargets';
|
|
9
|
-
import {enginesToBabelTargets} from './utils';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Generates a @babel/preset-env config for an asset.
|
|
13
|
-
* This is done by finding the source module's target engines, and the app's
|
|
14
|
-
* target engines, and doing a diff to include only the necessary plugins.
|
|
15
|
-
*/
|
|
16
|
-
export default async function getEnvOptions(
|
|
17
|
-
config: Config,
|
|
18
|
-
): Promise<?{|
|
|
19
|
-
config: BabelConfig,
|
|
20
|
-
targets: BabelTargets,
|
|
21
|
-
|}> {
|
|
22
|
-
// Only compile if there are engines defined in the environment.
|
|
23
|
-
if (Object.keys(config.env.engines).length === 0) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Load the target engines for the app and generate a @babel/preset-env config
|
|
28
|
-
let appBabelTargets = enginesToBabelTargets(config.env);
|
|
29
|
-
|
|
30
|
-
// If this is the app module, the source and target will be the same, so just compile everything.
|
|
31
|
-
// Otherwise, load the source engines and generate a babel-present-env config.
|
|
32
|
-
if (!config.isSource) {
|
|
33
|
-
let sourceBabelTargets = await getBabelTargets(config);
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
!sourceBabelTargets ||
|
|
37
|
-
!shouldCompileFurther(sourceBabelTargets, appBabelTargets)
|
|
38
|
-
) {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return {
|
|
44
|
-
targets: appBabelTargets,
|
|
45
|
-
config: {presets: ['@parcel/babel-preset-env']},
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function getNeededPlugins(targets: BabelTargets): Array<PresetEnvPlugin> {
|
|
50
|
-
return presetEnv(
|
|
51
|
-
{assertVersion: () => true},
|
|
52
|
-
{targets: targets},
|
|
53
|
-
).plugins.filter(p => p[0]);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function shouldCompileFurther(
|
|
57
|
-
sourceBabelTargets: BabelTargets,
|
|
58
|
-
appBabelTargets: BabelTargets,
|
|
59
|
-
): boolean {
|
|
60
|
-
let sourcePlugins = new Set(getNeededPlugins(sourceBabelTargets));
|
|
61
|
-
let appPlugins = getNeededPlugins(appBabelTargets);
|
|
62
|
-
|
|
63
|
-
// If there is any app plugin present that was not used to compile the source,
|
|
64
|
-
// then the asset was built to a higher target and will need to be compiled
|
|
65
|
-
// further
|
|
66
|
-
return appPlugins.some(plugin => {
|
|
67
|
-
return !sourcePlugins.has(plugin);
|
|
68
|
-
});
|
|
69
|
-
}
|