-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathwebpack.config.ts
More file actions
156 lines (146 loc) · 4.41 KB
/
webpack.config.ts
File metadata and controls
156 lines (146 loc) · 4.41 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as path from 'path';
// eslint-disable-next-line import/default
import CopyPlugin from 'copy-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import svgToMiniDataURI from 'mini-svg-data-uri';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import {
type Configuration as WebpackConfiguration,
EnvironmentPlugin,
type WebpackPluginInstance,
} from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
import { ConsoleRemotePlugin } from '@openshift-console/dynamic-plugin-sdk-webpack';
import { ENVIRONMENT_DEFAULTS } from './environment-defaults';
import extensions from './plugin-extensions';
import pluginMetadata from './plugin-metadata';
type Configuration = {
devServer?: WebpackDevServerConfiguration;
} & WebpackConfiguration;
const isAnalyzeBundle = process.env.ANALYZE_BUNDLE === 'true';
const config: Configuration = {
context: path.resolve(__dirname, 'src'),
devServer: {
allowedHosts: 'all',
client: {
progress: true,
},
compress: true, // Enable gzip compression
devMiddleware: {
writeToDisk: true,
},
headers: {
'Access-Control-Allow-Headers': 'X-Requested-With, Content-Type, Authorization',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-store', // Jan-4-2024, workaround for a caching bug in bridge
'Service-Worker-Allowed': '/', // Needed to support MockServiceWorker
},
historyApiFallback: true, // Better SPA routing support
hot: true,
port: parseInt(process.env.PORT ?? '9001', 10),
static: ['./dist'],
},
devtool: 'source-map',
entry: {},
mode: 'development',
module: {
rules: [
{
exclude: [/node_modules/u, /__tests__/u, /__mocks__/u],
test: /\.(?:jsx?|tsx?)$/u,
use: [
{
loader: 'ts-loader',
options: {
experimentalWatchApi: true, // Faster incremental builds
transpileOnly: true, // Skip type checking for faster builds
},
},
],
},
{
test: /\.s?(?:css)$/u,
use: ['style-loader', 'css-loader', 'sass-loader'],
},
{
generator: {
dataUrl: (content: Buffer) => {
return svgToMiniDataURI(content.toString());
},
},
test: /\.svg$/u,
type: 'asset/inline',
},
{
generator: {
filename: 'assets/[name].[ext]',
},
test: /\.(?:png|jpg|jpeg|gif|woff2?|ttf|eot|otf)(?:\?.*$|$)/u,
type: 'asset/resource',
},
{
resolve: {
fullySpecified: false,
},
test: /\.m?js/u,
},
],
},
optimization: {
chunkIds: 'named',
minimize: false,
},
output: {
chunkFilename: '[name]-chunk.js',
filename: '[name]-bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new ForkTsCheckerWebpackPlugin({
async: true,
devServer: true,
typescript: {
configFile: path.resolve(__dirname, 'tsconfig.json'),
diagnosticOptions: {
semantic: true,
syntactic: true,
},
},
}),
new ConsoleRemotePlugin({
extensions,
pluginMetadata,
}) as unknown as WebpackPluginInstance,
new CopyPlugin({
patterns: [{ from: '../locales', to: '../dist/locales' }],
}),
new EnvironmentPlugin(ENVIRONMENT_DEFAULTS),
...(isAnalyzeBundle
? [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: true,
reportFilename: 'report.html',
}) as unknown as WebpackPluginInstance,
]
: []),
],
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
plugins: [new TsconfigPathsPlugin({ baseUrl: '.' })],
},
};
if (process.env.NODE_ENV === 'production') {
config.mode = 'production';
// Ensure `output` is initialized if undefined
config.output ??= {};
config.output.filename = '[name]-bundle-[hash].min.js';
config.output.chunkFilename = '[name]-chunk-[chunkhash].min.js';
// Ensure `optimization` is initialized if undefined
config.optimization ??= {};
config.optimization.chunkIds = 'deterministic';
config.optimization.minimize = true;
}
export default config;