-
Notifications
You must be signed in to change notification settings - Fork 3
/
babel.config.js
113 lines (103 loc) · 2.69 KB
/
babel.config.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
const { join } = require('path');
module.exports = api => {
const env = api.env();
function createBabelPresets(options) {
const presets = [];
if (options.envSettings) {
presets.push(['@babel/preset-env', options.envSettings]);
}
presets.push('@babel/preset-react', '@babel/preset-typescript');
return presets;
}
function createBabelSettings(options) {
const settings = {
presets: createBabelPresets(options),
plugins: [
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
[
'@babel/plugin-transform-runtime',
{
regenerator: true,
},
],
],
};
if (env === 'test') {
settings.plugins.push('babel-plugin-istanbul');
}
return settings;
}
const nodeTargets = {
node: env === 'test' || env === 'development' ? 'current' : '6',
};
const browserTargets = {
browsers: [
'last 2 Chrome versions',
'last 2 ChromeAndroid versions',
'last 2 Firefox versions',
'Firefox ESR',
'last 2 Edge versions',
'last 2 Safari version',
'last 2 iOS version',
],
};
/**
* By default, packages need to support both node and browsers
*/
const defaultSettings = createBabelSettings({
envSettings: {
modules: env === 'es6-modules' ? false : undefined,
targets: {
...nodeTargets,
...browserTargets,
},
},
});
/**
* Note that tests run in node so the target needs be noed for test envs
*/
const frontendTargets = env === 'test' ? nodeTargets : browserTargets;
const storyshotsSettings = createBabelSettings({
envSettings: {
targets: {
...frontendTargets,
},
},
});
const frontendSettings = createBabelSettings({
envSettings: {
// Webpack handles modules for us (but not in test envs!)
modules: env !== 'test' ? false : undefined,
targets: {
...frontendTargets,
},
},
});
const overrides = [
{
test: [
join(__dirname, '.storybook'),
join(__dirname, 'packages/frontend-dist/test/snapshots/storyshots.test.tsx'),
],
...storyshotsSettings,
},
{
test: join(__dirname, 'packages/frontend-dist/src'),
...frontendSettings,
},
];
if (env === 'test') {
/*
This only exists because stories are found via require.context(...)
which is only available when code run through webpack. Snapshot tests
aren't run through Webpack.
*/
storyshotsSettings.plugins.push('require-context-hook');
}
const config = {
...defaultSettings,
overrides,
};
return config;
};