-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathwebpack.config.js
311 lines (297 loc) · 8.74 KB
/
webpack.config.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Copyright 2022 Harness Inc. All rights reserved.
* Use of this source code is governed by the PolyForm Shield 1.0.0 license
* that can be found in the licenses directory at the root of this repository, also available at
* https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
*/
/* eslint-disable @typescript-eslint/no-var-requires, no-console */
const buildVersion = JSON.stringify(require('./package.json').version)
const webpack = require('webpack')
const path = require('path')
const fs = require('fs')
const devServerProxyConfig = require('./webpack.devServerProxy.config')
const {
container: { ModuleFederationPlugin }
} = webpack
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
const CircularDependencyPlugin = require('circular-dependency-plugin')
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const JSONGeneratorPlugin = require('@harness/jarvis/lib/webpack/json-generator-plugin').default
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const GenerateStringTypesPlugin = require('./scripts/webpack/GenerateStringTypesPlugin').GenerateStringTypesPlugin
const { BugsnagSourceMapUploaderPlugin } = require('webpack-bugsnag-plugins')
const moduleFederationConfig = require('./configs/modulefederation.config.js')
const ExternalRemotesPlugin = require('external-remotes-plugin')
const DEV = process.env.NODE_ENV === 'development'
// this BUGSNAG_TOKEN needs to be same which is passed in the docker file
const BUGSNAG_TOKEN = process.env.BUGSNAG_TOKEN
const BUGSNAG_SOURCEMAPS_UPLOAD = `${process.env.BUGSNAG_SOURCEMAPS_UPLOAD}` === 'true'
const CONTEXT = process.cwd()
const isCypressCoverage = process.env.CYPRESS_COVERAGE
const isCypress = process.env.CYPRESS
const babelLoaderConfig = {
loader: 'babel-loader'
}
const tsLoaderConfig = {
loader: 'ts-loader',
options: {
transpileOnly: true
}
}
const tsLoaders = []
if (isCypress && isCypressCoverage) {
tsLoaders.push(babelLoaderConfig)
tsLoaders.push(tsLoaderConfig)
} else {
tsLoaders.push(tsLoaderConfig)
}
/**
* section for microfrontends
*/
const ChildAppError = path.resolve(CONTEXT, './src/microfrontends/ChildAppError.tsx')
const enableGitOpsUI = process.env.ENABLE_GITOPSUI === 'true'
const moduleFederationEnabled = true
const certificateExists = fs.existsSync('./certificates/localhost.pem')
if (DEV && !certificateExists) {
throw new Error('The certificate is missing, please run `yarn generate-certificate`')
}
const config = {
context: CONTEXT,
entry: './src/framework/app',
target: 'web',
mode: DEV ? 'development' : 'production',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: DEV ? '/' : '',
filename: DEV ? 'static/[name].js' : 'static/[name].[contenthash:6].js',
chunkFilename: DEV ? 'static/[name].[id].js' : 'static/[name].[id].[contenthash:6].js',
pathinfo: false
},
devtool: DEV ? 'cheap-module-source-map' : 'hidden-source-map',
devServer: DEV
? {
historyApiFallback: true,
port: 8181,
https: {
key: fs.readFileSync(path.resolve(__dirname, './certificates/localhost-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, './certificates/localhost.pem'))
},
proxy: Object.fromEntries(
Object.entries(devServerProxyConfig).map(([key, value]) => [
key,
Object.assign({ logLevel: 'info', secure: false, changeOrigin: true }, value)
])
)
}
: undefined,
stats: {
modules: false,
children: false
},
cache: DEV ? { type: 'filesystem' } : false,
module: {
rules: [
{
test: /\.m?js$/,
include: /node_modules/,
type: 'javascript/auto'
},
{
test: /\.(j|t)sx?$/,
exclude: /node_modules/,
use: tsLoaders
},
{
test: /\.module\.scss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{
loader: '@harness/css-types-loader',
options: {
prettierConfig: CONTEXT
}
},
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: {
mode: 'local',
localIdentName: DEV ? '[name]_[local]_[hash:base64:6]' : '[hash:base64:6]',
exportLocalsConvention: 'camelCaseOnly'
}
}
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: [path.join(CONTEXT, 'src')]
},
sourceMap: false,
implementation: require('sass')
}
}
]
},
{
test: /(?<!\.module)\.scss$/,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: false
}
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: [path.join(CONTEXT, 'src')]
},
implementation: require('sass')
}
}
]
},
{
test: /\.(jpg|jpeg|png|svg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 2000,
fallback: 'file-loader'
}
}
]
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},
{
test: /\.ttf$/,
loader: 'file-loader'
},
{
test: /\.ya?ml$/,
type: 'json',
use: [
{
loader: 'yaml-loader'
}
]
},
{
test: /\.gql$/,
type: 'asset/source'
},
{
test: /\.(mp4)$/,
use: [
{
loader: 'file-loader'
}
]
}
]
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.tsx', '.json', '.ttf'],
plugins: [new TsconfigPathsPlugin()],
alias: {
'@wings-software': '@harness'
}
},
optimization: {
splitChunks: {
chunks: 'all'
}
}
}
const commonPlugins = [
new MiniCssExtractPlugin({
filename: DEV ? 'static/[name].css' : 'static/[name].[contenthash:6].css',
chunkFilename: DEV ? 'static/[name].[id].css' : 'static/[name].[id].[contenthash:6].css'
}),
new HTMLWebpackPlugin({
template: 'src/index.html',
filename: 'index.html',
minify: false,
templateParameters: {
__DEV__: DEV
}
}),
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
new webpack.DefinePlugin({
'process.env': '{}', // required for @blueprintjs/core
__DEV__: DEV,
__BUGSNAG_RELEASE_VERSION__: buildVersion
}),
new MonacoWebpackPlugin({
// available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
languages: ['json', 'yaml', 'shell', 'powershell']
}),
new GenerateStringTypesPlugin()
]
if (moduleFederationEnabled) {
commonPlugins.unshift(new ExternalRemotesPlugin())
commonPlugins.unshift(new ModuleFederationPlugin(moduleFederationConfig({ enableGitOpsUI })))
}
if (!enableGitOpsUI) {
// render a mock app when MF is disabled
config.resolve.alias['gitopsui/MicroFrontendApp'] = ChildAppError
}
const devOnlyPlugins = [
new webpack.WatchIgnorePlugin({
paths: [/node_modules(?!\/@harness)/, /\.d\.ts$/, /stringTypes\.ts/]
}),
new ForkTsCheckerWebpackPlugin()
// new BundleAnalyzerPlugin()
]
const prodOnlyPlugins = [
new JSONGeneratorPlugin({
content: {
version: require('./package.json').version,
gitCommit: process.env.GIT_COMMIT,
gitBranch: process.env.GIT_BRANCH
},
filename: 'static/version.json'
}),
new CircularDependencyPlugin({
exclude: /node_modules/,
failOnError: true
}),
new HTMLWebpackPlugin({
template: 'src/versions.html',
filename: 'static/versions.html',
minify: false,
inject: false
})
]
if (BUGSNAG_SOURCEMAPS_UPLOAD && BUGSNAG_TOKEN) {
prodOnlyPlugins.push(
new BugsnagSourceMapUploaderPlugin({
apiKey: BUGSNAG_TOKEN,
appVersion: require('./package.json').version,
publicPath: '*',
overwrite: true
})
)
}
config.plugins = commonPlugins.concat(DEV ? devOnlyPlugins : prodOnlyPlugins)
console.table({
DEV,
FsEvents: process.env.TSC_WATCHFILE === 'UseFsEvents',
BUGSNAG_SOURCEMAPS_UPLOAD,
BugsnagTokenPresent: !!BUGSNAG_TOKEN
})
module.exports = config