Files
2026-08-20 09:18:20 +02:00

147 lines
3.9 KiB
JavaScript
Executable File

#!/usr/bin/node
const start = Date.now()
const fs = require("fs")
const { readdir, copyFile, mkdir } = require("node:fs/promises")
const path = require("path")
const { build } = require("esbuild")
const { compile, preprocess } = require("svelte/compiler")
const sveltePreprocess = require("svelte-preprocess")
const { loadTsConfig } = require("load-tsconfig")
const {
default: TsconfigPathsPlugin,
} = require("@esbuild-plugins/tsconfig-paths")
const { nodeExternalsPlugin } = require("esbuild-node-externals")
const svelteCompilePlugin = {
name: "svelteCompile",
setup(build) {
// Compiles `.svelte` files into JS classes so that they can be directly imported into our
// Typescript packages
build.onLoad({ filter: /\.svelte$/ }, async args => {
const source = await fs.promises.readFile(args.path, "utf8")
const dir = path.dirname(args.path)
try {
const preprocessed = await preprocess(
source,
sveltePreprocess({ filename: args.path })
)
const { js } = compile(preprocessed.code, {
css: "injected",
generate: "server",
})
return {
// The code placed in the generated file
contents: js.code,
// The loader this is passed to, basically how the above provided content is "treated",
// the contents provided above will be transpiled and bundled like any other JS file.
loader: "js",
// Where to resolve any imports present in the loaded file
resolveDir: dir,
}
} catch (e) {
return {
errors: [
{
text: e && e.message ? e.message : String(e),
detail: e,
},
],
}
}
})
},
}
let { argv } = require("yargs")
async function runBuild(entry, outfile, opts = {}) {
const isDev = !process.env.CI
console.log(`Building in mode dev mode: ${isDev}`)
const tsconfig = argv["p"] || `tsconfig.build.json`
const { data: tsconfigPathPluginContent } = loadTsConfig(
process.cwd(),
tsconfig
)
tsconfigPathPluginContent.compilerOptions.baseUrl = path.resolve(__dirname, "..")
const external = Array.isArray(opts.external) ? opts.external : []
const sharedConfig = {
entryPoints: [entry],
bundle: true,
minify: !isDev,
keepNames: true,
sourcemap: tsconfigPathPluginContent.compilerOptions.sourceMap,
sourcesContent: false,
tsconfig,
plugins: [
svelteCompilePlugin,
TsconfigPathsPlugin({ tsconfig: tsconfigPathPluginContent }),
nodeExternalsPlugin({
allowList: [
"@budibase/frontend-core",
"svelte",
"chat",
"@chat-adapter/slack",
"@chat-adapter/teams",
"@chat-adapter/state-ioredis",
"@chat-adapter/state-memory",
],
}),
],
preserveSymlinks: true,
metafile: true,
external,
}
await mkdir("dist", { recursive: true })
const hbsFiles = (async () => {
const dir = await readdir("./", { recursive: true })
const files = dir.filter(
entry => entry.endsWith(".hbs") || entry.endsWith("ivm.bundle.js")
)
const fileCopyPromises = files.map(file =>
copyFile(file, `dist/${path.basename(file)}`)
)
await Promise.all(fileCopyPromises)
})()
const mainBuild = build({
...sharedConfig,
platform: "node",
outfile,
})
await Promise.all([hbsFiles, mainBuild])
if (isDev) {
fs.writeFileSync(
`dist/${path.basename(outfile)}.meta.json`,
JSON.stringify((await mainBuild).metafile)
)
}
console.log(
"\x1b[32m%s\x1b[0m",
`Build successfully in ${(Date.now() - start) / 1000} seconds`
)
}
if (require.main === module) {
const entry = argv["e"] || "./src/index.ts"
const outfile = `dist/${entry.split("/").pop().replace(".ts", ".js")}`
runBuild(entry, outfile)
} else {
module.exports = runBuild
}