-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.babelrc.js
113 lines (103 loc) · 2.64 KB
/
.babelrc.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 genericNames = require('generic-names')
const {LOCAL_IDENT_NAME} = require('./buildOptions')
const ASYNC = process.env.ASYNC
const DIRECTORY_ALIASES = {
components: './components',
helpers: './helpers',
clientHelpers: './clientHelpers',
model: './model',
main: './main',
styles: './styles',
appConstants: './appConstants'
}
const clientPresets = [
['module:metro-react-native-babel-preset', {
disableImportExportTransform: !!ASYNC
}]
]
const serverPresets = ['module:metro-react-native-babel-preset']
const basePlugins = [
['@babel/plugin-proposal-decorators', {legacy: true}],
['module-resolver', { alias: DIRECTORY_ALIASES }]
]
const dotenvPlugin = ({production} = {}) =>
['dotenv-import', {
moduleName: '@env',
path: ['.env', production ? '.env.production' : '.env.local'],
safe: true,
allowUndefined: true
}]
const webReactCssModulesPlugin = ({production} = {}) =>
['react-css-modules', {
handleMissingStyleName: 'ignore',
filetypes: {
'.styl': {}
},
generateScopedName,
webpackHotModuleReloading: !production
}]
const nativeBasePlugins = () => [
['react-native-stylename-to-style', {
extensions: ['styl', 'css']
}],
[
'react-native-platform-specific-extensions',
{
extensions: ['styl', 'css']
}
]
]
const webBasePlugins = () => [
'react-native-web-pass-classname'
]
module.exports = {
plugins: basePlugins,
env: {
development: {
presets: clientPresets,
plugins: [].concat([
dotenvPlugin()
], nativeBasePlugins())
},
production: {
presets: clientPresets,
plugins: [].concat([
dotenvPlugin({production: true})
], nativeBasePlugins())
},
web_development: {
presets: clientPresets,
plugins: [].concat([
'react-hot-loader/babel',
dotenvPlugin(),
webReactCssModulesPlugin()
], webBasePlugins())
},
web_production: {
presets: clientPresets,
plugins: [].concat([
dotenvPlugin({production: true}),
webReactCssModulesPlugin({production: true})
], webBasePlugins())
},
server: {
presets: serverPresets,
plugins: [
['@babel/plugin-transform-runtime', {
regenerator: true
}]
]
}
}
}
function generateScopedName (name, filename/* , css */) {
let hashSize = LOCAL_IDENT_NAME.match(/base64:(\d+)]/)
if (!hashSize) {
throw new Error(
'wrong LOCAL_IDENT_NAME. Change generateScopeName() accordingly'
)
}
hashSize = Number(hashSize[1])
if (new RegExp(`_.{${hashSize}}_$`).test(name)) return name
return genericNames(LOCAL_IDENT_NAME)(name, filename)
}