-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.ts
88 lines (85 loc) · 2.32 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
import webpack from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import WorkboxWebpackPlugin from 'workbox-webpack-plugin'
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
import { execSync } from 'child_process'
import path from 'path'
const { NODE_ENV = 'development' } = process.env
const prod = NODE_ENV === 'production'
const GIT_SHA = execSync('git rev-parse HEAD')
const UPDATED_AT = new Date()
const config: webpack.Configuration = {
entry: './src/main.tsx',
output: {
publicPath: '/',
path: path.resolve(__dirname, 'dist'),
filename: prod ? '[name].[contenthash].js' : '[name].js',
chunkFilename: '[name].[chunkhash].js'
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
favicon: './src/images/favicon.png'
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: `"${prod ? 'production' : 'development'}"`,
GIT_SHA: `"${GIT_SHA.toString().trim()}"`,
UPDATED_AT: `"${UPDATED_AT.toISOString()}"`
}
})
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/']
},
{
test: /\.css$/i,
use: [
prod ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: prod ? '[hash:12]' : '[local]_[hash:4]'
}
}
},
'postcss-loader'
]
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset'
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
}
}
export = (): webpack.Configuration => {
if (prod) {
config.mode = 'production'
config.devtool = 'source-map'
config.optimization = {
minimizer: [new CssMinimizerPlugin(), new TerserPlugin()]
}
config.plugins!.push(
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[name].[chunkhash].css'
}),
new WorkboxWebpackPlugin.GenerateSW()
)
} else {
config.mode = 'development'
config.devtool = 'cheap-module-source-map'
}
return config
}