-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
225 lines (220 loc) · 6.68 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const path = require("path");
const dotenv = require("dotenv");
dotenv.config({ path: `./.env.${process.env.NODE_ENV}` });
const webpack = require("webpack");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const extractCSS = new ExtractTextPlugin("[name].css");
const copyFileTaks = require("./webpackPluginSets/CopyWebpackPluginSet");
const pageTemplasts = require("./webpackPluginSets/HtmlWebpackPluginSet");
const Dotenv = require("dotenv-webpack");
const NODE_ENV = process.env.NODE_ENV;
if (NODE_ENV === "development") {
console.log(
`browser webServer => http://${process.env.HOST}:${process.env.PORT} or http://localhost:${process.env.PORT}`
);
}
module.exports = {
mode: NODE_ENV,
context: path.resolve(__dirname, "./src"),
entry: {
main: "main",
p_pageLists: "assets/js/pages/p_pageLists",
p_index: "assets/js/pages/p_index",
p_editProfile: "assets/js/pages/p_editProfile",
p_register: "assets/js/pages/p_register",
p_allDynamicWall: "assets/js/pages/p_allDynamicWall",
p_followList: "assets/js/pages/p_followList",
p_postNews: "assets/js/pages/p_postNews",
p_myLike: "assets/js/pages/p_myLike",
p_personalPosts: "assets/js/pages/p_personalPosts",
p_thirdLogin: "assets/js/pages/p_thirdLogin",
},
devtool: NODE_ENV === "development" ? "cheap-module-source-map" : "false",
output: {
path: path.resolve(__dirname, NODE_ENV === "development" ? "dist" : "docs"),
filename: "./assets/js/[name].js?[hash:8]",
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
name: "vendor",
chunks: "initial",
enforce: true,
},
},
},
},
resolve: {
modules: [
path.resolve("src"),
path.resolve("src/js"),
path.resolve("src/scss"),
path.resolve("src/assets"),
path.resolve("node_modules"),
],
alias: {
vue$: "vue/dist/vue.esm.js", // vue 指定別名找 node_module/vue/dist/vue.esm.js 文件進行編譯
},
extensions: [".js"],
},
devServer: {
host: process.env.HOST,
port: process.env.PORT,
stats: {
assets: true, // 加入資源訊息
cached: false, // 加入暫存(但未建構)模塊的信息
chunkModules: false, // 將建構模塊信息加入到 chunk 信息
chunkOrigins: false,
chunks: false, // 加入 chunk 訊息(設置為 `false` 能允許較少的冗长輸出)
colors: true, // 等同 `webpack --colors`
hash: false, // 加入 compilation 的hash
modules: false, // 加入建構模塊訊息
reasons: false, // 加入模塊被引入的原因
source: false,
version: false, // 加入 webpack 版本信息
warnings: false, // 加入警告
},
/**
* Reloading [WDS] Disconnected! Fix
* 官方文件:https://webpack.docschina.org/configuration/dev-server/
* https://github.com/webpack/webpack-dev-server/issues/2199#issuecomment-522800528
* https://www.jianshu.com/p/85c0eb8f3b0f
* https://andyyou.github.io/2015/07/23/webpack/
*/
open: false,
// {
// app: [
// // 指定開啟瀏覽器 only one
// // 'Google Chrome',
// // 'Firefox',
// 'Firefox Developer Edition',
// // '--incognito', // 無痕模式
// '--other-flag',
// ],
// },
overlay: {
warnings: true,
errors: true,
}, // 編譯器錯誤或警告時全屏覆蓋
hot: false, // webpack 模块熱替換
inline: true, // false 瀏覽器路徑多 /webpack-dev-server/ 與狀態列 App ready.
noInfo: true, // --no-info option
// liveReload: false,
writeToDisk: true, // 檔案形式輸出 dev-server 程式碼,設定 false 內容只會在記憶體中不會有實體檔案
compress: true, // 啟用 gzip 壓縮,預設 false
},
module: {
rules: [
{
test: /\.(pug|jade)$/,
// use: ['html-loader', 'pug-html-loader'], // 預設壓單行
use: [
{
loader: "html-loader",
options: {
// minimize: false, // 不壓縮 HTML
},
},
{
loader: "pug-html-loader",
options: {
pretty: true, // 美化 HTML 的編排 (不壓縮 HTML 的一種)
data: {
env: process.env,
},
},
},
],
},
{
test: /\.(sass|scss)$/,
use: extractCSS.extract([
{
loader: "css-loader",
options: {
sourceMap: true, // 開啟 sourcemap 支持
},
},
{
loader: "postcss-loader",
options: {
sourceMap: true, // 開啟 sourcemap 支持
},
},
{
loader: "sass-loader",
options: {
sourceMap: true, // 開啟 sourcemap 支持
},
},
]),
include: path.resolve("src/scss"),
exclude: path.resolve("./node_modules"),
},
{
test: /\.(js)$/,
use: "babel-loader",
include: path.resolve("."),
},
{
test: /\.(jpe?g|png|gif|svg)$/,
use: [
{
loader: "url-loader",
options: {
limit: 8192,
name: "[path][name].[ext]?[hash:8]",
},
},
{
loader: "image-webpack-loader",
options: {
disable: NODE_ENV === "production" ? false : true,
mozjpeg: {
progressive: true,
quality: 65,
},
optipng: {
enabled: false,
},
pngquant: {
quality: [0.65, 0.9],
speed: 4,
},
gifsicle: {
interlaced: false,
},
},
},
],
include: path.resolve("src/assets/images"),
exclude: path.resolve("./node_modules"),
},
{
// 自定義安裝字型檔
test: /\.(woff|woff2|ttf|eot)$/,
loader: "file-loader",
options: {
name: "[path][name].[ext]?[hash:8]",
},
include: path.resolve("src/assets"),
exclude: path.resolve("./node_modules"),
},
],
},
plugins: [
new Dotenv({
/** dotenv-webpack 環境變數
* NODE_ENV 來決定取用 .env 檔
* 傳入 webpack 編譯的環境變數與 dotenv 環境變數不同
*/
path: `./.env.${process.env.NODE_ENV}`,
systemvars: true, // 允許讀取 process.env 下的任意系統變量
}),
extractCSS,
...copyFileTaks,
...pageTemplasts,
],
};