This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.js
155 lines (149 loc) · 3.66 KB
/
webpack.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const path = require("path")
const webpack = require("webpack")
const git = require("git-rev-sync")
const production = process.env.NODE_ENV === "production"
const STYLE_LOADER = production ? MiniCssExtractPlugin.loader : "style-loader"
const hashType = production ? "chunkhash" : "hash"
const config = {
mode: production ? "production" : "development",
name: "client",
target: "web",
resolve: {
extensions: ['.js', '.ts', '.tsx'],
},
entry: {
app: path.resolve(__dirname, "src/app.tsx"),
},
output: {
chunkFilename: `[name].[${hashType}].js`,
path: path.resolve(__dirname, "build/"),
publicPath: "/",
filename: `[name].[${hashType}].js`,
},
devServer: {
compress: true,
historyApiFallback: true,
host: "0.0.0.0",
hot: true,
port: 3000,
},
module: {
rules: [
{
test: /\.(js|tsx?)$/,
exclude: /node_modules/,
loader: "babel-loader",
options: {
cacheDirectory: true,
},
},
{
test: /\.css$/,
use: [STYLE_LOADER, "css-loader"],
},
{
test: /\.scss$/,
use: [STYLE_LOADER, "css-loader", "sass-loader"],
},
{
test: /\.json$/,
exclude: /node_modules/,
loader: "json-loader",
},
{
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "file-loader",
},
{
// TODO: Is this used?
test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url-loader",
options: {
limit: 10000,
mimetype: "application/font-woff",
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: "src/robots.txt",
},
// Allow to override environment during development.
// If the file exists it will get prioritied over the template.
{
from: "env.template.js",
to: "env.js",
},
{
from: "env.override.js",
to: "env.js",
noErrorOnMissing: true,
force: true,
},
]
}),
new HtmlWebpackPlugin({
template: "src/index.html",
hash: false,
filename: "index.html",
inject: "body",
minify: {
collapseWhitespace: true,
},
}),
],
optimization: {
splitChunks: {
chunks: "all",
cacheGroups: {
manifest: {
name: "manifest",
test: /src[\\/]manifest\.js$/,
priority: 10,
enforce: true,
},
vendor: {
test: /node_modules/,
chunks: "initial",
name: "vendor",
priority: -10,
enforce: true,
},
},
},
// Ensure all chunk information only lands in the manifest file
// to avoid uneeded cache invalidation.
runtimeChunk: {
name: "manifest",
},
},
}
config.plugins.push(
new webpack.DefinePlugin({
DEBUG: production ? true : false,
__BUILD_INFO__: JSON.stringify({
buildTime: new Date().toString(),
gitCommitShort: git.short(),
}),
}),
)
if (production) {
// Split CSS to separate files
config.plugins.push(
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: `[name].[${hashType}].css`,
chunkFilename: `[name].[${hashType}].css`,
}),
)
}
module.exports = config