From 4f7e56fed99969730cb4e7e79cc016fffcb54f75 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 27 Apr 2023 19:00:01 +0100 Subject: [PATCH] Making build script into a module, so that it can be used in a specialised server build which also builds the thread scripts as required for worker-farm. --- packages/server/package.json | 2 +- packages/server/scripts/build.js | 30 +++++++ packages/server/src/api/routes/static.ts | 4 +- .../src/utilities/fileSystem/clientLibrary.ts | 9 +- scripts/build.js | 88 ++++++++++--------- 5 files changed, 88 insertions(+), 45 deletions(-) create mode 100755 packages/server/scripts/build.js diff --git a/packages/server/package.json b/packages/server/package.json index d07e257c19..70041dde6f 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -10,7 +10,7 @@ }, "scripts": { "prebuild": "rimraf dist/", - "build": "../../scripts/build.js", + "build": "./scripts/build.js", "tsbuild": "tsc -p tsconfig.build.json && mv dist/src/* dist/ && rimraf dist/src/", "build:dev": "yarn prebuild && tsc --build --watch --preserveWatchOutput", "debug": "yarn build && node --expose-gc --inspect=9222 dist/index.js", diff --git a/packages/server/scripts/build.js b/packages/server/scripts/build.js new file mode 100755 index 0000000000..2bd765ae16 --- /dev/null +++ b/packages/server/scripts/build.js @@ -0,0 +1,30 @@ +#!/usr/bin/node +const { join } = require("path") +const fs = require("fs") +const coreBuild = require("../../../scripts/build") + +const dir = join(__dirname, "..") +const entryPath = join(dir, "src") +const outfilePath = join(dir, "dist") + +const ignoredFiles = ["definitions", "index", "utils"] +const threadNames = fs + .readdirSync(join(dir, "src", "threads")) + .filter(path => !ignoredFiles.find(file => path.includes(file))) + .map(path => path.replace(".ts", "")) +const files = [ + { + entry: join(entryPath, "index.ts"), + out: join(outfilePath, "index.js"), + }, +] +for (let name of threadNames) { + files.push({ + entry: join(entryPath, "threads", `${name}.ts`), + out: join(outfilePath, `${name}.js`), + }) +} + +for (let file of files) { + coreBuild(file.entry, file.out) +} diff --git a/packages/server/src/api/routes/static.ts b/packages/server/src/api/routes/static.ts index 58b2da11da..0012764b40 100644 --- a/packages/server/src/api/routes/static.ts +++ b/packages/server/src/api/routes/static.ts @@ -5,6 +5,7 @@ import authorized from "../../middleware/authorized" import { permissions } from "@budibase/backend-core" import env from "../../environment" import { paramResource } from "../../middleware/resourceId" +import { devClientLibPath } from "../../utilities/fileSystem" const { BUILDER, PermissionType, PermissionLevel } = permissions const router: Router = new Router() @@ -17,7 +18,8 @@ router.param("file", async (file: any, ctx: any, next: any) => { } // test serves from require if (env.isTest()) { - ctx.devPath = require.resolve("@budibase/client").split(ctx.file)[0] + const path = devClientLibPath() + ctx.devPath = path.split(ctx.file)[0] } else if (env.isDev()) { // Serving the client library from your local dir in dev ctx.devPath = budibaseTempDir() diff --git a/packages/server/src/utilities/fileSystem/clientLibrary.ts b/packages/server/src/utilities/fileSystem/clientLibrary.ts index da4decb3e3..c6b0a69481 100644 --- a/packages/server/src/utilities/fileSystem/clientLibrary.ts +++ b/packages/server/src/utilities/fileSystem/clientLibrary.ts @@ -6,6 +6,10 @@ import { resolve } from "../centralPath" import env from "../../environment" import { TOP_LEVEL_PATH } from "./filesystem" +export function devClientLibPath() { + return require.resolve("@budibase/client") +} + /** * Client library paths in the object store: * Previously, the entire client library package was downloaded from NPM @@ -89,9 +93,10 @@ export async function updateClientLibrary(appId: string) { let manifest, client if (env.isDev()) { + const path = devClientLibPath() // Load the symlinked version in dev which is always the newest - manifest = require.resolve("@budibase/client/manifest.json") - client = require.resolve("@budibase/client") + manifest = join(path, "manifest.json") + client = path } else { // Load the bundled version in prod manifest = resolve(TOP_LEVEL_PATH, "client", "manifest.json") diff --git a/scripts/build.js b/scripts/build.js index 7c3afe965b..0e813ab3f8 100755 --- a/scripts/build.js +++ b/scripts/build.js @@ -4,53 +4,59 @@ const start = Date.now() const glob = require("glob") const fs = require("fs") -var path = require("path") +const path = require("path") const { build } = require("esbuild") const sveltePlugin = require("esbuild-svelte") const { default: NodeResolve } = require("@esbuild-plugins/node-resolve") -const entry = process.argv[2] || "./src/index.ts" -const outfile = `dist/${entry.split("/").pop().replace(".ts", ".js")}` -console.log(`Building from ${entry} to ${outfile}`) - -const sharedConfig = { - entryPoints: [entry], - bundle: true, - minify: true, - tsconfig: `tsconfig.build.json`, - plugins: [ - sveltePlugin(), - NodeResolve({ - extensions: [".ts", ".js"], - onResolved: resolved => { - if (resolved.includes("node_modules")) { - return { - external: true, +function runBuild(entry, outfile) { + const sharedConfig = { + entryPoints: [entry], + bundle: true, + minify: true, + tsconfig: `tsconfig.build.json`, + plugins: [ + sveltePlugin(), + NodeResolve({ + extensions: [".ts", ".js"], + onResolved: resolved => { + if (resolved.includes("node_modules")) { + return { + external: true, + } } - } - return resolved - }, - }), - ], - target: "node14", - preserveSymlinks: true, + return resolved + }, + }), + ], + target: "node14", + preserveSymlinks: true, + } + + build({ + ...sharedConfig, + platform: "node", + outfile, + }).then(() => { + glob(`${process.cwd()}/src/**/*.hbs`, {}, (err, files) => { + for (const file of files) { + fs.copyFileSync(file, `${process.cwd()}/dist/${path.basename(file)}`) + } + + console.log( + "\x1b[32m%s\x1b[0m", + `Build successfully in ${(Date.now() - start) / 1000} seconds` + ) + }) + }) } -build({ - ...sharedConfig, - platform: "node", - outfile, -}).then(() => { - glob(`${process.cwd()}/src/**/*.hbs`, {}, (err, files) => { - for (const file of files) { - fs.copyFileSync(file, `${process.cwd()}/dist/${path.basename(file)}`) - } - - console.log( - "\x1b[32m%s\x1b[0m", - `Build successfully in ${(Date.now() - start) / 1000} seconds` - ) - }) -}) +if (require.main === module) { + const entry = process.argv[2] || "./src/index.ts" + const outfile = `dist/${entry.split("/").pop().replace(".ts", ".js")}` + runBuild(entry, outfile) +} else { + module.exports = runBuild +}