-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
178 lines (160 loc) · 4.7 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const path = require("path");
const webpack = require("webpack");
const CompressionPlugin = require("compression-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const WebpackShellPlugin = require("webpack-shell-plugin");
const HistoryApiFallback = require("./webpack-serve/historyApiFallback");
const { config } = require("./config/build");
console.log("CONFIG = ", config); // eslint-disable-line
const DEV = process.env.NODE_ENV === "development";
const PROD = process.env.NODE_ENV === "production";
module.exports = {
// Defaults to development, pass --mode production to override
mode: "development",
context: path.resolve(__dirname),
target: "web",
entry: {
app: "./src/index.tsx"
},
output: {
filename: "[name].[hash:7].js",
path: path.resolve(__dirname, "dist"),
publicPath: config.url.publicPath
},
module: {
rules: [
// Vanilla CSS
{
test: /\.css$/,
include: path.resolve(__dirname, "src"),
loaders: ["style-loader", "css-loader"]
},
// PostCSS
{
test: /\.scss$/,
include: path.resolve(__dirname, "src"),
loaders: [
"style-loader",
{
loader: "css-loader",
options: { modules: true, importLoaders: 1 }
},
"postcss-loader"
]
},
{
test: /\.(jpg|png|gif|mp4|webm|mp3|ogg|svg|mid|wav)$/,
loader: "file-loader",
options: {
name: "./f/[hash:16].[ext]"
}
},
// Mostly for tests, but legacy JS in source too
{
test: /\.jsx?$/,
exclude: /\/node_modules\//,
loader: "babel-loader",
options: {
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-syntax-dynamic-import",
"@babel/proposal-object-rest-spread",
...(DEV ? ["react-hot-loader/babel"] : [])
],
presets: ["@babel/env", "@babel/react"]
}
},
{
test: /\.tsx?$/,
exclude: /\/node_modules\//,
loader: "babel-loader",
options: {
plugins: [
"@babel/proposal-class-properties",
"@babel/plugin-syntax-dynamic-import",
"@babel/proposal-object-rest-spread",
...(DEV ? ["react-hot-loader/babel"] : [])
],
presets: ["@babel/env", "@babel/typescript", "@babel/react"]
}
}
]
},
plugins: [
// Inject app config at build-time: see comments in config/index.ts for details
new webpack.DefinePlugin({
__WEBPACK_DEFINE_CONFIG_JS_OBJ__: JSON.stringify(config)
}),
new webpack.NamedModulesPlugin(),
...(DEV
? new WebpackShellPlugin({
onBuildEnd: ["yarn --silent tsc:check:no-error --pretty"],
dev: false
})
: []),
new HtmlWebpackPlugin({
template: "./src/index.html",
favicon: "./src/static/favicon.ico"
}),
// MS Edge bindgen polyfill
// https://rustwasm.github.io/wasm-bindgen/reference/browser-support.html
new webpack.ProvidePlugin({
TextDecoder: ["text-encoding", "TextDecoder"],
TextEncoder: ["text-encoding", "TextEncoder"]
}),
// Generate .gz for production builds
// Consider adding brotli-webpack-plugin if your server supports .br
...(PROD
? [
new CompressionPlugin({
include: /\.(js|html|svg|wav)$/
})
]
: [])
],
optimization: {
// splitChunks: {
// cacheGroups: {
// vendors: {
// test: /\/node_modules\//,
// filename: "vendor.[hash:7].js",
// name: "vendor",
// chunks: "all"
// }
// }
// }
},
// Using cheap-eval-source-map for build times
// switch to inline-source-map if detailed debugging needed
devtool: PROD ? false : "cheap-eval-source-map",
serve: {
add: app => {
// historyApiFallback: required for redux-router to work on page refresh
// a refresh on localhost:8080/counter will go to the counter component defined in the route
app.use(HistoryApiFallback());
},
clipboard: false,
hotClient: {
logLevel: "warn"
},
devMiddleware: {
publicPath: config.url.publicPath,
stats: "minimal"
}
},
externals: {
cheerio: "window",
"react/addons": true,
"react/lib/ExecutionEnvironment": true,
"react/lib/ReactContext": true
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".wasm"],
modules: ["node_modules", path.resolve(__dirname, "src")],
alias: {
"@cfg": path.resolve(__dirname, "config"),
"@src": path.resolve(__dirname, "src"),
"@test": path.resolve(__dirname, "test")
}
}
};