Skip to content

Build your personal knowledge base with Trilium Notes

refactor(dx/server): simplify build script even further

Elian Doran72a256eccf3d
diff --git a/apps/server/scripts/build.ts b/apps/server/scripts/build.ts
index b029697225..c4a17941d5 100644
--- a/apps/server/scripts/build.ts
+++ b/apps/server/scripts/build.ts
@@ -1,76 +1,21 @@
-import * as esbuild from "esbuild";
-import { join } from "path";
-import * as child_process from "child_process";
import BuildHelper from "../../../scripts/build-utils";
-const projectDir = __dirname + "/..";
-const outDir = join(projectDir, "dist");
const build = new BuildHelper("apps/server");
-async function runBuild() {
- esbuild.build({
- entryPoints: [
- join(projectDir, "src/main.ts"),
- join(projectDir, "src/docker_healthcheck.ts")
- ],
- tsconfig: join(projectDir, "tsconfig.app.json"),
- platform: "node",
- bundle: true,
- outdir: outDir,
- outExtension: {
- ".js": ".cjs"
- },
- format: "cjs",
- external: [
- "electron",
- "@electron/remote",
- "better-sqlite3",
- "./xhr-sync-worker.js",
- "vite"
- ],
- splitting: false,
- loader: {
- ".css": "text",
- ".ejs": "text"
- },
- define: {
- "process.env.NODE_ENV": JSON.stringify("production"),
- },
- minify: true
- });
-}
+async function main() {
+ build.buildBackend([ "src/main.ts", "src/docker_healthcheck.ts" ])
-function copyAssets() {
- // Copy server assets
+ // Copy assets
build.copy("src/assets", "assets/");
-
- // Copy node modules
- for (const module of [ "better-sqlite3", "bindings", "file-uri-to-path" ]) {
- build.copy(`node_modules/${module}`, `node_modules/${module}/`);
- }
-
- // Copy sync worker.
- build.copy("node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", "xhr-sync-worker.js");
-
- // Copy share templates.
build.copy("../../packages/share-theme/src/templates", "share-theme/templates/");
-}
-
-function buildAndCopyClient() {
- // Trigger the build.
- child_process.execSync("pnpm build", { cwd: join(projectDir, "../client"), stdio: "inherit" });
- // Copy the artifacts.
- build.copy("../client/dist", "public/");
+ // Copy node modules dependencies
+ build.copyNodeModules([ "better-sqlite3", "bindings", "file-uri-to-path" ]);
+ build.copy("node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", "xhr-sync-worker.js");
- // Remove unnecessary files.
+ // Integrate the client.
+ build.triggerBuildAndCopyTo("apps/client", "public/");
build.deleteFromOutput("public/webpack-stats.json");
}
-async function main() {
- await runBuild();
- copyAssets();
- buildAndCopyClient();
-}
-
main();
diff --git a/scripts/build-utils.ts b/scripts/build-utils.ts
index 27790488b7..5857ffce4c 100644
--- a/scripts/build-utils.ts
+++ b/scripts/build-utils.ts
@@ -1,14 +1,18 @@
+import { execSync } from "child_process";
+import { build as esbuild } from "esbuild";
import { rmSync } from "fs";
import { copySync, emptyDirSync, mkdirpSync } from "fs-extra";
import { join } from "path";
export default class BuildHelper {
+ private rootDir: string;
private projectDir: string;
private outDir: string;
constructor(projectPath: string) {
- this.projectDir = join(__dirname, "..", projectPath);
+ this.rootDir = join(__dirname, "..");
+ this.projectDir = join(this.rootDir, projectPath);
this.outDir = join(this.projectDir, "dist");
emptyDirSync(this.outDir);
@@ -25,4 +29,46 @@ export default class BuildHelper {
rmSync(join(this.outDir, path), { recursive: true });
}
+ async buildBackend(entryPoints: string[]) {
+ await esbuild({
+ entryPoints: entryPoints.map(e => join(this.projectDir, e)),
+ tsconfig: join(this.projectDir, "tsconfig.app.json"),
+ platform: "node",
+ bundle: true,
+ outdir: this.outDir,
+ outExtension: {
+ ".js": ".cjs"
+ },
+ format: "cjs",
+ external: [
+ "electron",
+ "@electron/remote",
+ "better-sqlite3",
+ "./xhr-sync-worker.js",
+ "vite"
+ ],
+ splitting: false,
+ loader: {
+ ".css": "text",
+ ".ejs": "text"
+ },
+ define: {
+ "process.env.NODE_ENV": JSON.stringify("production"),
+ },
+ minify: true
+ });
+ }
+
+ triggerBuildAndCopyTo(projectToBuild: string, destPath: string) {
+ const projectDir = join(this.rootDir, projectToBuild);
+ execSync("pnpm build", { cwd: projectDir, stdio: "inherit" });
+ copySync(join(projectDir, "dist"), join(this.projectDir, "dist", destPath));
+ }
+
+ copyNodeModules(nodeModules: string[]) {
+ for (const module of nodeModules) {
+ this.copy(`node_modules/${module}`, `node_modules/${module}/`);
+ }
+ }
+
}