-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwebpack.config.js
225 lines (212 loc) · 5.85 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const path = require('path');
const webpack = require('webpack');
const globby = require('globby');
const { VueLoaderPlugin } = require('vue-loader');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// 输出文件夹
const DIST_DIR = 'dist';
// vue入口文件
const SPA_DIR_PATTERN = '!pages/spa';
const LIB_DIR_PATTERN = '!pages/**/lib';
const COMPONENTS_DIR_PATTERN = '!pages/**/components';
// js脚本文件入口
const SCRIPTS_PATTERNS = [
'./pages/**/*{.js,.jsx}',
// 排除以下路径
LIB_DIR_PATTERN,
COMPONENTS_DIR_PATTERN,
SPA_DIR_PATTERN,
];
const STYLES_PATTERNS = [
'./pages/**/*.less',
// 排除以下路径
LIB_DIR_PATTERN,
COMPONENTS_DIR_PATTERN,
SPA_DIR_PATTERN,
];
// 单页引用路径
const SPA_PATTERNS = ['./pages/spa/index.js'];
const scriptFiles = globby.sync(SCRIPTS_PATTERNS);
const styleFiles = globby.sync(STYLES_PATTERNS);
const spaFiles = globby.sync(SPA_PATTERNS);
function getFilePathMap(filePaths, query = '') {
const map = {};
for (const filePath of filePaths) {
map[filePath] = filePath + query;
}
return map;
}
function makeFilename(info) {
const file = info.chunk.name;
const dir = path.dirname(file);
const ext = path.extname(file).toLowerCase();
const basename = path.basename(file, ext);
// 如果是 mini-css-extract-plugin 插件产生的无用脚本,就在文件名后加个标志,防止误用
let filename;
if (['.less', '.css'].includes(ext)) {
if ('css/mini-extract' in info.chunk.contentHash) {
filename = `${dir}/${basename}.mini_css_cache.js`;
} else {
filename = `${dir}/${basename}.css.js`;
}
} else {
filename = `${dir}/${basename}.js`;
}
return filename;
}
const isProduction = process.env.NODE_ENV === 'production';
const baseConfig = {
mode: process.env.NODE_ENV || 'development',
devtool: isProduction ? false : 'inline-source-map',
target: 'es5',
cache: {
type: 'filesystem',
cacheDirectory: path.resolve(__dirname, '.webpack_cache'),
buildDependencies: {
config: [__filename],
},
},
};
module.exports = {
...baseConfig,
entry: {
...getFilePathMap(scriptFiles),
...getFilePathMap(styleFiles, '?css_assets'),
...getFilePathMap(spaFiles),
},
output: {
path: path.resolve(__dirname, DIST_DIR),
filename: makeFilename,
libraryTarget: 'umd',
globalObject: 'this',
publicPath: '/',
chunkFormat: 'commonjs',
},
module: {
rules: [
// https://vue-loader.vuejs.org/zh/guide/linting.html#eslint
/*{
enforce: 'pre',
test: /\.vue$/,
loader: 'eslint-loader',
exclude: /node_modules/,
},*/
{
test: /\.vue?$/,
loader: 'vue-loader',
},
{
test: /\.(le|c)ss?$/,
oneOf: [
// 这里匹配独立打包的less文件 (styleFiles数组内的文件)
{
resourceQuery: /\?css_assets/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false, // 不打包css中的外部资源(如字体、background-image: url(xxx))
},
},
'less-loader',
],
},
// 这里匹配使用CSS Modules的vue单文件组件
{
resourceQuery: /module/,
use: [
'vue-style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[local]_[hash:base64:8]',
},
},
},
'less-loader',
],
},
// 这里匹配使用普通全局style和scoped style的vue单文件组件
{
use: ['vue-style-loader', 'css-loader', 'less-loader'],
},
],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|ico)(\?\S*)?$/,
loader: 'file-loader',
options: {
name: '[name]-[contenthash].[ext]',
outputPath: 'pages',
publicPath: '/',
},
},
{
test: /\.jsx?$/,
use: [
// 出现语法兼容性问题时启用这个loader,但是*不推荐*,因为它会破坏源码映射
// {
// loader: "buble-loader"
// },
{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: {
ie: '8',
chrome: '36',
},
},
],
['@vue/babel-preset-jsx'],
],
plugins: [
[require.resolve('@babel/plugin-transform-modules-commonjs')],
[require.resolve('@babel/plugin-transform-runtime')],
[require.resolve('babel-plugin-preval')],
],
compact: false,
},
},
],
},
{
test: /\.pug$/,
use: ['pug-plain-loader'],
},
],
},
plugins: [
// new BundleAnalyzerPlugin(),
new VueLoaderPlugin(),
/* new ESLintPlugin({
extensions: '.vue',
}),*/
new webpack.ProgressPlugin(),
// new MiniCssExtractPlugin(),
new MiniCssExtractPlugin({
filename: (info) => {
const file = info.chunk.name;
const dir = path.dirname(file);
const ext = path.extname(file).toLowerCase();
const basename = path.basename(file, ext);
if (['.less', '.css'].includes(ext)) {
return `${dir}/${basename}.css`;
} else {
return `${dir}/${basename}${ext}.mini_css_out`;
}
},
}),
],
externals: {
vue: 'Vue',
},
resolve: {
extensions: ['.js', '.jsx', '.vue', '.json', '.less', '.css'],
},
};