Skip to content

Commit 64ce46a

Browse files
authored
Run webpack directly to build Adventure Pack web app (#494)
Instead of having a wrapper for `webpack`, we can use `webpack` plugins to run our additional code. This allows us to use other `webpack` features such as watch mode, for example.
1 parent c5d0686 commit 64ce46a

File tree

10 files changed

+174
-68
lines changed

10 files changed

+174
-68
lines changed

workspaces/adventure-pack/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"goodies:typescript:format": "prettier --color --write goodies/typescript",
4949
"goodies:typescript:install": "yarn",
5050
"goodies:typescript:test": "tsx ./jest.config.ts \"/goodies/typescript/\"",
51-
"build-app": "tsx src/scripts/build/main.ts",
51+
"build-app": "cross-env NODE_OPTIONS=\"--import tsx\" webpack",
5252
"build-chrome-extension": "tsx src/scripts/build/buildChromeExtension.ts",
5353
"package-goodies:test": "tsx ./jest.config.ts --testPathIgnorePatterns=\"<rootDir>/goodies/\"",
5454
"format": "yarn goodies:java:format && yarn goodies:kotlin:format && yarn goodies:python3:format && yarn goodies:typescript:format && prettier --color --write .",
@@ -74,6 +74,7 @@
7474
"@types/react": "18.3.12",
7575
"@types/react-dom": "18.3.1",
7676
"@types/react-syntax-highlighter": "15.5.13",
77+
"copy-webpack-plugin": "12.0.2",
7778
"cross-env": "7.0.3",
7879
"eslint": "9.14.0",
7980
"fork-ts-checker-webpack-plugin": "9.0.2",

workspaces/adventure-pack/src/app/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Language } from "./Language.ts";
22

3+
export const GOODIES_FILENAME = "goodies.json";
4+
35
export const LANGUAGE_NAMES: Record<Language, string> = {
46
java: "Java",
57
javascript: "JavaScript",

workspaces/adventure-pack/src/app/fetchGoodies.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ReadonlyDeep } from "type-fest";
22
import { z } from "zod";
33

4+
import { GOODIES_FILENAME } from "./constants.ts";
45
import { javaGoodyZodType } from "./zod-types/javaGoodyZodType.ts";
56
import { javaScriptGoodyZodType } from "./zod-types/javaScriptGoodyZodType.ts";
67
import { kotlinGoodyZodType } from "./zod-types/kotlinGoodyZodType.ts";
@@ -18,7 +19,7 @@ const goodiesByLanguage = z.object({
1819
export type GoodiesByLanguage = ReadonlyDeep<z.infer<typeof goodiesByLanguage>>;
1920

2021
export async function fetchGoodies(): Promise<GoodiesByLanguage> {
21-
const response = await fetch("goodies.json");
22+
const response = await fetch(GOODIES_FILENAME);
2223

2324
if (!response.ok) {
2425
throw new Error(`Got status ${response.status} from server!`);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Compiler } from "webpack";
2+
3+
import { jsonStringifyPrettyInDev } from "@code-chronicles/util/jsonStringifyPrettyInDev";
4+
5+
import { GOODIES_FILENAME } from "../../app/constants.ts";
6+
import { readAllGoodies } from "../package-goodies/readAllGoodies.ts";
7+
8+
export class WriteGoodiesJsonWebpackPlugin {
9+
apply(compiler: Compiler) {
10+
compiler.hooks.compilation.tap(this.constructor.name, (compilation) => {
11+
compilation.hooks.processAssets.tapPromise(
12+
{
13+
name: this.constructor.name,
14+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
15+
},
16+
async () => {
17+
const goodies = await readAllGoodies();
18+
19+
compilation.emitAsset(
20+
GOODIES_FILENAME,
21+
new compiler.webpack.sources.RawSource(
22+
jsonStringifyPrettyInDev(goodies),
23+
),
24+
);
25+
},
26+
);
27+
});
28+
}
29+
}

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

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

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

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

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

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

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

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

workspaces/adventure-pack/webpack.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { execSync } from "node:child_process";
22
import path from "node:path";
33

4-
import { DefinePlugin, type Configuration } from "webpack";
4+
import CopyPlugin from "copy-webpack-plugin";
55
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
6+
import { DefinePlugin, type Configuration } from "webpack";
67

78
import { WEB_APP_DIST } from "./src/scripts/build/constants.ts";
9+
import { WriteGoodiesJsonWebpackPlugin } from "./src/scripts/build/WriteGoodiesJsonWebpackPlugin.tsx";
810
import { WriteIndexHtmlWebpackPlugin } from "./src/scripts/build/WriteIndexHtmlWebpackPlugin.tsx";
911

1012
const commitHash = execSync("git rev-parse HEAD").toString().trim();
@@ -44,6 +46,9 @@ const config: Configuration = {
4446
ADVENTURE_PACK_COMMIT_HASH: JSON.stringify(commitHash),
4547
}),
4648

49+
// Add the other assets needed for the app.
50+
new CopyPlugin({ patterns: [{ from: "css", to: WEB_APP_DIST }] }),
51+
new WriteGoodiesJsonWebpackPlugin(),
4752
new WriteIndexHtmlWebpackPlugin(commitHash),
4853

4954
new ForkTsCheckerWebpackPlugin(),

0 commit comments

Comments
 (0)