@parcel/transformer-babel 2.0.0-nightly.97 → 2.1.0
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 +107 -47
- package/lib/astConverter.js +11 -3
- package/lib/babel7.js +81 -26
- package/lib/config.js +319 -213
- package/lib/constants.js +8 -0
- package/lib/flow.js +9 -1
- package/lib/jsx.js +30 -43
- package/lib/utils.js +33 -17
- package/package.json +20 -14
- package/src/BabelTransformer.js +44 -40
- package/src/babel7.js +47 -17
- package/src/config.js +364 -224
- package/src/constants.js +3 -0
- package/src/flow.js +19 -2
- package/src/jsx.js +29 -44
- package/src/types.js +0 -7
- package/src/utils.js +4 -14
- package/lib/babel6.js +0 -47
- package/lib/babelErrorUtils.js +0 -16
- package/lib/env.js +0 -63
- package/lib/getBabelTargets.js +0 -86
- package/lib/typescript.js +0 -18
- package/src/babel6.js +0 -52
- package/src/babelErrorUtils.js +0 -29
- 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,43 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default =
|
|
6
|
+
exports.default = isJSX;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
function _path() {
|
|
9
|
+
const data = _interopRequireDefault(require("path"));
|
|
10
|
+
|
|
11
|
+
_path = function () {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
9
17
|
|
|
10
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
19
|
|
|
12
|
-
const JSX_EXTENSIONS =
|
|
13
|
-
|
|
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
|
-
};
|
|
20
|
+
const JSX_EXTENSIONS = new Set(['.jsx', '.tsx']);
|
|
21
|
+
const JSX_LIBRARIES = ['react', 'preact', 'nervejs', 'hyperapp'];
|
|
34
22
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
23
|
+
* Returns whether an asset is likely JSX. Attempts to detect react or react-like libraries
|
|
24
|
+
* along with
|
|
37
25
|
*/
|
|
38
26
|
|
|
39
|
-
async function
|
|
27
|
+
async function isJSX(options, config) {
|
|
40
28
|
if (!config.isSource) {
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
};
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (JSX_EXTENSIONS.has(_path().default.extname(config.searchPath))) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let pkg = await config.getPackage();
|
|
37
|
+
|
|
38
|
+
if (pkg !== null && pkg !== void 0 && pkg.alias && pkg.alias['react']) {
|
|
39
|
+
// e.g.: `{ alias: { "react": "preact/compat" } }`
|
|
40
|
+
return true;
|
|
41
|
+
} else {
|
|
42
|
+
// Find a dependency that implies JSX syntax.
|
|
43
|
+
return JSX_LIBRARIES.some(libName => pkg && (pkg.dependencies && pkg.dependencies[libName] || pkg.devDependencies && pkg.devDependencies[libName] || pkg.peerDependencies && pkg.peerDependencies[libName]));
|
|
57
44
|
}
|
|
58
45
|
}
|
package/lib/utils.js
CHANGED
|
@@ -4,11 +4,36 @@ 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() {
|
|
9
|
+
const data = _interopRequireDefault(require("assert"));
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
_assert = 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 _options() {
|
|
29
|
+
const data = require("@babel/helper-compilation-targets/lib/options");
|
|
30
|
+
|
|
31
|
+
_options = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
12
37
|
|
|
13
38
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
39
|
|
|
@@ -30,8 +55,11 @@ function enginesToBabelTargets(env) {
|
|
|
30
55
|
if (engineName === 'browsers') {
|
|
31
56
|
targets[engineName] = engineValue;
|
|
32
57
|
} else {
|
|
33
|
-
|
|
34
|
-
|
|
58
|
+
var _semver$minVersion;
|
|
59
|
+
|
|
60
|
+
(0, _assert().default)(typeof engineValue === 'string');
|
|
61
|
+
if (!_options().TargetNames.hasOwnProperty(engineName)) continue;
|
|
62
|
+
let minVersion = (_semver$minVersion = _semver().default.minVersion(engineValue)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.toString();
|
|
35
63
|
targets[engineName] = minVersion !== null && minVersion !== void 0 ? minVersion : engineValue;
|
|
36
64
|
}
|
|
37
65
|
}
|
|
@@ -50,16 +78,4 @@ function enginesToBabelTargets(env) {
|
|
|
50
78
|
}
|
|
51
79
|
|
|
52
80
|
return targets;
|
|
53
|
-
} // TODO: Replace with `minVersion` (https://github.com/npm/node-semver#ranges-1)
|
|
54
|
-
// once semver has been upgraded across Parcel.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
function getMinSemver(version) {
|
|
58
|
-
try {
|
|
59
|
-
let range = new _semver.default.Range(version);
|
|
60
|
-
let sorted = range.set.sort((a, b) => a[0].semver.compare(b[0].semver));
|
|
61
|
-
return sorted[0][0].semver.version;
|
|
62
|
-
} catch (err) {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
81
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/transformer-babel",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.0",
|
|
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,28 +16,30 @@
|
|
|
12
16
|
"main": "lib/BabelTransformer.js",
|
|
13
17
|
"source": "src/BabelTransformer.js",
|
|
14
18
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
16
|
-
"parcel": "^2.
|
|
19
|
+
"node": ">= 12.0.0",
|
|
20
|
+
"parcel": "^2.1.0"
|
|
17
21
|
},
|
|
18
22
|
"dependencies": {
|
|
19
|
-
"@babel/core": "^7.
|
|
20
|
-
"@babel/generator": "^7.
|
|
23
|
+
"@babel/core": "^7.12.0",
|
|
24
|
+
"@babel/generator": "^7.9.0",
|
|
25
|
+
"@babel/helper-compilation-targets": "^7.8.4",
|
|
21
26
|
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
|
|
22
|
-
"@babel/plugin-transform-react-jsx": "^7.0.0",
|
|
23
|
-
"@babel/plugin-transform-typescript": "^7.4.5",
|
|
24
|
-
"@babel/preset-env": "^7.0.0",
|
|
25
27
|
"@babel/traverse": "^7.0.0",
|
|
26
|
-
"@parcel/babel-
|
|
27
|
-
"@parcel/
|
|
28
|
-
"@parcel/
|
|
29
|
-
"@parcel/
|
|
28
|
+
"@parcel/babel-ast-utils": "^2.1.0",
|
|
29
|
+
"@parcel/diagnostic": "^2.1.0",
|
|
30
|
+
"@parcel/plugin": "^2.1.0",
|
|
31
|
+
"@parcel/source-map": "^2.0.0",
|
|
32
|
+
"@parcel/utils": "^2.1.0",
|
|
30
33
|
"browserslist": "^4.6.6",
|
|
31
34
|
"core-js": "^3.2.1",
|
|
35
|
+
"json5": "^2.1.0",
|
|
32
36
|
"nullthrows": "^1.1.1",
|
|
33
37
|
"semver": "^5.7.0"
|
|
34
38
|
},
|
|
35
39
|
"devDependencies": {
|
|
36
|
-
"@
|
|
40
|
+
"@babel/core": "^7.12.0",
|
|
41
|
+
"@babel/preset-env": "^7.0.0",
|
|
42
|
+
"@parcel/types": "^2.1.0"
|
|
37
43
|
},
|
|
38
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "5a80fbe4af8797d0aab248a66f5254f42f00e83e"
|
|
39
45
|
}
|
package/src/BabelTransformer.js
CHANGED
|
@@ -1,25 +1,17 @@
|
|
|
1
|
-
// @flow
|
|
1
|
+
// @flow strict-local
|
|
2
2
|
|
|
3
|
+
import {babelErrorEnhancer} from '@parcel/babel-ast-utils';
|
|
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';
|
|
8
|
+
import generate from '@babel/generator';
|
|
7
9
|
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
|
-
},
|
|
16
|
-
|
|
17
|
-
preSerializeConfig({config}) {
|
|
18
|
-
return preSerialize(config);
|
|
19
|
-
},
|
|
10
|
+
import {load} from './config';
|
|
20
11
|
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
export default (new Transformer({
|
|
13
|
+
loadConfig({config, options, logger}) {
|
|
14
|
+
return load(config, options, logger);
|
|
23
15
|
},
|
|
24
16
|
|
|
25
17
|
canReuseAST({ast}) {
|
|
@@ -27,19 +19,20 @@ export default new Transformer({
|
|
|
27
19
|
},
|
|
28
20
|
|
|
29
21
|
async transform({asset, config, options}) {
|
|
30
|
-
// TODO: come up with a better name
|
|
31
22
|
try {
|
|
32
23
|
if (config?.config) {
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
asset.
|
|
24
|
+
if (
|
|
25
|
+
asset.meta.babelPlugins != null &&
|
|
26
|
+
Array.isArray(asset.meta.babelPlugins)
|
|
27
|
+
) {
|
|
28
|
+
await babel7({
|
|
36
29
|
asset,
|
|
37
30
|
options,
|
|
38
|
-
config,
|
|
39
|
-
asset.meta.babelPlugins,
|
|
40
|
-
);
|
|
31
|
+
babelOptions: config,
|
|
32
|
+
additionalPlugins: asset.meta.babelPlugins,
|
|
33
|
+
});
|
|
41
34
|
} else {
|
|
42
|
-
|
|
35
|
+
await babel7({asset, options, babelOptions: config});
|
|
43
36
|
}
|
|
44
37
|
}
|
|
45
38
|
|
|
@@ -49,27 +42,38 @@ export default new Transformer({
|
|
|
49
42
|
}
|
|
50
43
|
},
|
|
51
44
|
|
|
52
|
-
async generate({asset, options}) {
|
|
45
|
+
async generate({asset, ast, options}) {
|
|
46
|
+
let originalSourceMap = await asset.getMap();
|
|
53
47
|
let sourceFileName: string = relativeUrl(
|
|
54
48
|
options.projectRoot,
|
|
55
49
|
asset.filePath,
|
|
56
50
|
);
|
|
51
|
+
let {code, rawMappings} = generate(ast.program, {
|
|
52
|
+
sourceFileName,
|
|
53
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
54
|
+
comments: true,
|
|
55
|
+
});
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
sourceFileName: sourceFileName,
|
|
63
|
-
});
|
|
57
|
+
let map = new SourceMap(options.projectRoot);
|
|
58
|
+
if (rawMappings) {
|
|
59
|
+
map.addIndexedMappings(rawMappings);
|
|
60
|
+
}
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
62
|
+
if (originalSourceMap) {
|
|
63
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
64
|
+
// We need to copy over the source contents from the original map.
|
|
65
|
+
let sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
66
|
+
for (let filePath in sourcesContent) {
|
|
67
|
+
let content = sourcesContent[filePath];
|
|
68
|
+
if (content != null) {
|
|
69
|
+
map.setSourceContent(filePath, content);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
73
72
|
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
content: code,
|
|
76
|
+
map,
|
|
77
|
+
};
|
|
74
78
|
},
|
|
75
|
-
});
|
|
79
|
+
}): Transformer);
|
package/src/babel7.js
CHANGED
|
@@ -2,23 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
import type {MutableAsset, AST, PluginOptions} from '@parcel/types';
|
|
4
4
|
|
|
5
|
-
import packageJson from '../package.json';
|
|
6
5
|
import invariant from 'assert';
|
|
6
|
+
import * as internalBabelCore from '@babel/core';
|
|
7
|
+
import {relativeUrl} from '@parcel/utils';
|
|
8
|
+
import {remapAstLocations} from '@parcel/babel-ast-utils';
|
|
9
|
+
|
|
10
|
+
import packageJson from '../package.json';
|
|
7
11
|
|
|
8
12
|
const transformerVersion: mixed = packageJson.version;
|
|
9
13
|
invariant(typeof transformerVersion === 'string');
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
type Babel7TransformOptions = {|
|
|
12
16
|
asset: MutableAsset,
|
|
13
17
|
options: PluginOptions,
|
|
14
18
|
babelOptions: any,
|
|
15
|
-
additionalPlugins
|
|
19
|
+
additionalPlugins?: Array<any>,
|
|
20
|
+
|};
|
|
21
|
+
|
|
22
|
+
export default async function babel7(
|
|
23
|
+
opts: Babel7TransformOptions,
|
|
16
24
|
): Promise<?AST> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
25
|
+
let {asset, options, babelOptions, additionalPlugins = []} = opts;
|
|
26
|
+
const babelCore = babelOptions.internal
|
|
27
|
+
? internalBabelCore
|
|
28
|
+
: await options.packageManager.require('@babel/core', asset.filePath, {
|
|
29
|
+
range: '^7.12.0',
|
|
30
|
+
saveDev: true,
|
|
31
|
+
shouldAutoInstall: options.shouldAutoInstall,
|
|
32
|
+
});
|
|
22
33
|
|
|
23
34
|
let config = {
|
|
24
35
|
...babelOptions.config,
|
|
@@ -30,33 +41,52 @@ export default async function babel7(
|
|
|
30
41
|
configFile: false,
|
|
31
42
|
parserOpts: {
|
|
32
43
|
...babelOptions.config.parserOpts,
|
|
44
|
+
sourceFilename: relativeUrl(options.projectRoot, asset.filePath),
|
|
33
45
|
allowReturnOutsideFunction: true,
|
|
34
46
|
strictMode: false,
|
|
35
47
|
sourceType: 'module',
|
|
36
|
-
plugins: [
|
|
48
|
+
plugins: [
|
|
49
|
+
...(babelOptions.config.parserOpts?.plugins ?? []),
|
|
50
|
+
...(babelOptions.syntaxPlugins ?? []),
|
|
51
|
+
// Applied by preset-env
|
|
52
|
+
'classProperties',
|
|
53
|
+
'classPrivateProperties',
|
|
54
|
+
'classPrivateMethods',
|
|
55
|
+
'exportDefaultFrom',
|
|
56
|
+
// 'topLevelAwait'
|
|
57
|
+
],
|
|
37
58
|
},
|
|
38
59
|
caller: {
|
|
39
60
|
name: 'parcel',
|
|
40
61
|
version: transformerVersion,
|
|
41
62
|
targets: JSON.stringify(babelOptions.targets),
|
|
63
|
+
outputFormat: asset.env.outputFormat,
|
|
42
64
|
},
|
|
43
65
|
};
|
|
44
66
|
|
|
45
|
-
let
|
|
46
|
-
|
|
67
|
+
let ast = await asset.getAST();
|
|
47
68
|
let res;
|
|
48
|
-
if (
|
|
49
|
-
res =
|
|
69
|
+
if (ast) {
|
|
70
|
+
res = await babelCore.transformFromAstAsync(
|
|
71
|
+
ast.program,
|
|
72
|
+
asset.isASTDirty() ? undefined : await asset.getCode(),
|
|
73
|
+
config,
|
|
74
|
+
);
|
|
50
75
|
} else {
|
|
51
|
-
res =
|
|
76
|
+
res = await babelCore.transformAsync(await asset.getCode(), config);
|
|
77
|
+
if (res.ast) {
|
|
78
|
+
let map = await asset.getMap();
|
|
79
|
+
if (map) {
|
|
80
|
+
remapAstLocations(res.ast, map);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
52
83
|
}
|
|
53
84
|
|
|
54
85
|
if (res.ast) {
|
|
55
|
-
|
|
86
|
+
asset.setAST({
|
|
56
87
|
type: 'babel',
|
|
57
88
|
version: '7.0.0',
|
|
58
89
|
program: res.ast,
|
|
59
|
-
|
|
60
|
-
};
|
|
90
|
+
});
|
|
61
91
|
}
|
|
62
92
|
}
|