-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
182 lines (175 loc) · 5.21 KB
/
webpack.common.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
const webpack = require('webpack');
const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const autoprefixer = require('autoprefixer');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ASSET_PATH = process.env.ASSET_PATH || '../';
const getEntry = () => {
const entry = {};
glob.sync('./src/js/*.js').forEach((name) => {
const start = name.indexOf('/src/js/') + 8;
const end = name.length - 3;
const eArr = [];
const n = name.slice(start,end);
eArr.push(name);
entry[n] = eArr;
});
return entry;
};
const config = {
entry: getEntry(),
output: {
path: path.resolve(__dirname, './dist'),
filename: './js/[name].js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use:[
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
]
},
{
test: /\.pug$/,
use: [
{
loader: 'html-loader',
options: {
minimize: false // 不壓縮 HTML
}
},
{
loader: 'pug-html-loader',
options: {
pretty: true // 美化 HTML 的編排 (不壓縮HTML的一種)
}
},
]
},
{
test: /\.(sc|sa|c)ss$/,
exclude: /node_modules/,
use: [
'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: ASSET_PATH
}
},
{
loader: 'css-loader',
options: {
url: false
}
},
'postcss-loader',
'sass-loader'
]
},
{
test: /\.styl$/,
exclude: /node_modules/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: ASSET_PATH
}
},
'css-loader',
'postcss-loader',
'stylus-loader'
]
},
{
test: /\.(png|svg|jpg|jpge|gif)$/,
exclude: /node_modules/,
use: {
loader: 'url-loader',
options: {
limit: 8192, //bytes
name: '[name].[ext]?[hash:7]',
outputPath: 'images'
}
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 8192,
// name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
exclude: /node_modules/,
use: {
loader: 'file-loader',
options: {
limit: 8192, //bytes
name: '[name].[ext]?[hash:7]',
}
}
}
]
},
plugins:[
new CleanWebpackPlugin(),
new webpack.ProvidePlugin({
$: "jquery",
jquery: "jQuery",
"window.jQuery": "jquery"
}),
new MiniCssExtractPlugin({
filename: './css/[name].css',
chunkFilename: "./css/[name].css"
}),
new webpack.LoaderOptionsPlugin({
options: {
postcss: [
autoprefixer()
]
}
}),
],
resolveLoader: {
extensions: ['.js', '.styl'],
},
resolve: {
alias: {
jquery: "jquery/src/jquery"
}
},
stats: {
builtAt: false,
children: false,
chunks: false,
modules: false,
timings: false,
version: true // webpack v
},
};
Object.keys(config.entry).forEach((name) => {
config.plugins.push(new HtmlWebpackPlugin({
template:`./src/${name}.pug`,
filename: `${name}.html`,
chunks:['common','runtime','vendor','action',`${name}`],
minify: {
removeComments: false,
collapseWhitespace: false, // 壓縮 HTML
removeAttributeQuotes: false,
},
}));
});
module.exports = config;