-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.dev.js
157 lines (141 loc) · 3.75 KB
/
webpack.dev.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
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const argv = require('yargs').argv
const vendorModule = ['jquery', 'babel-polyfill', 'vue', 'vuex', 'axios', 'mock']
const config = {
mode: 'development',
entry: {
'index': './src/index/index.js',
'task': './src/task/task.js',
'ask': './src/ask/ask.js',
'find': './src/find/find.js',
'contact': './src/contact/contact.js',
'user': './src/user/user.js',
'centre': './src/centre/centre.js',
'login': './src/login/login.js',
'setting': './src/setting/setting.js',
'centre-dynamic': './src/centre/centre-dynamic.js',
'vendor': vendorModule
},
output: {
filename: 'js/[name].js',
chunkFilename: 'js/[id].js',
path: path.resolve(__dirname, 'public/')
},
module: {
rules: [{
test: /\.s?[ac]ss$/,
use: [
'style-loader',
'css-loader',
'resolve-url-loader',
'postcss-loader?sourceMap',
'sass-loader?sourceMap'
]
}, {
test: /\.ejs$/,
use: ['ejs-do-loader']
},
{
test: /\.vue$/,
use: ['vue-loader']
},
{
test: /\.js$/,
use: ['babel-loader']
},
{
test: /\.(png|jpg|jpeg|gif)(\?.+)?$/,
use: [{
loader: 'url-loader',
options: {
limit: 10000,
name: 'img/[name].[ext]',
publicPath: '../'
}
}]
},
{
test: /\.(eot|woff2?|svg|ttf)([\?]?.*)$/,
use: [{
loader: 'file-loader',
options: {
limit: 1000,
name: 'font/[name].[ext]',
publicPath: '../'
}
}]
}]
},
resolve: {
alias: {
mock: path.resolve(__dirname, 'src/common/js/mock/mock.js'),
vue: 'vue/dist/vue.js'
}
},
optimization: {
splitChunks: {
name: 'vendor',
minChunks: Infinity
}
// runtimeChunk: {
// name: 'manifest'
// }
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new VueLoaderPlugin(),
new CopyWebpackPlugin([
{ from: 'node_modules/bootstrap/dist/css', to: 'vendor/bootstrap/css/' },
{ from: 'node_modules/tinymce/plugins', to: 'js/plugins' },
{ from: 'node_modules/tinymce/themes', to: 'js/themes' },
{ from: 'node_modules/tinymce/skins', to: 'js/skins' }
])
]
}
// 解析子目录和文件名
const parseFileInfo = function (fileName) {
let directory = fileName
const moduleNameParse = fileName.split('-')
if (moduleNameParse && moduleNameParse.length === 2) {
directory = moduleNameParse[0]
}
return {
directory,
fileName
}
}
// 获取html-webpack-plugin参数
const getHtmlConfig = function (name, type = 'multi') {
const {directory, fileName} = parseFileInfo(name)
const options = {}
if (type === 'multi') {
options.chunks = ['vendor', name]
options.filename = fileName + '.html'
}
options.template = path.resolve(__dirname, 'src/', directory, `${fileName}.ejs`)
return options
}
// 未指定模块,构建全部
if (!argv.define) {
const cloneEntry = {...config.entry}
delete cloneEntry.vendor
const pages = Object.keys(cloneEntry)
pages.forEach(page => {
config.plugins.push(new HtmlWebpackPlugin(getHtmlConfig(page)))
})
} else {
const {directory, fileName} = parseFileInfo(argv.define)
config.entry = {}
config.entry.vendor = vendorModule
config.entry[fileName] = path.resolve(__dirname, 'src/', directory, fileName + '.js')
config.plugins.push(new HtmlWebpackPlugin(getHtmlConfig(fileName, 'single')))
}
console.log('entry', config.entry)
module.exports = config