-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.client.js
196 lines (193 loc) · 5.88 KB
/
webpack.client.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const path = require("path")
const webpack = require("webpack")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const WorkboxPlugin = require("workbox-webpack-plugin")
const WebpackPwaManifest = require("webpack-pwa-manifest")
// const ImageminWebpWebpackPlugin = require("imagemin-webp-webpack-plugin")
const TerserJSPlugin = require("terser-webpack-plugin")
const {
isDEV,
webpackPlugins,
babelPlugins,
babelPresets,
webpackResolve,
getCommandArg,
commonRules,
webpackSplitChunks,
} = require("./webpack.common.js")
const ModuleHtmlPlugin = require("./module-html-plugin")
// const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
// .BundleAnalyzerPlugin
const settedBabelPlugins = !isDEV
? [
...babelPlugins,
["transform-remove-console", { exclude: ["error", "warn"] }],
]
: [...babelPlugins, "react-hot-loader/babel"]
const webpackAlias = isDEV
? {
"react-dom": "@hot-loader/react-dom",
}
: {}
module.exports = (env, argv) => {
console.log("env", env)
return {
target: ["web", "es6"],
entry: {
client: path.resolve(__dirname, "./src/client/index.js"),
},
output: {
publicPath: isDEV ? "/" : "/public/",
filename: isDEV ? "[name]-[hash].js" : `[name]-[contenthash].js`,
chunkFilename: isDEV ? "[name]-[hash].js" : `[name]-[contenthash].js`,
path: path.resolve(__dirname, "public"),
},
devServer: {
proxy: {
"/api": {
target: "https://81.69.200.140",
// 修改发往 target的host: "localhost:8010", referrer: "http://localhost:8010/"
changeOrigin: true,
secure: false,
},
},
publicPath: "/",
historyApiFallback: true,
port: 8010,
compress: true,
contentBase: path.join(__dirname, "./public"),
},
devtool: isDEV ? "eval-cheap-source-map" : "nosources-source-map",
resolve: {
...webpackResolve,
alias: webpackAlias,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: "babel-loader",
options: {
presets: [babelPresets(), "@babel/preset-react"],
plugins: [...settedBabelPlugins],
},
exclude: /node_modules/,
},
...commonRules(),
],
},
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
}),
...webpackPlugins,
isDEV
? new HtmlWebpackPlugin({
title: "music-motion",
template: path.resolve(__dirname, "./index.html"),
})
: new HtmlWebpackPlugin({
minify: {
collapseWhitespace: true,
removeComments: false,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
title: "music-motion",
template: path.resolve(__dirname, "./src/assets/index.html"),
filename:
env.module === "yes"
? path.join(__dirname, "./public/views/index.html")
: path.join(__dirname, "./public/build/views/index.html"),
favicon: path.resolve(__dirname, "./src/assets/favicon.ico"),
}),
new webpack.DefinePlugin({
RENDER_OPTS: JSON.stringify(getCommandArg("render")),
}),
new WebpackPwaManifest({
name: "Pika",
short_name: "Pika",
description: "A PWA Muisc Web Site",
display: "standalone",
start_url: "/",
background_color: "#ffffff",
theme_color: "#FEDD27",
inject: true,
ios: true,
scope: "",
icons: [
{
src: path.resolve(__dirname, "./src/assets/pika_tail_192x192.png"),
sizes: "192x192",
type: "image/png",
ios: true,
},
{
src: path.resolve(__dirname, "./src/assets/pika_tail_512x512.png"),
sizes: "512x512",
type: "image/png",
ios: true,
},
{
src: path.resolve(__dirname, "./src/assets/pika_tail_152x152.png"),
sizes: "152x152",
type: "image/png",
ios: true,
},
],
}),
new WorkboxPlugin.InjectManifest({
swSrc: path.join(process.cwd(), "./src/src-service-worker.js"),
swDest: "service-worker.js",
exclude: [
/\.map$/,
/manifest$/,
/\.htaccess$/,
// /service-worker\.js$/,
/\.js$/,
/\.webp$/,
],
}),
new ModuleHtmlPlugin(),
// new BundleAnalyzerPlugin(),
],
optimization: {
minimize: !isDEV,
minimizer: !isDEV
? [
new TerserJSPlugin({
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: 8,
},
compress: {
ecma: 2015,
warnings: false,
},
output: {
ecma: 2015,
comments: false,
safari10: false,
},
safari10: false,
},
extractComments: false,
// cache: true,
parallel: true,
sourceMap: true, // Must be set to true if using source-maps in production
}),
]
: [],
runtimeChunk: true,
splitChunks: webpackSplitChunks,
},
}
}