forked from skupperproject/skupper-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.mjs
More file actions
140 lines (133 loc) · 3.77 KB
/
webpack.config.mjs
File metadata and controls
140 lines (133 loc) · 3.77 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
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
import path from 'path';
import fs from 'fs';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import TerserJSPlugin from 'terser-webpack-plugin';
import webpack from 'webpack';
import CircularDependencyPlugin from 'circular-dependency-plugin';
const ROOT = process.cwd();
/** Helper for defining environment variables */
const defineEnvVariables = () =>
new webpack.DefinePlugin({
'process.env.OBSERVER_URL': JSON.stringify(process.env.OBSERVER_URL || ''),
'process.env.BRAND_APP_LOGO': JSON.stringify(process.env.BRAND_APP_LOGO || ''),
'process.env.API_VERSION': JSON.stringify(process.env.API_VERSION || ''),
'process.env.USE_MOCK_SERVER': JSON.stringify(process.env.USE_MOCK_SERVER || ''),
'process.env.MOCK_ITEM_COUNT': JSON.stringify(process.env.MOCK_ITEM_COUNT || ''),
'process.env.MOCK_RESPONSE_DELAY': JSON.stringify(process.env.MOCK_RESPONSE_DELAY || '')
});
/** Module rules configuration */
const moduleRules = [
{
test: /\.tsx?$/,
loader: 'ts-loader',
include: [path.resolve(ROOT, 'src'), path.resolve(ROOT, 'mocks')],
options: { transpileOnly: true, happyPackMode: true }
},
{
test: /\.(svg|jpg|jpeg|png)$/i,
type: 'asset/resource',
generator: { filename: 'assets/[name].[contenthash][ext]' }
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
generator: { filename: 'fonts/[name][ext]' }
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
];
/** Optimization configuration */
const optimizationConfig = {
chunkIds: 'named',
minimize: true,
splitChunks: {
chunks: 'all',
maxInitialRequests: 6,
maxAsyncRequests: 8,
cacheGroups: {
vendors: {
test: /\/node_modules\//,
priority: -10,
enforce: true
}
}
},
runtimeChunk: 'single',
minimizer: [
new TerserJSPlugin({
test: /\.js$/,
terserOptions: {
compress: true,
mangle: true,
format: { comments: false }
},
extractComments: false
})
]
};
/** Plugins configuration */
const pluginsConfig = () => [
defineEnvVariables(),
new HtmlWebpackPlugin({
template: path.resolve(ROOT, 'index.html'),
favicon: process.env.BRAND_FAVICON || path.resolve(ROOT, 'public', 'favicon.ico'),
templateContent: (parameters, compilation, options) => {
const html = fs.readFileSync(path.resolve(ROOT, 'index.html'), 'utf8');
return html.replace(/<!-- vite only -->[\s\S]*?<\/script>/, ''); // remove the script tag used by vite in dev mode
}
}),
...(process.env.BRAND_APP_LOGO
? [
new CopyWebpackPlugin({
patterns: [
{
from: process.env.BRAND_APP_LOGO,
to: path.resolve(ROOT, 'build', path.basename(process.env.BRAND_APP_LOGO))
}
]
})
]
: []),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: 'css/[name].[chunkhash].min.css',
ignoreOrder: true
}),
new CircularDependencyPlugin({
include: /src/,
exclude: /node_modules/,
failOnError: true
})
];
/** Webpack configuration object */
const config = {
mode: 'production',
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: moduleRules
},
plugins: pluginsConfig(),
output: {
path: path.join(ROOT, '/build'),
filename: '[name].[contenthash].min.js',
chunkFilename: 'js/[name].[chunkhash].min.js',
publicPath: '/',
clean: true
},
optimization: optimizationConfig,
performance: {
maxEntrypointSize: 614400,
maxAssetSize: 1048576
},
infrastructureLogging: {
debug: true
}
};
export default config;