Skip to content

Commit 3d69c98

Browse files
authored
Use a webpack plugin to generate Adventure Pack index.html (#487)
This allows the file to be written when we run webpack in watch mode.
1 parent 2585a38 commit 3d69c98

File tree

5 files changed

+64
-63
lines changed

5 files changed

+64
-63
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from "react";
2+
import ReactDOMServer from "react-dom/server";
3+
import type { Compiler } from "webpack";
4+
5+
import { App } from "../../app/components/App.tsx";
6+
7+
export class WriteIndexHtmlWebpackPlugin {
8+
constructor(private commitHash: string) {}
9+
10+
apply(compiler: Compiler) {
11+
compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
12+
compilation.hooks.processAssets.tap(
13+
{
14+
name: this.constructor.name,
15+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
16+
},
17+
() => {
18+
compilation.emitAsset(
19+
"index.html",
20+
new compiler.webpack.sources.RawSource(
21+
"<!DOCTYPE html>\n" +
22+
ReactDOMServer.renderToStaticMarkup(
23+
<html lang="en-US">
24+
<head>
25+
<meta charSet="utf-8" />
26+
<title>Adventure Pack</title>
27+
<link
28+
rel="preconnect"
29+
href="https://fonts.googleapis.com"
30+
crossOrigin=""
31+
/>
32+
<link
33+
rel="preconnect"
34+
href="https://fonts.gstatic.com"
35+
crossOrigin=""
36+
/>
37+
<link
38+
href="https://fonts.googleapis.com/css2?family=Courgette&display=swap"
39+
rel="stylesheet"
40+
/>
41+
<script async src="main.js" />
42+
<script async src="dependencies.js" />
43+
<link href="style.css" rel="stylesheet" />
44+
</head>
45+
<body>
46+
<div id="main">
47+
<App commitHash={this.commitHash} />
48+
</div>
49+
</body>
50+
</html>,
51+
) +
52+
"\n",
53+
),
54+
);
55+
},
56+
);
57+
});
58+
}
59+
}

workspaces/adventure-pack/src/scripts/build/main.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ import { mkdir } from "node:fs/promises";
33
import { WEB_APP_DIST } from "./constants.ts";
44
import { runWebpack } from "./runWebpack.ts";
55
import { writeGoodiesJson } from "./writeGoodiesJson.ts";
6-
import { writeIndexHtml } from "./writeIndexHtml.tsx";
76
import { writeStyleCss } from "./writeStyleCss.ts";
87

98
async function main(): Promise<void> {
109
await mkdir(WEB_APP_DIST, { recursive: true });
1110

12-
await Promise.all([
13-
runWebpack(),
14-
writeIndexHtml(),
15-
writeGoodiesJson(),
16-
writeStyleCss(),
17-
]);
11+
await Promise.all([runWebpack(), writeGoodiesJson(), writeStyleCss()]);
1812
}
1913

2014
main().catch((err) => {

workspaces/adventure-pack/src/scripts/build/writeIndexHtml.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

workspaces/adventure-pack/webpack.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DefinePlugin, type Configuration } from "webpack";
55
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
66

77
import { WEB_APP_DIST } from "./src/scripts/build/constants.ts";
8+
import { WriteIndexHtmlWebpackPlugin } from "./src/scripts/build/WriteIndexHtmlWebpackPlugin.tsx";
89

910
const commitHash = execSync("git rev-parse HEAD").toString().trim();
1011

@@ -43,6 +44,8 @@ const config: Configuration = {
4344
ADVENTURE_PACK_COMMIT_HASH: JSON.stringify(commitHash),
4445
}),
4546

47+
new WriteIndexHtmlWebpackPlugin(commitHash),
48+
4649
new ForkTsCheckerWebpackPlugin(),
4750
],
4851

workspaces/webpack-chrome-extension-manifest-plugin/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class WebpackChromeExtensionManifestPlugin {
77
constructor(private manifest: JsonObject) {}
88

99
apply(compiler: Compiler) {
10-
compiler.hooks.compilation.tap("ManifestPlugin", (compilation) => {
10+
compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
1111
compilation.hooks.processAssets.tap(
1212
{
1313
name: this.constructor.name,

0 commit comments

Comments
 (0)