/
babel.js
131 lines (105 loc) · 4.25 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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 expect = require('chai').expect;
const WebpackConfig = require('../../lib/WebpackConfig');
const RuntimeConfig = require('../../lib/config/RuntimeConfig');
const babelLoader = require('../../lib/loaders/babel');
function createConfig() {
const runtimeConfig = new RuntimeConfig();
runtimeConfig.context = __dirname;
runtimeConfig.babelRcFileExists = false;
return new WebpackConfig(runtimeConfig);
}
describe('loaders/babel', () => {
it('getLoaders() basic usage', () => {
const config = createConfig();
config.runtimeConfig.babelRcFileExists = false;
config.configureBabel(function(config) {
config.foo = 'bar';
});
const actualLoaders = babelLoader.getLoaders(config);
expect(actualLoaders).to.have.lengthOf(1);
// the env preset is enabled by default
expect(actualLoaders[0].options.presets).to.have.lengthOf(1);
// callback is used
expect(actualLoaders[0].options.foo).to.equal('bar');
});
it('getLoaders() when .babelrc IS present', () => {
const config = createConfig();
config.runtimeConfig.babelRcFileExists = true;
const actualLoaders = babelLoader.getLoaders(config);
// we only add cacheDirectory
expect(actualLoaders[0].options).to.deep.equal({
cacheDirectory: true,
sourceType: 'unambiguous',
});
});
it('getLoaders() for production', () => {
const config = createConfig();
config.runtimeConfig.babelRcFileExists = true;
config.runtimeConfig.environment = 'production';
const actualLoaders = babelLoader.getLoaders(config);
// cacheDirectory is disabled in production mode
expect(actualLoaders[0].options).to.deep.equal({
cacheDirectory: false,
sourceType: 'unambiguous',
});
});
it('getLoaders() with react', () => {
const config = createConfig();
config.enableReactPreset();
config.configureBabel(function(babelConfig) {
babelConfig.presets.push('foo');
});
const actualLoaders = babelLoader.getLoaders(config);
// env, react & foo
expect(actualLoaders[0].options.presets).to.have.lengthOf(3);
expect(actualLoaders[0].options.presets).to.include('@babel/react');
// foo is also still there, not overridden
expect(actualLoaders[0].options.presets).to.include('foo');
});
it('getLoaders() with preact', () => {
const config = createConfig();
config.enablePreactPreset();
config.configureBabel(function(babelConfig) {
babelConfig.plugins.push('foo');
});
const actualLoaders = babelLoader.getLoaders(config);
expect(actualLoaders[0].options.plugins).to.deep.include.members([
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-react-jsx', { pragma: 'h' }],
'foo'
]);
});
it('getLoaders() with preact and preact-compat', () => {
const config = createConfig();
config.enablePreactPreset({ preactCompat: true });
config.configureBabel(function(babelConfig) {
babelConfig.plugins.push('foo');
});
const actualLoaders = babelLoader.getLoaders(config);
expect(actualLoaders[0].options.plugins).to.deep.include.members([
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-react-jsx'],
'foo'
]);
});
it('getLoaders() with a callback that returns an object', () => {
const config = createConfig();
config.enablePreactPreset({ preactCompat: true });
config.configureBabel(function(babelConfig) {
babelConfig.plugins.push('foo');
// This should override the original config
return { foo: true };
});
const actualLoaders = babelLoader.getLoaders(config);
expect(actualLoaders[0].options).to.deep.equal({ 'foo': true });
});
});