-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
146 lines (134 loc) · 4.17 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
const path = require("path");
const webpack = require("webpack");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const HtmlWebpackPlugin = require("html-webpack-plugin"),
webpackDevServerWaitpage = require("webpack-dev-server-waitpage"),
CopyWebpackPlugin = require("copy-webpack-plugin"),
WorkboxPlugin = require("workbox-webpack-plugin"),
ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin"),
ESLintPlugin = require("eslint-webpack-plugin");
const node_modules = /[\\/]node_modules[\\/]/;
const PRODUCTION = process.env.NODE_ENV === "production";
const ANALYZE = process.env.ANALYZE;
const outputPath = path.resolve(__dirname, PRODUCTION ? "dist" : "dist-dev");
module.exports = {
target: PRODUCTION ? "browserslist" : "web",
bail: PRODUCTION,
output: {
filename: "[name]-[contenthash].js",
path: outputPath,
publicPath: "/",
chunkFilename: "[name]-[chunkhash].js",
devtoolModuleFilenameTemplate: info =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, "/")
},
devtool: ANALYZE ? "source-map" : PRODUCTION ? false : "cheap-module-source-map",
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: node_modules,
name: "vendors",
chunks: "all"
}
}
}
},
devServer: {
hot: true,
port: 8080,
compress: true,
historyApiFallback: true,
onBeforeSetupMiddleware: server => {
server.app.use(require("./api.mock"));
// Be sure to pass the server argument from the arguments
server.app.use(webpackDevServerWaitpage(server, { theme: "material" }));
}
},
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
symlinks: false,
fallback: {
vm: false
}
},
plugins: [
new webpack.BannerPlugin({
// Add copyright notice
banner: (() => {
const packageJson = require("./package.json");
const author = (packageJson.author && packageJson.author.name) || packageJson.author;
return `Copyright (c) ${new Date().getFullYear()} ${author}. All Rights Reserved.`;
})(),
entryOnly: true,
exclude: /\.worker\.js$/,
raw: false // wrap in a comment
}),
new HtmlWebpackPlugin({
template: "src/index.ejs",
scriptLoading: "defer"
}),
PRODUCTION && new CleanWebpackPlugin(), // Cleanup before each build
!PRODUCTION && new ReactRefreshWebpackPlugin(), // hot module replacement for React
!PRODUCTION && webpackDevServerWaitpage.plugin(),
new CopyWebpackPlugin({
patterns: [{ from: "public", to: "." }]
}),
ANALYZE && new BundleAnalyzerPlugin({ analyzerMode: "static", openAnalyzer: false }),
PRODUCTION &&
new WorkboxPlugin.GenerateSW({
skipWaiting: true,
exclude: [/\.html$/, /htaccess/, /robots\.txt/, /\.php$/],
runtimeCaching: [
{
urlPattern: ({ event }) => event.request.url === "/",
handler: "NetworkFirst"
}
]
}),
new ESLintPlugin({
lintDirtyModulesOnly: !PRODUCTION,
formatter: require("eslint-friendly-formatter"),
failOnWarning: PRODUCTION,
failOnError: PRODUCTION,
emitWarning: !PRODUCTION
})
].filter(Boolean),
module: {
strictExportPresence: true,
rules: [
{
test: /\.(ico|gif|png|jpe?g|svg)$/,
use: {
loader: "url-loader",
options: {
limit: 10 * 1024, // 10KB
name: "[path][name].[ext]",
context: "src"
}
}
},
{
test: /\.worker\.js$/,
use: {
loader: "worker-loader",
options: { inline: "no-fallback" }
}
},
{
test: /\.js$/,
exclude: node_modules,
resolve: {
fullySpecified: false
},
use: {
loader: "babel-loader",
options: {
cacheDirectory: !PRODUCTION
}
}
}
]
}
};