-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
70 lines (67 loc) · 1.82 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
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const glob = require('glob')
const { argv } = require('process')
const getEntries = () => {
const entries = {}
const templates = []
const files = glob.sync(path.resolve(__dirname, 'src', './**/*.js'))
for (let i = 0; i < files.length; i++) {
const filename = files[i]
const parsePath = path.parse(filename)
const key = parsePath.name
entries[key] = filename
templates.push(
new HtmlWebpackPlugin({
filename: key + '.html',
chunks: [key],
template: path.resolve(parsePath.dir, `${key}.html`),
})
)
}
return { entries, templates }
}
const IS_PROD =
argv[argv.indexOf('--mode') + 1] === 'production' ||
process.env.NODE_ENV === 'production'
const { entries, templates } = getEntries()
module.exports = {
entry: entries,
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: IS_PROD ? 'js/[name].[contenthash:8].js' : 'js/[name].js',
chunkFilename: '[name].js',
clean: true,
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: [{ loader: 'babel-loader' }],
},
{
test: /\.s[ac]ss$/i,
use: [
// 将 JS 字符串生成为 style 节点
IS_PROD ? MiniCssExtractPlugin.loader : 'style-loader',
// 将 CSS 转化成 CommonJS 模块
'css-loader',
// 将 Sass 编译成 CSS
'sass-loader',
'postcss-loader',
],
},
],
},
resolve: {
extensions: ['.json', '.js'],
},
devtool: 'source-map',
plugins: [
...templates,
new MiniCssExtractPlugin({ filename: 'css/[name].[contenthash:8].css' }),
],
}