@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.
- package/lib/BabelTransformer.js +28 -81
- package/lib/babel7.js +56 -52
- package/lib/babelErrorUtils.js +15 -0
- package/lib/config.js +164 -124
- package/lib/constants.js +2 -3
- package/lib/flow.js +26 -7
- package/lib/jsx.js +2 -10
- package/lib/remapAstLocations.js +55 -0
- package/lib/utils.js +21 -38
- package/package.json +13 -17
- package/src/BabelTransformer.js +29 -11
- package/src/babel7.js +64 -12
- package/src/babelErrorUtils.js +30 -0
- package/src/config.js +209 -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
package/lib/utils.js
CHANGED
|
@@ -4,65 +4,61 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.enginesToBabelTargets = enginesToBabelTargets;
|
|
7
|
-
exports.getMinSemver = getMinSemver;
|
|
8
|
-
|
|
9
7
|
function _assert() {
|
|
10
8
|
const data = _interopRequireDefault(require("assert"));
|
|
11
|
-
|
|
12
9
|
_assert = function () {
|
|
13
10
|
return data;
|
|
14
11
|
};
|
|
15
|
-
|
|
16
12
|
return data;
|
|
17
13
|
}
|
|
18
|
-
|
|
19
14
|
function _semver() {
|
|
20
15
|
const data = _interopRequireDefault(require("semver"));
|
|
21
|
-
|
|
22
16
|
_semver = function () {
|
|
23
17
|
return data;
|
|
24
18
|
};
|
|
25
|
-
|
|
26
19
|
return data;
|
|
27
20
|
}
|
|
28
|
-
|
|
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
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
|
+
};
|
|
40
37
|
|
|
41
38
|
// List of browsers to exclude when the esmodule target is specified.
|
|
42
39
|
// Based on https://caniuse.com/#feat=es6-module
|
|
43
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'];
|
|
44
|
-
|
|
45
41
|
function enginesToBabelTargets(env) {
|
|
46
42
|
// "Targets" is the name @babel/preset-env uses for what Parcel calls engines.
|
|
47
43
|
// This should not be confused with Parcel's own targets.
|
|
48
44
|
// Unlike Parcel's engines, @babel/preset-env expects to work with minimum
|
|
49
45
|
// versions, not semver ranges, of its targets.
|
|
50
46
|
let targets = {};
|
|
51
|
-
|
|
52
47
|
for (let engineName of Object.keys(env.engines)) {
|
|
53
|
-
let engineValue = env.engines[engineName];
|
|
54
|
-
// possible version instead.
|
|
48
|
+
let engineValue = env.engines[engineName];
|
|
55
49
|
|
|
50
|
+
// if the engineValue is a string, it might be a semver range. Use the minimum
|
|
51
|
+
// possible version instead.
|
|
56
52
|
if (engineName === 'browsers') {
|
|
57
53
|
targets[engineName] = engineValue;
|
|
58
54
|
} else {
|
|
55
|
+
var _semver$minVersion;
|
|
59
56
|
(0, _assert().default)(typeof engineValue === 'string');
|
|
60
|
-
if (!
|
|
61
|
-
let minVersion =
|
|
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();
|
|
62
59
|
targets[engineName] = minVersion !== null && minVersion !== void 0 ? minVersion : engineValue;
|
|
63
60
|
}
|
|
64
61
|
}
|
|
65
|
-
|
|
66
62
|
if (env.outputFormat === 'esmodule' && env.isBrowser()) {
|
|
67
63
|
// If there is already a browsers target, add a blacklist to exclude
|
|
68
64
|
// instead of using babel's esmodules target. This allows specifying
|
|
@@ -75,18 +71,5 @@ function enginesToBabelTargets(env) {
|
|
|
75
71
|
targets.esmodules = true;
|
|
76
72
|
}
|
|
77
73
|
}
|
|
78
|
-
|
|
79
74
|
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
75
|
}
|
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-dev.1510+a9bb85adf",
|
|
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": "^2.0.0-
|
|
20
|
+
"parcel": "^2.0.0-dev.1508+a9bb85adf"
|
|
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",
|
|
29
|
-
"@parcel/plugin": "2.0.0-beta.3",
|
|
30
|
-
"@parcel/source-map": "2.0.0-rc.1.0",
|
|
31
|
-
"@parcel/utils": "2.0.0-beta.3",
|
|
23
|
+
"@parcel/diagnostic": "2.0.0-dev.1510+a9bb85adf",
|
|
24
|
+
"@parcel/plugin": "2.0.0-dev.1510+a9bb85adf",
|
|
25
|
+
"@parcel/source-map": "^2.1.1",
|
|
26
|
+
"@parcel/utils": "2.0.0-dev.1510+a9bb85adf",
|
|
32
27
|
"browserslist": "^4.6.6",
|
|
33
|
-
"
|
|
28
|
+
"json5": "^2.2.0",
|
|
34
29
|
"nullthrows": "^1.1.1",
|
|
35
|
-
"semver": "^5.
|
|
30
|
+
"semver": "^7.5.2"
|
|
36
31
|
},
|
|
37
32
|
"devDependencies": {
|
|
38
|
-
"@babel/core": "^7.
|
|
39
|
-
"@babel/preset-env": "^7.
|
|
40
|
-
"@
|
|
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-dev.1510+a9bb85adf"
|
|
41
37
|
},
|
|
42
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "a9bb85adf8f3b38631e178b3aacaa30c78696e36"
|
|
43
39
|
}
|
package/src/BabelTransformer.js
CHANGED
|
@@ -1,29 +1,23 @@
|
|
|
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}) {
|
|
18
17
|
return ast.type === 'babel' && semver.satisfies(ast.version, '^7.0.0');
|
|
19
18
|
},
|
|
20
19
|
|
|
21
|
-
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
|
|
20
|
+
async transform({asset, config, logger, options, tracer}) {
|
|
27
21
|
try {
|
|
28
22
|
if (config?.config) {
|
|
29
23
|
if (
|
|
@@ -33,11 +27,19 @@ export default (new Transformer({
|
|
|
33
27
|
await babel7({
|
|
34
28
|
asset,
|
|
35
29
|
options,
|
|
30
|
+
logger,
|
|
36
31
|
babelOptions: config,
|
|
37
32
|
additionalPlugins: asset.meta.babelPlugins,
|
|
33
|
+
tracer,
|
|
38
34
|
});
|
|
39
35
|
} else {
|
|
40
|
-
await babel7({
|
|
36
|
+
await babel7({
|
|
37
|
+
asset,
|
|
38
|
+
options,
|
|
39
|
+
logger,
|
|
40
|
+
babelOptions: config,
|
|
41
|
+
tracer,
|
|
42
|
+
});
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -53,6 +55,22 @@ export default (new Transformer({
|
|
|
53
55
|
options.projectRoot,
|
|
54
56
|
asset.filePath,
|
|
55
57
|
);
|
|
58
|
+
|
|
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
|
+
);
|
|
68
|
+
|
|
69
|
+
const {default: generate} = await options.packageManager.require(
|
|
70
|
+
'@babel/generator',
|
|
71
|
+
babelCorePath.resolved,
|
|
72
|
+
);
|
|
73
|
+
|
|
56
74
|
let {code, rawMappings} = generate(ast.program, {
|
|
57
75
|
sourceFileName,
|
|
58
76
|
sourceMaps: !!asset.env.sourceMap,
|
package/src/babel7.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
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';
|
|
6
|
-
import
|
|
13
|
+
import path from 'path';
|
|
14
|
+
import {md} from '@parcel/diagnostic';
|
|
7
15
|
import {relativeUrl} from '@parcel/utils';
|
|
8
|
-
import {remapAstLocations} from '
|
|
16
|
+
import {remapAstLocations} from './remapAstLocations';
|
|
9
17
|
|
|
10
18
|
import packageJson from '../package.json';
|
|
11
19
|
|
|
@@ -15,21 +23,25 @@ invariant(typeof transformerVersion === 'string');
|
|
|
15
23
|
type Babel7TransformOptions = {|
|
|
16
24
|
asset: MutableAsset,
|
|
17
25
|
options: PluginOptions,
|
|
26
|
+
logger: PluginLogger,
|
|
18
27
|
babelOptions: any,
|
|
19
28
|
additionalPlugins?: Array<any>,
|
|
29
|
+
tracer: PluginTracer,
|
|
20
30
|
|};
|
|
21
31
|
|
|
22
32
|
export default async function babel7(
|
|
23
33
|
opts: Babel7TransformOptions,
|
|
24
34
|
): Promise<?AST> {
|
|
25
|
-
let {asset, options, babelOptions, additionalPlugins = []} = opts;
|
|
26
|
-
const babelCore =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
);
|
|
33
45
|
|
|
34
46
|
let config = {
|
|
35
47
|
...babelOptions.config,
|
|
@@ -64,6 +76,28 @@ export default async function babel7(
|
|
|
64
76
|
},
|
|
65
77
|
};
|
|
66
78
|
|
|
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
|
+
}
|
|
100
|
+
|
|
67
101
|
let ast = await asset.getAST();
|
|
68
102
|
let res;
|
|
69
103
|
if (ast) {
|
|
@@ -77,7 +111,25 @@ export default async function babel7(
|
|
|
77
111
|
if (res.ast) {
|
|
78
112
|
let map = await asset.getMap();
|
|
79
113
|
if (map) {
|
|
80
|
-
remapAstLocations(res.ast, 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
|
+
}
|
|
81
133
|
}
|
|
82
134
|
}
|
|
83
135
|
}
|
|
@@ -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
|
+
}
|