@parcel/transformer-babel 2.0.0-nightly.151 → 2.0.0-nightly.1511
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 +91 -66
- package/lib/babel7.js +102 -38
- package/lib/babelErrorUtils.js +2 -3
- package/lib/config.js +266 -236
- package/lib/constants.js +2 -3
- package/lib/flow.js +31 -4
- package/lib/jsx.js +25 -46
- package/lib/remapAstLocations.js +55 -0
- package/lib/utils.js +36 -29
- package/package.json +18 -19
- package/src/BabelTransformer.js +68 -41
- package/src/babel7.js +97 -19
- package/src/babelErrorUtils.js +4 -3
- package/src/config.js +343 -218
- package/src/constants.js +1 -1
- package/src/flow.js +50 -3
- package/src/jsx.js +29 -44
- 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 -47
- 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 -52
- package/src/env.js +0 -70
- package/src/getBabelTargets.js +0 -97
- package/src/typescript.js +0 -16
package/lib/BabelTransformer.js
CHANGED
|
@@ -4,100 +4,125 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
var _babelErrorUtils = require("./babelErrorUtils");
|
|
8
|
+
function _plugin() {
|
|
9
|
+
const data = require("@parcel/plugin");
|
|
10
|
+
_plugin = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
function _utils() {
|
|
16
|
+
const data = require("@parcel/utils");
|
|
17
|
+
_utils = function () {
|
|
18
|
+
return data;
|
|
19
|
+
};
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
function _sourceMap() {
|
|
23
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
24
|
+
_sourceMap = function () {
|
|
25
|
+
return data;
|
|
26
|
+
};
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
function _semver() {
|
|
30
|
+
const data = _interopRequireDefault(require("semver"));
|
|
31
|
+
_semver = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
16
36
|
var _babel = _interopRequireDefault(require("./babel7"));
|
|
17
|
-
|
|
18
|
-
var _utils = require("@parcel/utils");
|
|
19
|
-
|
|
20
37
|
var _config = require("./config");
|
|
21
|
-
|
|
22
|
-
var _babelErrorUtils = require("./babelErrorUtils");
|
|
23
|
-
|
|
24
38
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
async loadConfig({
|
|
39
|
+
var _default = exports.default = new (_plugin().Transformer)({
|
|
40
|
+
loadConfig({
|
|
28
41
|
config,
|
|
29
42
|
options,
|
|
30
43
|
logger
|
|
31
44
|
}) {
|
|
32
|
-
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
preSerializeConfig({
|
|
36
|
-
config
|
|
37
|
-
}) {
|
|
38
|
-
return (0, _config.preSerialize)(config);
|
|
45
|
+
return (0, _config.load)(config, options, logger);
|
|
39
46
|
},
|
|
40
|
-
|
|
41
|
-
postDeserializeConfig({
|
|
42
|
-
config,
|
|
43
|
-
options
|
|
44
|
-
}) {
|
|
45
|
-
return (0, _config.postDeserialize)(config, options);
|
|
46
|
-
},
|
|
47
|
-
|
|
48
47
|
canReuseAST({
|
|
49
48
|
ast
|
|
50
49
|
}) {
|
|
51
|
-
return ast.type === 'babel' && _semver.default.satisfies(ast.version, '^7.0.0');
|
|
50
|
+
return ast.type === 'babel' && _semver().default.satisfies(ast.version, '^7.0.0');
|
|
52
51
|
},
|
|
53
|
-
|
|
54
52
|
async transform({
|
|
55
53
|
asset,
|
|
56
54
|
config,
|
|
57
|
-
|
|
55
|
+
logger,
|
|
56
|
+
options,
|
|
57
|
+
tracer
|
|
58
58
|
}) {
|
|
59
|
-
// TODO: come up with a better name
|
|
60
59
|
try {
|
|
61
|
-
if (config
|
|
62
|
-
if (asset.meta.babelPlugins && Array.isArray(asset.meta.babelPlugins)) {
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
if (config !== null && config !== void 0 && config.config) {
|
|
61
|
+
if (asset.meta.babelPlugins != null && Array.isArray(asset.meta.babelPlugins)) {
|
|
62
|
+
await (0, _babel.default)({
|
|
63
|
+
asset,
|
|
64
|
+
options,
|
|
65
|
+
logger,
|
|
66
|
+
babelOptions: config,
|
|
67
|
+
additionalPlugins: asset.meta.babelPlugins,
|
|
68
|
+
tracer
|
|
69
|
+
});
|
|
65
70
|
} else {
|
|
66
|
-
|
|
71
|
+
await (0, _babel.default)({
|
|
72
|
+
asset,
|
|
73
|
+
options,
|
|
74
|
+
logger,
|
|
75
|
+
babelOptions: config,
|
|
76
|
+
tracer
|
|
77
|
+
});
|
|
67
78
|
}
|
|
68
79
|
}
|
|
69
|
-
|
|
70
80
|
return [asset];
|
|
71
81
|
} catch (e) {
|
|
72
82
|
throw await (0, _babelErrorUtils.babelErrorEnhancer)(e, asset);
|
|
73
83
|
}
|
|
74
84
|
},
|
|
75
|
-
|
|
76
85
|
async generate({
|
|
77
86
|
asset,
|
|
87
|
+
ast,
|
|
78
88
|
options
|
|
79
89
|
}) {
|
|
80
|
-
let
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
90
|
+
let originalSourceMap = await asset.getMap();
|
|
91
|
+
let sourceFileName = (0, _utils().relativeUrl)(options.projectRoot, asset.filePath);
|
|
92
|
+
const babelCorePath = await options.packageManager.resolve('@babel/core', asset.filePath, {
|
|
93
|
+
range: '^7.12.0',
|
|
94
|
+
saveDev: true,
|
|
95
|
+
shouldAutoInstall: options.shouldAutoInstall
|
|
96
|
+
});
|
|
97
|
+
const {
|
|
98
|
+
default: generate
|
|
99
|
+
} = await options.packageManager.require('@babel/generator', babelCorePath.resolved);
|
|
100
|
+
let {
|
|
101
|
+
code,
|
|
102
|
+
rawMappings
|
|
103
|
+
} = generate(ast.program, {
|
|
104
|
+
sourceFileName,
|
|
105
|
+
sourceMaps: !!asset.env.sourceMap,
|
|
106
|
+
comments: true
|
|
107
|
+
});
|
|
108
|
+
let map = new (_sourceMap().default)(options.projectRoot);
|
|
109
|
+
if (rawMappings) {
|
|
110
|
+
map.addIndexedMappings(rawMappings);
|
|
98
111
|
}
|
|
112
|
+
if (originalSourceMap) {
|
|
113
|
+
// The babel AST already contains the correct mappings, but not the source contents.
|
|
114
|
+
// We need to copy over the source contents from the original map.
|
|
115
|
+
let sourcesContent = originalSourceMap.getSourcesContentMap();
|
|
116
|
+
for (let filePath in sourcesContent) {
|
|
117
|
+
let content = sourcesContent[filePath];
|
|
118
|
+
if (content != null) {
|
|
119
|
+
map.setSourceContent(filePath, content);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
content: code,
|
|
125
|
+
map
|
|
126
|
+
};
|
|
99
127
|
}
|
|
100
|
-
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
exports.default = _default;
|
|
128
|
+
});
|
package/lib/babel7.js
CHANGED
|
@@ -4,66 +4,130 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = babel7;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
function _assert() {
|
|
8
|
+
const data = _interopRequireDefault(require("assert"));
|
|
9
|
+
_assert = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _path() {
|
|
15
|
+
const data = _interopRequireDefault(require("path"));
|
|
16
|
+
_path = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _diagnostic() {
|
|
22
|
+
const data = require("@parcel/diagnostic");
|
|
23
|
+
_diagnostic = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _utils() {
|
|
29
|
+
const data = require("@parcel/utils");
|
|
30
|
+
_utils = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
var _remapAstLocations = require("./remapAstLocations");
|
|
12
36
|
var _package = _interopRequireDefault(require("../package.json"));
|
|
13
|
-
|
|
14
37
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
-
|
|
16
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
-
|
|
18
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
19
|
-
|
|
20
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
21
|
-
|
|
22
38
|
const transformerVersion = _package.default.version;
|
|
23
|
-
(0, _assert.default)(typeof transformerVersion === 'string');
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
(0, _assert().default)(typeof transformerVersion === 'string');
|
|
40
|
+
async function babel7(opts) {
|
|
41
|
+
var _babelOptions$config$, _babelOptions$config$2, _babelOptions$syntaxP;
|
|
42
|
+
let {
|
|
43
|
+
asset,
|
|
44
|
+
options,
|
|
45
|
+
babelOptions,
|
|
46
|
+
additionalPlugins = [],
|
|
47
|
+
tracer
|
|
48
|
+
} = opts;
|
|
49
|
+
const babelCore = await options.packageManager.require('@babel/core', asset.filePath, {
|
|
50
|
+
range: '^7.12.0',
|
|
51
|
+
saveDev: true,
|
|
52
|
+
shouldAutoInstall: options.shouldAutoInstall
|
|
30
53
|
});
|
|
31
|
-
|
|
32
|
-
|
|
54
|
+
let config = {
|
|
55
|
+
...babelOptions.config,
|
|
33
56
|
plugins: additionalPlugins.concat(babelOptions.config.plugins),
|
|
34
57
|
code: false,
|
|
35
58
|
ast: true,
|
|
36
59
|
filename: asset.filePath,
|
|
37
60
|
babelrc: false,
|
|
38
61
|
configFile: false,
|
|
39
|
-
parserOpts:
|
|
62
|
+
parserOpts: {
|
|
63
|
+
...babelOptions.config.parserOpts,
|
|
64
|
+
sourceFilename: (0, _utils().relativeUrl)(options.projectRoot, asset.filePath),
|
|
40
65
|
allowReturnOutsideFunction: true,
|
|
41
66
|
strictMode: false,
|
|
42
67
|
sourceType: 'module',
|
|
43
|
-
plugins: [
|
|
44
|
-
|
|
68
|
+
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 : []),
|
|
69
|
+
// Applied by preset-env
|
|
70
|
+
'classProperties', 'classPrivateProperties', 'classPrivateMethods', 'exportDefaultFrom'
|
|
71
|
+
// 'topLevelAwait'
|
|
72
|
+
]
|
|
73
|
+
},
|
|
74
|
+
|
|
45
75
|
caller: {
|
|
46
76
|
name: 'parcel',
|
|
47
77
|
version: transformerVersion,
|
|
48
|
-
targets: JSON.stringify(babelOptions.targets)
|
|
78
|
+
targets: JSON.stringify(babelOptions.targets),
|
|
79
|
+
outputFormat: asset.env.outputFormat
|
|
49
80
|
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
81
|
+
};
|
|
82
|
+
if (tracer.enabled) {
|
|
83
|
+
config.wrapPluginVisitorMethod = (key, nodeType, fn) => {
|
|
84
|
+
return function () {
|
|
85
|
+
let pluginKey = key;
|
|
86
|
+
if (pluginKey.startsWith(options.projectRoot)) {
|
|
87
|
+
pluginKey = _path().default.relative(options.projectRoot, pluginKey);
|
|
88
|
+
}
|
|
89
|
+
const measurement = tracer.createMeasurement(pluginKey, nodeType, _path().default.relative(options.projectRoot, asset.filePath));
|
|
90
|
+
fn.apply(this, arguments);
|
|
91
|
+
measurement && measurement.end();
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
let ast = await asset.getAST();
|
|
53
96
|
let res;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
res = babel.transformFromAstSync(asset.ast.program, code, config);
|
|
97
|
+
if (ast) {
|
|
98
|
+
res = await babelCore.transformFromAstAsync(ast.program, asset.isASTDirty() ? undefined : await asset.getCode(), config);
|
|
57
99
|
} else {
|
|
58
|
-
res =
|
|
100
|
+
res = await babelCore.transformAsync(await asset.getCode(), config);
|
|
101
|
+
if (res.ast) {
|
|
102
|
+
let map = await asset.getMap();
|
|
103
|
+
if (map) {
|
|
104
|
+
(0, _remapAstLocations.remapAstLocations)(babelCore.types, res.ast, map);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (res.externalDependencies) {
|
|
108
|
+
for (let f of res.externalDependencies) {
|
|
109
|
+
if (!_path().default.isAbsolute(f)) {
|
|
110
|
+
opts.logger.warn({
|
|
111
|
+
message: (0, _diagnostic().md)`Ignoring non-absolute Babel external dependency: ${f}`,
|
|
112
|
+
hints: ['Please report this to the corresponding Babel plugin and/or to Parcel.']
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
if (await options.inputFS.exists(f)) {
|
|
116
|
+
asset.invalidateOnFileChange(f);
|
|
117
|
+
} else {
|
|
118
|
+
asset.invalidateOnFileCreate({
|
|
119
|
+
filePath: f
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
59
125
|
}
|
|
60
|
-
|
|
61
126
|
if (res.ast) {
|
|
62
|
-
|
|
127
|
+
asset.setAST({
|
|
63
128
|
type: 'babel',
|
|
64
129
|
version: '7.0.0',
|
|
65
|
-
program: res.ast
|
|
66
|
-
|
|
67
|
-
};
|
|
130
|
+
program: res.ast
|
|
131
|
+
});
|
|
68
132
|
}
|
|
69
133
|
}
|
package/lib/babelErrorUtils.js
CHANGED
|
@@ -4,12 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.babelErrorEnhancer = babelErrorEnhancer;
|
|
7
|
-
|
|
8
7
|
async function babelErrorEnhancer(error, asset) {
|
|
9
8
|
if (error.loc) {
|
|
10
|
-
|
|
9
|
+
let start = error.message.startsWith(asset.filePath) ? asset.filePath.length + 1 : 0;
|
|
10
|
+
error.message = error.message.slice(start).split('\n')[0].trim();
|
|
11
11
|
}
|
|
12
|
-
|
|
13
12
|
error.source = await asset.getCode();
|
|
14
13
|
error.filePath = asset.filePath;
|
|
15
14
|
return error;
|