-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebpack.config.js
More file actions
96 lines (93 loc) · 2.44 KB
/
Copy pathwebpack.config.js
File metadata and controls
96 lines (93 loc) · 2.44 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
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 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([ 'src/frontend/page/*/index.tsx' ])
.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].[contenthash:8].js',
clean: !devMode,
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
],
},
{
test: /\.less/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'less-loader',
],
},
{
test: /\.(png|svg|jpe?g|bmp|gif|ttf|woff)$/,
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 1024 * 2,
},
},
generator: {
filename: 'lib/[name].[contenthash:8][ext]',
},
},
],
},
plugins: [
...Object.keys(entry).map(chunk => {
const main = entry[chunk];
return new HtmlWebpackPlugin({
filename: `${chunk}.html`,
template: main.replace(/\.[jt]sx?$/, '.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].[contenthash:8].css',
}),
process.stdout.isTTY && new webpack.ProgressPlugin(),
new CopyWebpackPlugin({
patterns: [ { from: 'src/frontend/asset' } ],
}),
].filter(Boolean),
};