-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
120 lines (113 loc) · 3.41 KB
/
webpack.config.js
File metadata and controls
120 lines (113 loc) · 3.41 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
const { resolve } = require('path')
const fs = require('fs')
const DotEnv = require('dotenv')
DotEnv.config({ path: '.env.local' })
DotEnv.config()
const pages = require('./pages.config.json')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const { log } = require('./node-utils')
const ESLintWebpackPlugin = require('eslint-webpack-plugin')
const isDevelopment = process.env.NODE_ENV === 'development'
const { PUBLIC_PATH, BUILD_PATH } = process.env
const __root__ = __dirname
// 插件配置
const plugins = pages.map(page => new HtmlWebpackPlugin({
template: resolve(__root__, `src/page/${page.name}/${page.template ?? 'template'}.html`),
filename: page.name + '.html',
chunks: ['common-style', 'common', page.name],
templateParameters: {
title: page.title,
keyword: page.keyword,
description: page.description,
}
}))
if(!isDevelopment){
plugins.push(new MiniCssExtractPlugin({ filename: 'style/[id].[hash].css' }))
}
plugins.push(
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
'**/*',
'!.git/**'
]
}),
new ESLintWebpackPlugin()
)
// 入口配置
const entry = {
'common-style': resolve(__root__, `src/style/public.less`)
}
for(const page of pages) {
if(!page.name) {
log.error('The [ name ] is required. Please check your [ page.config.js ]')
process.exit(1)
}
const pagePath = resolve(__root__, 'src/page', page.name)
const entryJs = resolve(pagePath, 'main.js')
const entryLess = resolve(pagePath, 'main.less')
if(fs.existsSync(entryJs)){
entry[page.name] = entryJs
} else if(fs.existsSync(entryLess)){
entry[page.name] = entryLess
} else {
log.error(`Can not found file [ main.js ] or [ main.less ] at [ ${pagePath} ]`)
process.exit(1)
}
}
// 输出配置
const output = {
path: resolve(__root__, isDevelopment ? 'dist' : BUILD_PATH ),
filename: 'script/[id].[hash].js'
}
if (!isDevelopment) {
output.publicPath = PUBLIC_PATH
}
// 主配置
module.exports = {
mode: isDevelopment ? 'development' : 'production',
entry,
output,
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: ['babel-loader'],
},
{
test: /\.(?:le|c)ss$/,
use: [
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader:'css-loader',
options: { esModule: false }
},
'less-loader',
]
},
{
test: /\.(?:jpe?g|png|webp|gif|svg|eot|woff2?|ttf)$/,
use: [
{ loader: 'file-loader', options: { name:'assets/[hash].[ext]' } },
],
},
]
},
plugins,
resolve: {
alias: {
'#': resolve(__root__),
'@': resolve(__root__, 'src'),
}
},
optimization: {
minimizer: [
new CssMinimizerPlugin(),
new UglifyJsPlugin()
],
},
}