-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbabel.js
43 lines (40 loc) · 1.84 KB
/
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
Chomp.addExtension('[email protected]:npm');
const defaultConfig = {};
if (!ENV.CHOMP_EJECT)
Chomp.registerTask({
target: '.babelrc',
display: 'none',
invalidation: 'not-found',
run: `
echo '\n\x1b[93mChomp\x1b[0m: Creating \x1b[1m.babelrc\x1b[0m (\x1b[1m"babel-rc = true"\x1b[0m Babel template option in use)\n'
echo '${JSON.stringify(defaultConfig, null, 2)}' > .babelrc
`
});
Chomp.registerTemplate('babel', function ({ name, targets, deps, env, templateOptions: { presets = [], plugins = [], sourceMaps = true, babelRc = false, configFile = null, autoInstall, ...invalid } }) {
if (Object.keys(invalid).length)
throw new Error(`Invalid babel template option "${Object.keys(invalid)[0]}", expected one of "presets", "plugins", "source-maps", "babel-rc", "config-file" or "auto-install".`);
return [{
name,
targets,
deps: [...deps, ...!babelRc || ENV.CHOMP_EJECT ? [] : ['.babelrc'], ...ENV.CHOMP_EJECT ? ['npm:install'] : presets.map(p => `node_modules/${p}`), ...plugins.map(p => `node_modules/${p}`), ...ENV.CHOMP_EJECT ? [] : ['node_modules/@babel/core', 'node_modules/@babel/cli']],
env,
run: `babel $DEP -o $TARGET${
sourceMaps ? ' --source-maps' : ''
}${
plugins.length ? ` --plugins=${plugins.join(',')}` : ''
}${
presets.length ? ` --presets=${presets.join(',')}` : ''
}${
!babelRc ? ' --no-babelrc' : ''
}${
configFile ? ` --config-file=${configFile.startsWith('./') ? configFile : './' + configFile}` : ''
}`
}, ...!babelRc || ENV.CHOMP_EJECT ? [] : ['.babelrc'], ...ENV.CHOMP_EJECT ? [] : [{
template: 'npm',
templateOptions: {
packages: [...presets.map(p => p.startsWith('@babel/') ? p + '@7' : p), ...plugins.map(p => p.startsWith('@babel/') ? p + '@7' : p), '@babel/core@7', '@babel/cli@7'],
dev: true,
autoInstall
}
}]];
});