1
- const { resolve } = require ( 'path' ) ; // node:path
2
- const config = require ( '@redhat-cloud-services/frontend-components-config ' ) ;
3
- const MiniCssExtractPlugin = require ( 'mini-css-extract -plugin' ) ;
1
+ const { resolve } = require ( 'node: path' ) ;
2
+ const { CleanWebpackPlugin } = require ( 'clean-webpack-plugin ' ) ;
3
+ const ForkTsCheckerWebpackPlugin = require ( 'fork-ts-checker-webpack -plugin' ) ;
4
4
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
5
+ const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
5
6
const MonacoWebpackPlugin = require ( 'monaco-editor-webpack-plugin' ) ;
6
- const { execSync } = require ( 'child_process' ) ; // node:child_process
7
-
8
- const isBuild = process . env . NODE_ENV === 'production' ;
7
+ const { execSync } = require ( 'node:child_process' ) ;
8
+ const webpack = require ( 'webpack' ) ;
9
9
10
10
// NOTE: This file is not meant to be consumed directly by weback. Instead it
11
11
// should be imported, initialized with the following settings and exported like
12
12
// a normal webpack config. See config/standalone.dev.webpack.config.js for an
13
13
// example
14
14
15
+ const isBuild = process . env . NODE_ENV === 'production' ;
16
+
15
17
// only run git when HUB_UI_VERSION is NOT provided
16
18
const gitCommit =
17
19
process . env . HUB_UI_VERSION ||
@@ -33,7 +35,6 @@ const defaultConfigs = [
33
35
{ name : 'UI_EXTERNAL_LOGIN_URI' , default : '/login' , scope : 'global' } ,
34
36
35
37
// Webpack scope: only available in customConfigs here, not exposed to the UI
36
- { name : 'UI_DEBUG' , default : false , scope : 'webpack' } ,
37
38
{ name : 'UI_PORT' , default : 8002 , scope : 'webpack' } ,
38
39
{ name : 'UI_USE_HTTPS' , default : false , scope : 'webpack' } ,
39
40
{ name : 'WEBPACK_PROXY' , default : undefined , scope : 'webpack' } ,
@@ -45,51 +46,52 @@ module.exports = (inputConfigs) => {
45
46
const globals = { } ;
46
47
47
48
defaultConfigs . forEach ( ( item ) => {
48
- // == will match null and undefined, but not false
49
- if ( inputConfigs [ item . name ] == null ) {
50
- customConfigs [ item . name ] = item . default ;
51
- } else {
52
- customConfigs [ item . name ] = inputConfigs [ item . name ] ;
53
- }
54
- if ( item . scope === 'global' ) {
55
- globals [ item . name ] = JSON . stringify (
56
- inputConfigs [ item . name ] || item . default ,
57
- ) ;
58
- }
49
+ customConfigs [ item . name ] = inputConfigs [ item . name ] ?? item . default ;
59
50
} ) ;
60
51
52
+ defaultConfigs
53
+ . filter ( ( { scope } ) => scope === 'global' )
54
+ . forEach ( ( item ) => {
55
+ globals [ item . name ] = JSON . stringify ( customConfigs [ item . name ] ) ;
56
+ } ) ;
57
+
61
58
// 4.6+: pulp APIs live under API_BASE_PATH now, ignore previous overrides
62
59
globals . PULP_API_BASE_PATH = JSON . stringify (
63
60
customConfigs . API_BASE_PATH + 'pulp/api/v3/' ,
64
61
) ;
65
62
66
- const { config : webpackConfig , plugins } = config ( {
67
- rootFolder : resolve ( __dirname , '../' ) ,
68
- definePlugin : globals ,
69
- debug : customConfigs . UI_DEBUG ,
70
- https : customConfigs . UI_USE_HTTPS ,
71
-
72
- // defines port for dev server
73
- port : customConfigs . UI_PORT ,
74
-
75
- // frontend-components-config 4.5.0+: don't remove patternfly from builds
76
- bundlePfModules : true ,
77
-
78
- // frontend-components-config 4.6.25-29+: ensure hashed filenames
79
- useFileHash : true ,
80
- } ) ;
81
-
82
- // Override sections of the webpack config to work with TypeScript
83
- const newWebpackConfig = {
84
- ...webpackConfig ,
85
-
86
- // override from empty
63
+ return {
87
64
devtool : 'source-map' ,
88
65
66
+ ...( isBuild
67
+ ? { }
68
+ : {
69
+ devServer : {
70
+ allowedHosts : 'all' ,
71
+ client : { overlay : false } ,
72
+ devMiddleware : { writeToDisk : true } ,
73
+ historyApiFallback : true ,
74
+ host : '0.0.0.0' ,
75
+ hot : false ,
76
+ https : customConfigs . UI_USE_HTTPS ,
77
+ liveReload : true ,
78
+ magicHtml : false ,
79
+ onListening : ( server ) =>
80
+ console . log (
81
+ 'App should run on:' ,
82
+ `${ server . options . https ? 'https' : 'http' } ://localhost:${
83
+ server . options . port
84
+ } `,
85
+ ) ,
86
+ port : customConfigs . UI_PORT ,
87
+ proxy : customConfigs . WEBPACK_PROXY ,
88
+ static : { directory : resolve ( __dirname , '../dist' ) } ,
89
+ } ,
90
+ } ) ,
91
+
92
+ entry : { App : resolve ( __dirname , '../src/entry-standalone.tsx' ) } ,
93
+ mode : isBuild ? 'production' : 'development' ,
89
94
module : {
90
- ...webpackConfig . module ,
91
-
92
- // override to drop ts-loader
93
95
rules : [
94
96
{
95
97
test : / s r c \/ .* \. ( j s | j s x | t s | t s x ) $ / ,
@@ -108,64 +110,55 @@ module.exports = (inputConfigs) => {
108
110
type : 'asset/resource' ,
109
111
generator : { filename : 'fonts/[name][ext][query]' } ,
110
112
} ,
113
+ { test : / \. m j s $ / , include : / n o d e _ m o d u l e s / , type : 'javascript/auto' } ,
111
114
] ,
112
115
} ,
113
-
116
+ output : {
117
+ filename : 'js/[name].[fullhash].js' ,
118
+ path : resolve ( __dirname , '../dist' ) ,
119
+ publicPath : customConfigs . WEBPACK_PUBLIC_PATH ?? '/' ,
120
+ chunkFilename : 'js/[name].[fullhash].js' ,
121
+ } ,
122
+ plugins : [
123
+ // sourcemaps
124
+ new webpack . SourceMapDevToolPlugin ( {
125
+ exclude : / n o d e _ m o d u l e s / ,
126
+ filename : 'sourcemaps/[name].[contenthash].js.map' ,
127
+ } ) ,
128
+ // extract css in prod
129
+ isBuild &&
130
+ new MiniCssExtractPlugin ( {
131
+ filename : 'css/[name].[contenthash].css' ,
132
+ } ) ,
133
+ // clean dist/
134
+ new CleanWebpackPlugin ( {
135
+ cleanOnceBeforeBuildPatterns : [ '**/*' , '!index.html' ] ,
136
+ } ) ,
137
+ // define global vars
138
+ new webpack . DefinePlugin ( globals ) ,
139
+ // typescript check
140
+ new ForkTsCheckerWebpackPlugin ( ) ,
141
+ // inject src/index.html
142
+ new HtmlWebpackPlugin ( {
143
+ applicationName : customConfigs . APPLICATION_NAME ,
144
+ favicon : 'static/images/favicon.ico' ,
145
+ template : resolve ( __dirname , '../src/index.html' ) ,
146
+ } ) ,
147
+ // @patternfly /react-code-editor
148
+ new MonacoWebpackPlugin ( {
149
+ languages : [ 'yaml' ] ,
150
+ } ) ,
151
+ ] . filter ( Boolean ) ,
114
152
resolve : {
115
- ...webpackConfig . resolve ,
116
-
117
- // override to support jsx, drop scss
118
- extensions : [ '.ts' , '.tsx' , '.js' , '.jsx' ] ,
119
-
153
+ extensions : [ '.js' , '.jsx' , '.ts' , '.tsx' ] ,
120
154
alias : {
121
- ...webpackConfig . resolve . alias ,
122
-
123
155
// imports relative to repo root
124
156
src : resolve ( __dirname , '../src' ) ,
125
157
} ,
126
158
} ,
127
-
128
- // ignore editor files when watching
129
159
watchOptions : {
160
+ // ignore editor files when watching
130
161
ignored : [ '**/.*.sw[po]' ] ,
131
162
} ,
132
163
} ;
133
-
134
- if ( customConfigs . WEBPACK_PROXY ) {
135
- newWebpackConfig . devServer . proxy = customConfigs . WEBPACK_PROXY ;
136
- }
137
-
138
- if ( customConfigs . WEBPACK_PUBLIC_PATH ) {
139
- console . log ( `New output.publicPath: ${ customConfigs . WEBPACK_PUBLIC_PATH } ` ) ;
140
- newWebpackConfig . output . publicPath = customConfigs . WEBPACK_PUBLIC_PATH ;
141
- }
142
-
143
- console . log ( 'Overriding configs for standalone mode.' ) ;
144
-
145
- const newEntry = resolve ( __dirname , '../src/entry-standalone.tsx' ) ;
146
- console . log ( `New entry.App: ${ newEntry } ` ) ;
147
- newWebpackConfig . entry . App = newEntry ;
148
-
149
- // ForkTsCheckerWebpackPlugin is part of default config since @redhat -cloud-services/frontend-components-config 4.6.24
150
-
151
- // keep HtmlWebpackPlugin for standalone, inject src/index.html
152
- plugins . push (
153
- new HtmlWebpackPlugin ( {
154
- applicationName : customConfigs . APPLICATION_NAME ,
155
- favicon : 'static/images/favicon.ico' ,
156
- template : resolve ( __dirname , '../src/index.html' ) ,
157
- } ) ,
158
- ) ;
159
-
160
- // @patternfly /react-code-editor
161
- plugins . push (
162
- new MonacoWebpackPlugin ( {
163
- languages : [ 'yaml' ] ,
164
- } ) ,
165
- ) ;
166
-
167
- return {
168
- ...newWebpackConfig ,
169
- plugins,
170
- } ;
171
164
} ;
0 commit comments