-
Notifications
You must be signed in to change notification settings - Fork 27
/
webpack.config.ts
104 lines (97 loc) · 2.7 KB
/
webpack.config.ts
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
/* eslint-disable no-console */
import Path from 'path'
import TerserPlugin from 'terser-webpack-plugin'
import { Configuration, DefinePlugin, WebpackPluginInstance } from 'webpack'
interface WebpackEnvParams {
debug: boolean
fileName: string
}
const base = async (env?: Partial<WebpackEnvParams>): Promise<Configuration> => {
const isProduction = process.env['NODE_ENV'] === 'production'
const filename = env?.fileName || ['index.browser', isProduction ? '.min' : null, '.js'].filter(Boolean).join('')
const entry = Path.resolve(__dirname, 'src')
const path = Path.resolve(__dirname, 'dist')
const plugins: WebpackPluginInstance[] = [
new DefinePlugin({
'process.env.ENV': process.env['NODE_ENV'] || 'development',
'process.env.IS_WEBPACK_BUILD': 'true',
}),
]
return {
bail: Boolean(isProduction),
mode: (process.env['NODE_ENV'] as 'production') || 'development',
devtool: isProduction ? 'source-map' : 'cheap-module-source-map',
entry,
output: {
path,
filename,
sourceMapFilename: '[file].map[query]',
library: 'BeeJs',
libraryTarget: 'umd',
globalObject: 'this',
},
module: {
rules: [
{
test: /\.(ts|js)$/,
// include: entry,
use: {
loader: 'babel-loader',
},
},
],
},
resolve: {
extensions: ['.ts', '.js'],
fallback: {
path: false,
fs: false,
stream: false,
},
},
optimization: {
minimize: isProduction,
minimizer: [
// This is only used in production mode
new TerserPlugin({
terserOptions: {
parse: {
// we want terser to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 2018,
},
compress: {
ecma: 5,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
},
},
}),
],
},
plugins,
performance: {
hints: false,
},
}
}
export default async (env?: Partial<WebpackEnvParams>): Promise<Configuration> => {
const nodeEnv = process.env['NODE_ENV'] || 'development'
if (nodeEnv == 'debug') {
const config = {
...(await base(env)),
plugins: [],
profile: true,
}
return config
}
return base(env)
}