@parcel/transformer-babel 2.0.0-beta.3.1 → 2.0.0-nightly.1002
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 +17 -47
- package/lib/babel7.js +5 -36
- package/lib/babelErrorUtils.js +17 -0
- package/lib/config.js +178 -77
- package/lib/constants.js +3 -3
- package/lib/flow.js +29 -2
- package/lib/jsx.js +1 -1
- package/lib/remapAstLocations.js +59 -0
- package/lib/utils.js +20 -26
- package/package.json +10 -14
- package/src/BabelTransformer.js +19 -9
- package/src/babel7.js +12 -10
- package/src/babelErrorUtils.js +30 -0
- package/src/config.js +214 -58
- package/src/constants.js +1 -1
- package/src/flow.js +36 -5
- package/src/jsx.js +2 -1
- package/src/remapAstLocations.js +70 -0
- package/src/utils.js +17 -14
- package/lib/astConverter.js +0 -114
- package/lib/getBabelTargets.js +0 -104
- package/src/astConverter.js +0 -102
- package/src/getBabelTargets.js +0 -97
|
@@ -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
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.enginesToBabelTargets = enginesToBabelTargets;
|
|
7
|
-
exports.getMinSemver = getMinSemver;
|
|
8
7
|
|
|
9
8
|
function _assert() {
|
|
10
9
|
const data = _interopRequireDefault(require("assert"));
|
|
@@ -26,20 +25,25 @@ function _semver() {
|
|
|
26
25
|
return data;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
function _options() {
|
|
30
|
-
const data = require("@babel/helper-compilation-targets/lib/options");
|
|
31
|
-
|
|
32
|
-
_options = function () {
|
|
33
|
-
return data;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
return data;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
40
29
|
|
|
41
|
-
//
|
|
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.
|
|
42
45
|
// Based on https://caniuse.com/#feat=es6-module
|
|
46
|
+
|
|
43
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'];
|
|
44
48
|
|
|
45
49
|
function enginesToBabelTargets(env) {
|
|
@@ -56,9 +60,11 @@ function enginesToBabelTargets(env) {
|
|
|
56
60
|
if (engineName === 'browsers') {
|
|
57
61
|
targets[engineName] = engineValue;
|
|
58
62
|
} else {
|
|
63
|
+
var _semver$minVersion;
|
|
64
|
+
|
|
59
65
|
(0, _assert().default)(typeof engineValue === 'string');
|
|
60
|
-
if (!
|
|
61
|
-
let minVersion =
|
|
66
|
+
if (!TargetNames.hasOwnProperty(engineName)) continue;
|
|
67
|
+
let minVersion = (_semver$minVersion = _semver().default.minVersion(engineValue)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString();
|
|
62
68
|
targets[engineName] = minVersion !== null && minVersion !== void 0 ? minVersion : engineValue;
|
|
63
69
|
}
|
|
64
70
|
}
|
|
@@ -77,16 +83,4 @@ function enginesToBabelTargets(env) {
|
|
|
77
83
|
}
|
|
78
84
|
|
|
79
85
|
return targets;
|
|
80
|
-
} // TODO: Replace with `minVersion` (https://github.com/npm/node-semver#ranges-1)
|
|
81
|
-
// once semver has been upgraded across Parcel.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
function getMinSemver(version) {
|
|
85
|
-
try {
|
|
86
|
-
let range = new (_semver().default.Range)(version);
|
|
87
|
-
let sorted = range.set.sort((a, b) => a[0].semver.compare(b[0].semver));
|
|
88
|
-
return sorted[0][0].semver.version;
|
|
89
|
-
} catch (err) {
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
86
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/transformer-babel",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-nightly.1002+5530a6ef",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,27 +17,23 @@
|
|
|
17
17
|
"source": "src/BabelTransformer.js",
|
|
18
18
|
"engines": {
|
|
19
19
|
"node": ">= 12.0.0",
|
|
20
|
-
"parcel": "
|
|
20
|
+
"parcel": "2.0.0-nightly.1000+5530a6ef"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"@babel/traverse": "^7.0.0",
|
|
28
|
-
"@parcel/babel-ast-utils": "2.0.0-beta.3.1",
|
|
29
|
-
"@parcel/plugin": "2.0.0-beta.3.1",
|
|
30
|
-
"@parcel/source-map": "2.0.0-rc.1.0",
|
|
31
|
-
"@parcel/utils": "2.0.0-beta.3.1",
|
|
23
|
+
"@parcel/diagnostic": "2.0.0-nightly.1002+5530a6ef",
|
|
24
|
+
"@parcel/plugin": "2.0.0-nightly.1002+5530a6ef",
|
|
25
|
+
"@parcel/source-map": "^2.0.0",
|
|
26
|
+
"@parcel/utils": "2.0.0-nightly.1002+5530a6ef",
|
|
32
27
|
"browserslist": "^4.6.6",
|
|
33
|
-
"
|
|
28
|
+
"json5": "^2.2.0",
|
|
34
29
|
"nullthrows": "^1.1.1",
|
|
35
30
|
"semver": "^5.7.0"
|
|
36
31
|
},
|
|
37
32
|
"devDependencies": {
|
|
38
33
|
"@babel/core": "^7.12.0",
|
|
39
34
|
"@babel/preset-env": "^7.0.0",
|
|
40
|
-
"@
|
|
35
|
+
"@babel/types": "^7.12.0",
|
|
36
|
+
"@parcel/types": "2.0.0-nightly.1002+5530a6ef"
|
|
41
37
|
},
|
|
42
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "5530a6eff8b619873353baeb0457ae4ec591e9fa"
|
|
43
39
|
}
|
package/src/BabelTransformer.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
|
|
3
|
-
import {babelErrorEnhancer} from '
|
|
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
|
|
|
12
11
|
export default (new Transformer({
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
loadConfig({config, options, logger}) {
|
|
13
|
+
return load(config, options, logger);
|
|
15
14
|
},
|
|
16
15
|
|
|
17
16
|
canReuseAST({ast}) {
|
|
@@ -19,11 +18,6 @@ export default (new Transformer({
|
|
|
19
18
|
},
|
|
20
19
|
|
|
21
20
|
async transform({asset, config, options}) {
|
|
22
|
-
// TODO: Provide invalidateOnEnvChange on config?
|
|
23
|
-
asset.invalidateOnEnvChange('BABEL_ENV');
|
|
24
|
-
asset.invalidateOnEnvChange('NODE_ENV');
|
|
25
|
-
|
|
26
|
-
// TODO: come up with a better name
|
|
27
21
|
try {
|
|
28
22
|
if (config?.config) {
|
|
29
23
|
if (
|
|
@@ -53,6 +47,22 @@ export default (new Transformer({
|
|
|
53
47
|
options.projectRoot,
|
|
54
48
|
asset.filePath,
|
|
55
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
|
+
|
|
56
66
|
let {code, rawMappings} = generate(ast.program, {
|
|
57
67
|
sourceFileName,
|
|
58
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 '
|
|
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 =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
}
|