-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbabel.config.js
More file actions
56 lines (43 loc) · 1.49 KB
/
babel.config.js
File metadata and controls
56 lines (43 loc) · 1.49 KB
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
const { compilerOptions } = require('./tsconfig');
const pathsEntries = Object.entries(compilerOptions.paths);
/**
* @param glob path | alias name
* @return {string} path without glob pattern
*/
const removeGlobPattern = glob => glob.replace('/*', '');
/**
* @param name alias name
* @return {string} formated name (for use by babel)
*/
const prepareName = name => removeGlobPattern(name);
/**
* @param path alias path
* @return {string} formated path (for use by babel)
*/
const preparePath = ([path]) => `./${compilerOptions.baseUrl}/${removeGlobPattern(path)}`;
const alias = pathsEntries.reduce((accum, [name, path]) => ({ ...accum, [prepareName(name)]: preparePath(path) }), {});
module.exports = function(api) {
const { NODE_ENV, TARGET } = process.env;
const isProduction = NODE_ENV === 'production';
const isServer = TARGET === 'server';
api.cache(() => `${NODE_ENV}${TARGET}`);
// client settings in .browserslistrc
const targets = isServer ? { node: 'current' } : undefined;
const presets = [
['@babel/preset-env', { targets, loose: true, modules: false, useBuiltIns: 'usage', corejs: 3 }],
'@babel/preset-typescript',
'@babel/preset-react'
];
const plugins = [
['@babel/plugin-proposal-class-properties', { loose: true }],
['babel-plugin-module-resolver', { alias }],
['@babel/plugin-transform-runtime']
];
if (isProduction) {
plugins.push(['@babel/plugin-transform-react-inline-elements']);
}
return {
presets,
plugins
};
};