@parcel/transformer-babel 2.0.0-beta.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 +98 -30
- package/lib/babel7.js +46 -19
- package/lib/babelErrorUtils.js +17 -0
- package/lib/config.js +294 -213
- package/lib/constants.js +3 -3
- package/lib/flow.js +38 -3
- package/lib/jsx.js +24 -46
- package/lib/remapAstLocations.js +59 -0
- package/lib/utils.js +38 -20
- package/package.json +17 -21
- package/src/BabelTransformer.js +64 -19
- package/src/babel7.js +36 -15
- package/src/babelErrorUtils.js +30 -0
- package/src/config.js +339 -225
- package/src/constants.js +1 -1
- package/src/flow.js +50 -3
- package/src/jsx.js +18 -47
- 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 -49
- 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 -53
- package/src/env.js +0 -70
- package/src/getBabelTargets.js +0 -97
- package/src/typescript.js +0 -16
package/lib/BabelTransformer.js
CHANGED
|
@@ -5,11 +5,47 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _babelErrorUtils = require("./babelErrorUtils");
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
function _plugin() {
|
|
11
|
+
const data = require("@parcel/plugin");
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
_plugin = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _utils() {
|
|
21
|
+
const data = require("@parcel/utils");
|
|
22
|
+
|
|
23
|
+
_utils = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function _sourceMap() {
|
|
31
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
32
|
+
|
|
33
|
+
_sourceMap = function () {
|
|
34
|
+
return data;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return data;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function _semver() {
|
|
41
|
+
const data = _interopRequireDefault(require("semver"));
|
|
42
|
+
|
|
43
|
+
_semver = function () {
|
|
44
|
+
return data;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
13
49
|
|
|
14
50
|
var _babel = _interopRequireDefault(require("./babel7"));
|
|
15
51
|
|
|
@@ -17,32 +53,19 @@ var _config = require("./config");
|
|
|
17
53
|
|
|
18
54
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
55
|
|
|
20
|
-
var _default = new _plugin.Transformer({
|
|
21
|
-
|
|
56
|
+
var _default = new (_plugin().Transformer)({
|
|
57
|
+
loadConfig({
|
|
22
58
|
config,
|
|
23
59
|
options,
|
|
24
60
|
logger
|
|
25
61
|
}) {
|
|
26
|
-
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
preSerializeConfig({
|
|
30
|
-
config
|
|
31
|
-
}) {
|
|
32
|
-
return (0, _config.preSerialize)(config);
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
postDeserializeConfig({
|
|
36
|
-
config,
|
|
37
|
-
options
|
|
38
|
-
}) {
|
|
39
|
-
return (0, _config.postDeserialize)(config, options);
|
|
62
|
+
return (0, _config.load)(config, options, logger);
|
|
40
63
|
},
|
|
41
64
|
|
|
42
65
|
canReuseAST({
|
|
43
66
|
ast
|
|
44
67
|
}) {
|
|
45
|
-
return ast.type === 'babel' && _semver.default.satisfies(ast.version, '^7.0.0');
|
|
68
|
+
return ast.type === 'babel' && _semver().default.satisfies(ast.version, '^7.0.0');
|
|
46
69
|
},
|
|
47
70
|
|
|
48
71
|
async transform({
|
|
@@ -50,32 +73,77 @@ var _default = new _plugin.Transformer({
|
|
|
50
73
|
config,
|
|
51
74
|
options
|
|
52
75
|
}) {
|
|
53
|
-
// TODO: come up with a better name
|
|
54
76
|
try {
|
|
55
|
-
if (config
|
|
77
|
+
if (config !== null && config !== void 0 && config.config) {
|
|
56
78
|
if (asset.meta.babelPlugins != null && Array.isArray(asset.meta.babelPlugins)) {
|
|
57
|
-
await (0, _babel.default)(
|
|
79
|
+
await (0, _babel.default)({
|
|
80
|
+
asset,
|
|
81
|
+
options,
|
|
82
|
+
babelOptions: config,
|
|
83
|
+
additionalPlugins: asset.meta.babelPlugins
|
|
84
|
+
});
|
|
58
85
|
} else {
|
|
59
|
-
await (0, _babel.default)(
|
|
86
|
+
await (0, _babel.default)({
|
|
87
|
+
asset,
|
|
88
|
+
options,
|
|
89
|
+
babelOptions: config
|
|
90
|
+
});
|
|
60
91
|
}
|
|
61
92
|
}
|
|
62
93
|
|
|
63
94
|
return [asset];
|
|
64
95
|
} catch (e) {
|
|
65
|
-
throw await (0,
|
|
96
|
+
throw await (0, _babelErrorUtils.babelErrorEnhancer)(e, asset);
|
|
66
97
|
}
|
|
67
98
|
},
|
|
68
99
|
|
|
69
|
-
generate({
|
|
100
|
+
async generate({
|
|
70
101
|
asset,
|
|
71
102
|
ast,
|
|
72
103
|
options
|
|
73
104
|
}) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
105
|
+
let originalSourceMap = await asset.getMap();
|
|
106
|
+
let sourceFileName = (0, _utils().relativeUrl)(options.projectRoot, asset.filePath);
|
|
107
|
+
const babelCorePath = await options.packageManager.resolve('@babel/core', asset.filePath, {
|
|
108
|
+
range: '^7.12.0',
|
|
109
|
+
saveDev: true,
|
|
110
|
+
shouldAutoInstall: options.shouldAutoInstall
|
|
78
111
|
});
|
|
112
|
+
const {
|
|
113
|
+
default: generate
|
|
114
|
+
} = await options.packageManager.require('@babel/generator', babelCorePath.resolved);
|
|
115
|
+
let {
|
|
116
|
+
code,
|
|
117
|
+
rawMappings
|
|
118
|
+
} = generate(ast.program, {
|
|
119
|
+
sourceFileName,
|
|
120
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
121
|
+
comments: true
|
|
122
|
+
});
|
|
123
|
+
let map = new (_sourceMap().default)(options.projectRoot);
|
|
124
|
+
|
|
125
|
+
if (rawMappings) {
|
|
126
|
+
map.addIndexedMappings(rawMappings);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (originalSourceMap) {
|
|
130
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
131
|
+
// We need to copy over the source contents from the original map.
|
|
132
|
+
let sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
133
|
+
|
|
134
|
+
for (let filePath in sourcesContent) {
|
|
135
|
+
let content = sourcesContent[filePath];
|
|
136
|
+
|
|
137
|
+
if (content != null) {
|
|
138
|
+
map.setSourceContent(filePath, content);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
content: code,
|
|
145
|
+
map
|
|
146
|
+
};
|
|
79
147
|
}
|
|
80
148
|
|
|
81
149
|
});
|
package/lib/babel7.js
CHANGED
|
@@ -5,31 +5,48 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = babel7;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
function _assert() {
|
|
9
|
+
const data = _interopRequireDefault(require("assert"));
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
_assert = function () {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
13
17
|
|
|
14
|
-
|
|
18
|
+
function _utils() {
|
|
19
|
+
const data = require("@parcel/utils");
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
_utils = function () {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
var _remapAstLocations = require("./remapAstLocations");
|
|
29
|
+
|
|
30
|
+
var _package = _interopRequireDefault(require("../package.json"));
|
|
21
31
|
|
|
22
32
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
33
|
|
|
24
34
|
const transformerVersion = _package.default.version;
|
|
25
|
-
(0, _assert.default)(typeof transformerVersion === 'string');
|
|
26
|
-
|
|
27
|
-
async function babel7(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
(0, _assert().default)(typeof transformerVersion === 'string');
|
|
36
|
+
|
|
37
|
+
async function babel7(opts) {
|
|
38
|
+
var _babelOptions$config$, _babelOptions$config$2, _babelOptions$syntaxP;
|
|
39
|
+
|
|
40
|
+
let {
|
|
41
|
+
asset,
|
|
42
|
+
options,
|
|
43
|
+
babelOptions,
|
|
44
|
+
additionalPlugins = []
|
|
45
|
+
} = opts;
|
|
46
|
+
const babelCore = await options.packageManager.require('@babel/core', asset.filePath, {
|
|
47
|
+
range: '^7.12.0',
|
|
48
|
+
saveDev: true,
|
|
49
|
+
shouldAutoInstall: options.shouldAutoInstall
|
|
33
50
|
});
|
|
34
51
|
let config = { ...babelOptions.config,
|
|
35
52
|
plugins: additionalPlugins.concat(babelOptions.config.plugins),
|
|
@@ -39,11 +56,13 @@ async function babel7(asset, options, babelOptions, additionalPlugins = []) {
|
|
|
39
56
|
babelrc: false,
|
|
40
57
|
configFile: false,
|
|
41
58
|
parserOpts: { ...babelOptions.config.parserOpts,
|
|
42
|
-
sourceFilename: (0, _utils.relativeUrl)(options.projectRoot, asset.filePath),
|
|
59
|
+
sourceFilename: (0, _utils().relativeUrl)(options.projectRoot, asset.filePath),
|
|
43
60
|
allowReturnOutsideFunction: true,
|
|
44
61
|
strictMode: false,
|
|
45
62
|
sourceType: 'module',
|
|
46
|
-
plugins: [
|
|
63
|
+
plugins: [...((_babelOptions$config$ = (_babelOptions$config$2 = babelOptions.config.parserOpts) === null || _babelOptions$config$2 === void 0 ? void 0 : _babelOptions$config$2.plugins) !== null && _babelOptions$config$ !== void 0 ? _babelOptions$config$ : []), ...((_babelOptions$syntaxP = babelOptions.syntaxPlugins) !== null && _babelOptions$syntaxP !== void 0 ? _babelOptions$syntaxP : []), // Applied by preset-env
|
|
64
|
+
'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom' // 'topLevelAwait'
|
|
65
|
+
]
|
|
47
66
|
},
|
|
48
67
|
caller: {
|
|
49
68
|
name: 'parcel',
|
|
@@ -56,9 +75,17 @@ async function babel7(asset, options, babelOptions, additionalPlugins = []) {
|
|
|
56
75
|
let res;
|
|
57
76
|
|
|
58
77
|
if (ast) {
|
|
59
|
-
res = await
|
|
78
|
+
res = await babelCore.transformFromAstAsync(ast.program, asset.isASTDirty() ? undefined : await asset.getCode(), config);
|
|
60
79
|
} else {
|
|
61
|
-
res = await
|
|
80
|
+
res = await babelCore.transformAsync(await asset.getCode(), config);
|
|
81
|
+
|
|
82
|
+
if (res.ast) {
|
|
83
|
+
let map = await asset.getMap();
|
|
84
|
+
|
|
85
|
+
if (map) {
|
|
86
|
+
(0, _remapAstLocations.remapAstLocations)(babelCore.types, res.ast, map);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
62
89
|
}
|
|
63
90
|
|
|
64
91
|
if (res.ast) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.babelErrorEnhancer = babelErrorEnhancer;
|
|
7
|
+
|
|
8
|
+
async function babelErrorEnhancer(error, asset) {
|
|
9
|
+
if (error.loc) {
|
|
10
|
+
let start = error.message.startsWith(asset.filePath) ? asset.filePath.length + 1 : 0;
|
|
11
|
+
error.message = error.message.slice(start).split('\n')[0].trim();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
error.source = await asset.getCode();
|
|
15
|
+
error.filePath = asset.filePath;
|
|
16
|
+
return error;
|
|
17
|
+
}
|