3
3
* be shared between development and production.
4
4
*/
5
5
6
- import * as path from "path"
7
- import { Configuration } from "webpack"
8
6
import HtmlWebpackPlugin from "html-webpack-plugin"
7
+ import * as path from "path"
8
+ import { Configuration , EnvironmentPlugin } from "webpack"
9
+
10
+ /**
11
+ * environmentPlugin sets process.env.* variables so that they're available in
12
+ * the application.
13
+ */
14
+ const environmentPlugin = new EnvironmentPlugin ( {
15
+ INSPECT_XSTATE : "" ,
16
+ } )
17
+ console . info ( `--- Setting INSPECT_XSTATE to '${ process . env . INSPECT_XSTATE || "" } '` )
18
+ console . info ( `--- Setting NODE_ENV to '${ process . env . NODE_ENV || "" } '` )
9
19
20
+ /**
21
+ * dashboardEntrypoint is the top-most module in the dashboard chunk.
22
+ */
23
+ const dashboardEntrypoint = path . join ( __dirname , "src/Main.tsx" )
24
+
25
+ /**
26
+ * templatePath is the path to HTML templates for injecting webpack bundles
27
+ */
10
28
const templatePath = path . join ( __dirname , "html_templates" )
11
29
12
- const plugins = [
13
- // The HTML webpack plugin tells webpack to use our `index.html` and inject
14
- // the bundle script, which might have special naming .
15
- new HtmlWebpackPlugin ( {
16
- template : path . join ( templatePath , "index.html" ) ,
17
- publicPath : "/" ,
18
- } ) ,
19
- ]
30
+ /**
31
+ * dashboardHTMLPluginConfig is the HtmlWebpackPlugin configuration for the
32
+ * dashboard chunk .
33
+ */
34
+ const dashboardHTMLPluginConfig = new HtmlWebpackPlugin ( {
35
+ publicPath : "/" ,
36
+ template : path . join ( templatePath , "index.html" ) ,
37
+ } )
20
38
21
39
export const commonWebpackConfig : Configuration = {
22
- // entry defines each "page" or "chunk". Currently, for v2, we only have one bundle -
23
- // a bundle that is shared across all of the UI. However, we may need to eventually split
24
- // like in v1, where there is a separate entry piont for dashboard & terminal.
25
- entry : path . join ( __dirname , "src/Main.tsx" ) ,
40
+ // entry defines each "page" or "chunk". In v1, we have two "pages":
41
+ // dashboard and terminal. This is desired because the terminal has the xterm
42
+ // vendor, and it is undesireable to load all of xterm on a dashboard
43
+ // page load.
44
+ //
45
+ // The object key determines the chunk 'name'. This can be used in `output`
46
+ // to create a final bundle name.
47
+ //
48
+ // REMARK: We may need to further vendorize the pieces shared by the chunks
49
+ // such as React, ReactDOM. This is not yet _optimized_, but having
50
+ // them split means less initial load on dashboard page load.
51
+ entry : dashboardEntrypoint ,
52
+
53
+ // output defines the name and location of the final bundle
54
+ output : {
55
+ // The chunk name along with a hash of its content will be used for the
56
+ // generated bundle name.
57
+ //
58
+ // REMARK: It's important to use [contenthash] here to invalidate caches.
59
+ filename : "bundle.[contenthash].js" ,
60
+ path : path . resolve ( __dirname , "out" ) ,
61
+ } ,
26
62
27
63
// modules specify how different modules are loaded
28
64
// See: https://webpack.js.org/concepts/modules/
29
65
module : {
30
66
rules : [
67
+ // TypeScript (ts, tsx) files use ts-loader for simplicity.
68
+ //
69
+ // REMARK: We may want to configure babel-loader later on for further
70
+ // optimization (build time, tree-shaking). babel-loader on its
71
+ // own does not run type checks.
31
72
{
32
73
test : / \. t s x ? $ / ,
33
74
use : [
@@ -38,28 +79,29 @@ export const commonWebpackConfig: Configuration = {
38
79
} ,
39
80
} ,
40
81
] ,
41
- exclude : [ / n o d e _ m o d u l e s / ] ,
82
+ exclude : / n o d e _ m o d u l e s / ,
83
+ } ,
84
+
85
+ // REMARK: webpack 5 asset modules
86
+ {
87
+ test : / \. ( p n g | s v g | j p g | j p e g | g i f ) $ / i,
88
+ type : "asset/resource" ,
42
89
} ,
43
90
] ,
44
91
} ,
45
92
93
+ // resolve extend/modify how modules are resolved.
94
+ //
95
+ // REMARK: Do not add aliases here, unless they cannot be defined in a
96
+ // tsconfig file (see TSConfigWebpackPlugin).
46
97
resolve : {
47
- // Let webpack know to consider ts/tsx files for bundling
98
+ // extensions are attempted in order and enable importing files without
99
+ // the extensions explicitly stated
100
+ //
48
101
// See: https://webpack.js.org/guides/typescript/
49
102
extensions : [ ".tsx" , ".ts" , ".js" ] ,
50
103
} ,
51
104
52
- // output defines the name and location of the final bundle
53
- output : {
54
- // The chunk name along with a hash of its content will be used for the
55
- // generated bundle name.
56
- //
57
- // REMARK: It's important to use [contenthash] here to invalidate caches.
58
- filename : "bundle.[contenthash].js" ,
59
- path : path . resolve ( __dirname , "out" ) ,
60
- } ,
61
-
62
- plugins : plugins ,
63
-
64
- target : "web" ,
105
+ // plugins customize the build process
106
+ plugins : [ environmentPlugin , dashboardHTMLPluginConfig ] ,
65
107
}
0 commit comments