2023-04-24 19:24:09 +02:00
|
|
|
#!/usr/bin/node
|
|
|
|
|
2023-03-02 16:34:52 +01:00
|
|
|
const start = Date.now()
|
|
|
|
|
2023-03-02 17:03:56 +01:00
|
|
|
const fs = require("fs")
|
2024-05-16 18:33:47 +02:00
|
|
|
const { cp, readdir, copyFile, mkdir } = require("node:fs/promises")
|
2023-04-27 20:00:01 +02:00
|
|
|
const path = require("path")
|
2023-03-02 17:03:56 +01:00
|
|
|
|
2023-03-02 16:34:52 +01:00
|
|
|
const { build } = require("esbuild")
|
2024-05-16 18:33:47 +02:00
|
|
|
const { compile } = require("svelte/compiler")
|
2023-03-02 16:34:52 +01:00
|
|
|
|
2023-05-25 11:49:38 +02:00
|
|
|
const {
|
|
|
|
default: TsconfigPathsPlugin,
|
|
|
|
} = require("@esbuild-plugins/tsconfig-paths")
|
2023-06-16 15:26:34 +02:00
|
|
|
const { nodeExternalsPlugin } = require("esbuild-node-externals")
|
2023-03-02 16:34:52 +01:00
|
|
|
|
2024-02-22 14:19:29 +01:00
|
|
|
const svelteCompilePlugin = {
|
2024-05-16 18:33:47 +02:00
|
|
|
name: "svelteCompile",
|
2024-02-22 14:19:29 +01:00
|
|
|
setup(build) {
|
|
|
|
// Compiles `.svelte` files into JS classes so that they can be directly imported into our
|
|
|
|
// Typescript packages
|
2024-05-16 18:33:47 +02:00
|
|
|
build.onLoad({ filter: /\.svelte$/ }, async args => {
|
|
|
|
const source = await fs.promises.readFile(args.path, "utf8")
|
|
|
|
const dir = path.dirname(args.path)
|
2024-02-22 14:19:29 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
const { js } = compile(source, { css: "injected", generate: "ssr" })
|
|
|
|
|
|
|
|
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.
|
2024-05-16 18:33:47 +02:00
|
|
|
loader: "js",
|
2024-02-22 14:19:29 +01:00
|
|
|
// Where to resolve any imports present in the loaded file
|
2024-05-16 18:33:47 +02:00
|
|
|
resolveDir: dir,
|
2024-02-22 14:19:29 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { errors: [JSON.stringify(e)] }
|
|
|
|
}
|
|
|
|
})
|
2024-05-16 18:33:47 +02:00
|
|
|
},
|
2024-02-22 14:19:29 +01:00
|
|
|
}
|
|
|
|
|
2023-12-05 11:02:44 +01:00
|
|
|
var { argv } = require("yargs")
|
2023-05-03 13:38:23 +02:00
|
|
|
|
2024-02-22 14:19:29 +01:00
|
|
|
async function runBuild(entry, outfile) {
|
2023-05-03 13:38:23 +02:00
|
|
|
const isDev = process.env.NODE_ENV !== "production"
|
2023-05-03 15:04:54 +02:00
|
|
|
const tsconfig = argv["p"] || `tsconfig.build.json`
|
2023-07-26 11:32:03 +02:00
|
|
|
const tsconfigPathPluginContent = JSON.parse(
|
|
|
|
fs.readFileSync(tsconfig, "utf-8")
|
|
|
|
)
|
|
|
|
|
2023-04-27 20:00:01 +02:00
|
|
|
const sharedConfig = {
|
|
|
|
entryPoints: [entry],
|
2023-10-03 09:49:58 +02:00
|
|
|
bundle: true,
|
2023-05-03 13:38:23 +02:00
|
|
|
minify: !isDev,
|
2023-05-03 18:15:57 +02:00
|
|
|
sourcemap: isDev,
|
2023-05-03 15:04:54 +02:00
|
|
|
tsconfig,
|
2023-07-26 11:32:03 +02:00
|
|
|
plugins: [
|
2024-02-22 14:19:29 +01:00
|
|
|
svelteCompilePlugin,
|
2023-07-26 11:32:03 +02:00
|
|
|
TsconfigPathsPlugin({ tsconfig: tsconfigPathPluginContent }),
|
2024-03-25 17:39:42 +01:00
|
|
|
nodeExternalsPlugin({
|
2024-07-29 15:57:38 +02:00
|
|
|
allowList: ["@budibase/frontend-core", "@budibase/pro", "svelte"],
|
2024-03-25 17:39:42 +01:00
|
|
|
}),
|
2023-07-26 11:32:03 +02:00
|
|
|
],
|
2023-04-27 20:00:01 +02:00
|
|
|
preserveSymlinks: true,
|
2023-10-03 09:49:58 +02:00
|
|
|
metafile: true,
|
2023-10-31 15:21:40 +01:00
|
|
|
external: [
|
|
|
|
"deasync",
|
|
|
|
"mock-aws-s3",
|
|
|
|
"nock",
|
|
|
|
"bull",
|
|
|
|
"pouchdb",
|
|
|
|
"bcrypt",
|
|
|
|
"bcryptjs",
|
2023-12-18 18:41:15 +01:00
|
|
|
"graphql/*",
|
2024-02-06 10:18:58 +01:00
|
|
|
"bson",
|
2024-05-16 18:33:47 +02:00
|
|
|
"better-sqlite3",
|
|
|
|
"sqlite3",
|
|
|
|
"mysql",
|
|
|
|
"mysql2",
|
|
|
|
"oracle",
|
2024-05-29 12:29:43 +02:00
|
|
|
"oracledb",
|
2024-05-16 18:33:47 +02:00
|
|
|
"pg",
|
|
|
|
"pg-query-stream",
|
|
|
|
"pg-native",
|
2023-10-31 15:21:40 +01:00
|
|
|
],
|
2023-04-27 20:00:01 +02:00
|
|
|
}
|
|
|
|
|
2024-05-16 18:33:47 +02:00
|
|
|
await mkdir("dist", { recursive: true })
|
2024-02-22 14:19:29 +01:00
|
|
|
|
|
|
|
const hbsFiles = (async () => {
|
2024-05-16 18:33:47 +02:00
|
|
|
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)}`)
|
|
|
|
)
|
2024-02-22 14:19:29 +01:00
|
|
|
|
|
|
|
await Promise.all(fileCopyPromises)
|
|
|
|
})()
|
|
|
|
|
2024-03-25 17:39:42 +01:00
|
|
|
const oldClientVersions = (async () => {
|
|
|
|
try {
|
2024-05-16 18:33:47 +02:00
|
|
|
await cp("./build/oldClientVersions", "./dist/oldClientVersions", {
|
|
|
|
recursive: true,
|
|
|
|
})
|
2024-03-25 17:39:42 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== "EEXIST" && e.code !== "ENOENT") {
|
2024-05-16 18:33:47 +02:00
|
|
|
throw e
|
2024-03-25 17:39:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})()
|
|
|
|
|
2024-02-22 14:19:29 +01:00
|
|
|
const mainBuild = build({
|
2023-04-27 20:00:01 +02:00
|
|
|
...sharedConfig,
|
|
|
|
platform: "node",
|
2023-05-03 15:04:54 +02:00
|
|
|
outfile,
|
2024-02-22 14:19:29 +01:00
|
|
|
})
|
2023-04-27 20:00:01 +02:00
|
|
|
|
2024-03-25 17:39:42 +01:00
|
|
|
await Promise.all([hbsFiles, mainBuild, oldClientVersions])
|
2023-06-16 15:16:32 +02:00
|
|
|
|
2024-02-22 14:19:29 +01:00
|
|
|
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`
|
|
|
|
)
|
2023-03-02 16:34:52 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 20:00:01 +02:00
|
|
|
if (require.main === module) {
|
2023-05-03 13:38:23 +02:00
|
|
|
const entry = argv["e"] || "./src/index.ts"
|
2023-05-03 16:45:29 +02:00
|
|
|
const outfile = `dist/${entry.split("/").pop().replace(".ts", ".js")}`
|
|
|
|
runBuild(entry, outfile)
|
2023-04-27 20:00:01 +02:00
|
|
|
} else {
|
|
|
|
module.exports = runBuild
|
|
|
|
}
|