mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	refactor(dx/server): simplify build script even further
This commit is contained in:
		| @@ -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"; | import BuildHelper from "../../../scripts/build-utils"; | ||||||
|  |  | ||||||
| const projectDir = __dirname + "/.."; |  | ||||||
| const outDir = join(projectDir, "dist"); |  | ||||||
| const build = new BuildHelper("apps/server"); | const build = new BuildHelper("apps/server"); | ||||||
|  |  | ||||||
| async function runBuild() { | async function main() { | ||||||
|     esbuild.build({ |     build.buildBackend([ "src/main.ts", "src/docker_healthcheck.ts" ]) | ||||||
|         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 |  | ||||||
|     }); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function copyAssets() { |     // Copy assets | ||||||
|     // Copy server assets |  | ||||||
|     build.copy("src/assets", "assets/"); |     build.copy("src/assets", "assets/"); | ||||||
|  |     build.copy("../../packages/share-theme/src/templates", "share-theme/templates/"); | ||||||
|  |  | ||||||
|     // Copy node modules |     // Copy node modules dependencies | ||||||
|     for (const module of [ "better-sqlite3", "bindings", "file-uri-to-path" ]) { |     build.copyNodeModules([ "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"); |     build.copy("node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js", "xhr-sync-worker.js"); | ||||||
|  |  | ||||||
|     // Copy share templates. |     // Integrate the client. | ||||||
|     build.copy("../../packages/share-theme/src/templates", "share-theme/templates/"); |     build.triggerBuildAndCopyTo("apps/client", "public/"); | ||||||
| } |  | ||||||
|  |  | ||||||
| function buildAndCopyClient() { |  | ||||||
|     // Trigger the build. |  | ||||||
|     child_process.execSync("pnpm build", { cwd: join(projectDir, "../client"), stdio: "inherit" }); |  | ||||||
|  |  | ||||||
|     // Copy the artifacts. |  | ||||||
|     build.copy("../client/dist", "public/"); |  | ||||||
|  |  | ||||||
|     // Remove unnecessary files. |  | ||||||
|     build.deleteFromOutput("public/webpack-stats.json"); |     build.deleteFromOutput("public/webpack-stats.json"); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function main() { |  | ||||||
|     await runBuild(); |  | ||||||
|     copyAssets(); |  | ||||||
|     buildAndCopyClient(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| main(); | main(); | ||||||
|   | |||||||
| @@ -1,14 +1,18 @@ | |||||||
|  | import { execSync } from "child_process"; | ||||||
|  | import { build as esbuild } from "esbuild"; | ||||||
| import { rmSync } from "fs"; | import { rmSync } from "fs"; | ||||||
| import { copySync, emptyDirSync, mkdirpSync } from "fs-extra"; | import { copySync, emptyDirSync, mkdirpSync } from "fs-extra"; | ||||||
| import { join } from "path"; | import { join } from "path"; | ||||||
|  |  | ||||||
| export default class BuildHelper { | export default class BuildHelper { | ||||||
|  |  | ||||||
|  |     private rootDir: string; | ||||||
|     private projectDir: string; |     private projectDir: string; | ||||||
|     private outDir: string; |     private outDir: string; | ||||||
|  |  | ||||||
|     constructor(projectPath: 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"); |         this.outDir = join(this.projectDir, "dist"); | ||||||
|  |  | ||||||
|         emptyDirSync(this.outDir); |         emptyDirSync(this.outDir); | ||||||
| @@ -25,4 +29,46 @@ export default class BuildHelper { | |||||||
|         rmSync(join(this.outDir, path), { recursive: true }); |         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}/`); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user