-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpack.config.js
166 lines (159 loc) · 4.31 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
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
/**
* 通用基础配置
* @author tangsj
*/
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const pagesRoot = './src/pages';
const componentsRoot = './src/components';
const env = process.env.NODE_ENV;
const pageEntry = {}; // 页面入口
const pageHtml = []; // 页面模板
const commonChunks = []; // 自定义组件
/**
* 按规则生成entry 及 html plugin
* 在 pages 目录的一个目录表示一个页面
* 页面目录下必须包含一个 index.html(模板) 和 index.js(入口)
* 其它比如图片,css及页面模块也放在该目录下引用即可
*/
!function generateEntryAndHtml() {
const pages = fs.readdirSync(pagesRoot);
pages.forEach(function (name, index) {
const url = path.join(__dirname, pagesRoot, name);
if (fs.statSync(url).isDirectory()) {
pageEntry[name] = `${pagesRoot}/${name}`;
const hwp = new HtmlWebpackPlugin({
filename: `${pagesRoot.replace('./src/', '')}/${name}/index.html`,
template: `${pagesRoot}/${name}/index.html`,
chunks: ['vendor', 'common', name],
inject: 'body',
minify: {
collapseWhitespace: true,
},
});
pageHtml.push(hwp);
}
});
}();
/**
* 生自定义组件打包使用的数组
* array
*/
!function generateCommonChunks(folder) {
const comps = fs.readdirSync(folder);
comps.forEach(function (name, index) {
const url = path.join(folder, name);
if (fs.statSync(url).isDirectory()) {
generateCommonChunks(url);
} else {
const ext = path.extname(url).substring(1);
if (ext.toLowerCase() === 'js') {
commonChunks.push(path.join(__dirname, url));
}
}
});
}(componentsRoot);
const config = {
// 入口
entry: Object.assign(pageEntry, {
// 将所有第3方模块提取到一个chunk - vendor
'vendor': [
'lodash',
],
// 将自己写的所有通用模块提取到一个chunk - common
'common': commonChunks,
}),
// 输出
output: {
sourceMapFilename: '[file].map',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
resolve: {
// 快捷访问
alias: {
'@': path.resolve(__dirname, 'src'),
'@c': path.resolve(__dirname, 'src', 'components'),
},
// 自动匹配文件后缀顺序
extensions: ['.js', '.json', '.css'],
// 模块搜索目录
modules: [
path.join(__dirname, 'src'),
path.join(__dirname, 'node_modules'),
],
},
module: {
// 不解析
noParse(content) {
return /lodash/.test(content);
},
// loaders
rules: [
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src', 'img:src', 'audio:src', 'video:src', 'video:poster', 'source:src'],
},
},
},
{
test: /\.(png|jpe?g|gif|svg|mp[3,4])$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
outputPath: function (path) {
return path.replace('src/pages', 'images');
},
name: '[path][name].[hash:8].[ext]',
},
},
],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
use: [
{
loader: 'file-loader',
options: {
name: 'font/[name].[hash:8].[ext]'
}
}
],
},
{
test: /\.js$/i,
include: path.resolve(__dirname, 'src'),
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
'env',
],
plugins: [
'transform-runtime'
],
compact: 'auto', // 不要包含多余的空格符和换行符 设置为 "auto" 时,当输入大小 > 500KB 时,compact会被设置为 true
},
},
},
],
},
// 插件
plugins: [
// 提取公用文件
new webpack.optimize.CommonsChunkPlugin({
// 这里的顺序和html里面生成的script 标签顺序有关系
// 这样生成的script 顺序是 vendor -> common
names: ['common', 'vendor'],
}),
].concat(pageHtml),
};
module.exports = config;