-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
134 lines (133 loc) · 3.34 KB
/
webpack.config.js
File metadata and controls
134 lines (133 loc) · 3.34 KB
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
const path = require('path');
// 載入轉存 CSS 檔案的套件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// 壓縮 CSS
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
// 產一個全新的 html
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 壓縮 JS
const TerserWebpackPlugin = require('terser-webpack-plugin');
// 清除多餘的 JS 檔案
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// 清除多餘的 CSS 檔案
module.exports = {
context: path.resolve(__dirname, 'src'),
// 進入點
entry: {
//- 入口 js
main: './js/index.js',
//- css
// all: './scss/all.scss'
},
// 出口
output: {
filename: 'js/[name].bundle.js',
//- 有需要用到 cdn 才需要
publicPath: './',
path: path.resolve(__dirname, 'dist') // dirname = '/'
},
module: {
rules: [{
test: /\.css$/,
use: [
'style-loader', // 這個會後執行
'css-loader' // 這個會先執行
]
},
{
test: /\.(sa|sc|c)ss$/,
use: [{
loader: MiniCssExtractPlugin.loader
},
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: function() {
return [
require('autoprefixer'),
require('cssnano')({ preset: ['default', { discardComments: { removeAll: true } }] })
];
}
}
},
'sass-loader'
]
}, {
test: /\.pug$/,
use: ['pug-loader']
},
{
test: /\.(js)$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.(png|jpg|gif|jpe?g|svg)$/,
use: [{
loader: 'url-loader',
options: {
name: '[name].[ext]',
publicPath: './images',
outputPath: './images'
}
},
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true,
}
}
]
},
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: ['img:src', 'img:data-src', 'audio:src'],
minimize: true
}
}
},
]
},
//- 壓縮 CSS, JS
optimization: {
minimizer: [
//- 壓縮 JS
new TerserWebpackPlugin(),
//- 壓縮 CSS
new OptimizeCssAssetsWebpackPlugin()
]
},
plugins: [
//- 4.0後默認刪除 output 資料夾內檔案
new CleanWebpackPlugin({
//- 要刪除的根目錄
// root: path.resolve(__dirname, '../../'),
//- 顯示刪除訊息
"verbose": true,
//- 不希望刪除的檔案加在下面
// "exclude": ['05ef02be5a02714eab77.vendor.js'],
}),
// new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
// 指定輸出位置
// [name] 為上方進入點設定的 "名稱"
filename: "./css/all.min.css",
}),
// 有幾個 pug 檔就要用幾個
new HtmlWebpackPlugin({
title: 'RexWebpack',
hash: true,
template: './pug/index.pug',
filename: './index.html'
}),
]
};