-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.prod.js
145 lines (138 loc) · 3.87 KB
/
webpack.prod.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
'use strict';
const glob = require('glob');
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); //提取 css 到单独文件
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); //压缩 css 文件
const { CleanWebpackPlugin } = require('clean-webpack-plugin'); // 构建前清空构建目录
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 生成 html,并进行压缩
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin') //提取外部资源
const setMPA = () => {
const entry = {};
const htmlWebpackPlugins = [];
// 找到符合条件的路径
const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
// console.log('entryFiles', entryFiles);
//entryFiles [ '/Users/zuoqi/Desktop/Codes/postcss-test-project/src/index/index.js', '/Users/zuoqi/Desktop/Codes/postcss-test-project/src/search/index.js' ]
entryFiles.forEach(entryFile => {
const match = entryFile.match(/src\/(.*)\/index\.js/);
const pageName = match && match[1];
entry[pageName] = entryFile;
htmlWebpackPlugins.push(
new HtmlWebpackPlugin({
template: path.join(__dirname, `src/${pageName}/index.html`),
filename: `${pageName}.html`,
chunks: [pageName],
minify: {
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
removeComments: true,
}
})
)
})
return {
entry,
htmlWebpackPlugins,
}
}
const { entry, htmlWebpackPlugins } = setMPA();
module.exports = {
entry,
output: {
filename: '[name]_[chunkhash:8].js',
path: path.resolve(__dirname, './dist'),
},
externals: {
'@txdfe/at': 'var AT',
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
},
{
test: /\.(css|scss)$/,
use: [
MiniCssExtractPlugin.loader, //使用 MiniCssExtractPlugin 就不用 style-loader 了,作用冲突
'css-loader',
'resolve-url-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
sourceMap: true, // 结合resolve-url-loader使用必填,详见https://www.npmjs.com/package/resolve-url-loader
prependData: '@import "~@txdfe/at-theme/teambition/scss/index.scss";\n',
},
},
]
},
{
test: /\.(png|jpg|gif|jpeg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 5120,
name: '[name]_[hash:8][ext]'
}
}
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 5120,
name: '[name]_[hash:8][ext]'
}
}
]
}
]
},
optimization: {
splitChunks: {
minSize: 0,
cacheGroups: {
commons: {
name: 'commons',
chunks: 'all',
minChunks: 2,
}
}
}
},
plugins: [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[name]_[contenthash].css',
chunkFilename: '[id].css',
}),
new OptimizeCssAssetsPlugin(),
].concat(htmlWebpackPlugins).concat([
new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'react',
entry: '//cdn.jsdelivr.net/npm/[email protected]/umd/react.production.min.js',
global: 'React',
},
{
module: 'react-dom',
entry: '//cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.production.min.js',
global: 'ReactDOM',
},
{
module: '@txdfe/at',
entry: '//cdn.jsdelivr.net/npm/@txdfe/[email protected]/build/at.min.js',
global: 'AT',
},
],
})
]),
mode: 'production',
};