-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebpack.config.js
More file actions
107 lines (104 loc) · 2.74 KB
/
webpack.config.js
File metadata and controls
107 lines (104 loc) · 2.74 KB
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
const path = require('path');
const webpack = require('webpack');
const glob = require('glob-all');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const mode = process.env.NODE_ENV === 'production' ? 'production' : 'development';
const devMode = mode === 'development';
const outputPath = path.join(__dirname, 'lib/public'); // 输出目录
const publicPath = '/';
const entry = glob.sync([ 'lib/frontend/page/*/index.js' ])
.reduce((obj, item) => {
const chunk = item.split(/[\/\\]/)[3];
obj[chunk] = path.join(__dirname, item);
return obj;
}, {});
module.exports = {
mode,
devtool: devMode ? 'source-map' : false,
entry,
output: {
path: outputPath,
publicPath,
filename: devMode ? 'lib/[name].js' : 'lib/[name].[hash:8].js',
},
resolve: {
extensions: [ '.ts', '.tsx', '.js' ],
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// hmr: devMode,
},
},
'css-loader',
],
},
{
test: /\.less/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// hmr: devMode,
},
},
'css-loader',
'less-loader',
],
},
{
test: /\.(png|svg|jpe?g|bmp|gif|ttf|woff)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 1024 * 2,
fallback: 'file-loader',
name: 'lib/[name].[hash:8].[ext]',
},
},
],
},
],
},
plugins: [
!devMode && new CleanWebpackPlugin(),
...Object.keys(entry).map(chunk => {
const main = entry[chunk];
return new HtmlWebpackPlugin({
filename: `${chunk}.html`,
template: main.replace(/\.(ts|js)$/, '.html'),
chunks: [ chunk ],
minify: devMode ? false : {
minifyJS: true,
minifyCSS: true,
collapseWhitespace: true,
preserveLineBreaks: true,
},
});
}),
new webpack.EnvironmentPlugin({
NODE_ENV: mode,
DEBUG: devMode,
}),
new MiniCssExtractPlugin({
filename: devMode ? 'lib/[name].css' : 'lib/[name].[hash:8].css',
}),
new webpack.ProgressPlugin(),
new CopyWebpackPlugin([ 'lib/frontend/asset' ]),
// new webpack.HotModuleReplacementPlugin(),
].filter(item => item),
};