-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
88 lines (83 loc) · 2.25 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const path = require('path');
const glob = require('glob');
const webpack = require('webpack');
const loaderConfig = require('./config/loader.config')
const serverConfig = require('./config/server.config')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
var config = {
// 入口配置
entry: {
jquery: 'jquery',
index: './src/index.js'
},
// 出口配置
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'front/js/[name].js'
},
// 规则
module: loaderConfig,
// 服务器
devServer: serverConfig,
// 插件
plugins: [
new CleanWebpackPlugin(['dist']), //删除某个目录
new webpack.HotModuleReplacementPlugin(), //热重载
/* new HtmlWebpackPlugin({
hash: true,
title: 'a demo',
template: './src/index.html'
}), */
new ExtractTextPlugin('front/css/site.css'), //提取css文件
new webpack.ProvidePlugin({ //引入jQuery
$: 'jqeury',
jQuery: 'jquery'
}),
new CopyWebpackPlugin([{ //输出静态资源
from: path.resolve(__dirname, 'src/assets'),
to: './front'
}])
],
optimization: {
splitChunks: {
cacheGroups: {
vender: {
chunks: 'initial',
name: 'jquery',
enforce: true
}
}
}
}
}
var pages = getEntry('./src/pages/**/*.ejs');
// console.log(pages)
for (var pathname in pages) {
var conf = {
filename: path.resolve(__dirname, 'dist/' + pathname + '.html'),
template: path.resolve(__dirname, './src/pages/' + pathname + '/' + pathname + '.js'),
excludeChunks: ['jquery', 'index']
// cache:true,
}
config.plugins.push(new HtmlWebpackPlugin(conf))
}
function getEntry(globPath, pathDir) {
var files = glob.sync(globPath);
// console.log(files)
var entries = {},
entry, dirname, basename, pathname, extname;
for (var i = 0; i < files.length; i++) {
entry = files[i];
dirname = path.dirname(entry);
extname = path.extname(entry);
basename = path.basename(entry, extname);
pathname = path.join(dirname, basename);
entries[basename] = './' + entry;
// console.log(files.length, entry, dirname, basename, pathname, extname)
}
return entries;
}
module.exports = config;