/
babel.js
89 lines (77 loc) · 3.24 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
'use strict';
const loaderFeatures = require('../features');
const applyOptionsCallback = require('../utils/apply-options-callback');
/**
* @param {WebpackConfig} webpackConfig
* @return {Array} of loaders to use for Babel
*/
module.exports = {
getLoaders(webpackConfig) {
let babelConfig = {
// improves performance by caching babel compiles
// this option is always added but is set to FALSE in
// production to avoid cache invalidation issues caused
// by some Babel presets/plugins (for instance the ones
// that use browserslist)
// https://github.com/babel/babel-loader#options
cacheDirectory: !webpackConfig.isProduction(),
// let Babel guess which kind of import/export syntax
// it should use based on the content of files
sourceType: 'unambiguous',
};
// configure babel (unless the user is specifying .babelrc)
// todo - add a sanity check for their babelrc contents
if (!webpackConfig.doesBabelRcFileExist()) {
const presetEnvOptions = {
// modules don't need to be transformed - webpack will parse
// the modules for us. This is a performance improvement
// https://babeljs.io/docs/en/babel-preset-env#modules
modules: false,
targets: {},
forceAllTransforms: webpackConfig.isProduction(),
useBuiltIns: webpackConfig.babelOptions.useBuiltIns,
corejs: webpackConfig.babelOptions.corejs,
};
Object.assign(babelConfig, {
presets: [
['@babel/preset-env', presetEnvOptions]
],
plugins: ['@babel/plugin-syntax-dynamic-import']
});
if (webpackConfig.useReact) {
loaderFeatures.ensurePackagesExistAndAreCorrectVersion('react');
babelConfig.presets.push('@babel/react');
}
if (webpackConfig.usePreact) {
loaderFeatures.ensurePackagesExistAndAreCorrectVersion('preact');
if (webpackConfig.preactOptions.preactCompat) {
// If preact-compat is enabled tell babel to
// transform JSX into React.createElement calls.
babelConfig.plugins.push(['@babel/plugin-transform-react-jsx']);
} else {
// If preact-compat is disabled tell babel to
// transform JSX into Preact h() calls.
babelConfig.plugins.push([
'@babel/plugin-transform-react-jsx',
{ 'pragma': 'h' }
]);
}
}
babelConfig = applyOptionsCallback(webpackConfig.babelConfigurationCallback, babelConfig);
}
return [
{
loader: 'babel-loader',
options: babelConfig
}
];
}
};