Skip to content

Commit 473aa6b

Browse files
authored
refactor(site): webpack configuration (#870)
This refactor introduces the following changes: * re-order and re-comment items to match v1 * add EnvironmentPlugin for environment variables * remove target (unnecessary, we will use browserslist)
1 parent 63b4006 commit 473aa6b

File tree

1 file changed

+71
-29
lines changed

1 file changed

+71
-29
lines changed

site/webpack.common.ts

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,72 @@
33
* be shared between development and production.
44
*/
55

6-
import * as path from "path"
7-
import { Configuration } from "webpack"
86
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 || ""}'`)
919

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+
*/
1028
const templatePath = path.join(__dirname, "html_templates")
1129

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+
})
2038

2139
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+
},
2662

2763
// modules specify how different modules are loaded
2864
// See: https://webpack.js.org/concepts/modules/
2965
module: {
3066
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.
3172
{
3273
test: /\.tsx?$/,
3374
use: [
@@ -38,28 +79,29 @@ export const commonWebpackConfig: Configuration = {
3879
},
3980
},
4081
],
41-
exclude: [/node_modules/],
82+
exclude: /node_modules/,
83+
},
84+
85+
// REMARK: webpack 5 asset modules
86+
{
87+
test: /\.(png|svg|jpg|jpeg|gif)$/i,
88+
type: "asset/resource",
4289
},
4390
],
4491
},
4592

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).
4697
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+
//
48101
// See: https://webpack.js.org/guides/typescript/
49102
extensions: [".tsx", ".ts", ".js"],
50103
},
51104

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],
65107
}

0 commit comments

Comments
 (0)