-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
babel.js
43 lines (34 loc) · 870 Bytes
/
babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const babel = require('babel-core');
const path = require('path');
const config = require('../utils/config');
module.exports = async function(asset) {
if (!await shouldTransform(asset)) {
return;
}
await asset.parseIfNeeded();
let config = {
code: false,
filename: asset.name
};
if (asset.isES6Module) {
config.plugins = [
require('babel-plugin-transform-es2015-modules-commonjs')
];
}
let res = babel.transformFromAst(asset.ast, asset.contents, config);
asset.ast = res.ast;
asset.isAstDirty = true;
};
async function shouldTransform(asset) {
if (asset.isES6Module) {
return true;
}
if (asset.ast) {
return !!asset.babelConfig;
}
if (asset.package && asset.package.babel) {
return true;
}
let babelrc = await config.resolve(asset.name, ['.babelrc', '.babelrc.js']);
return !!babelrc;
}