budibase/scripts/build.js

101 lines
2.4 KiB
JavaScript
Raw Normal View History

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 glob = require("glob")
const fs = require("fs")
const path = require("path")
2023-03-02 17:03:56 +01:00
2023-03-02 16:34:52 +01:00
const { build } = require("esbuild")
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
2023-12-05 11:02:44 +01:00
var { argv } = require("yargs")
2023-05-03 13:38:23 +02:00
2023-10-03 09:49:58 +02:00
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`
const tsconfigPathPluginContent = JSON.parse(
fs.readFileSync(tsconfig, "utf-8")
)
2023-08-10 10:48:18 +02:00
if (
2023-10-31 14:49:40 +01:00
!fs.existsSync(path.join(__dirname, "../packages/pro/src")) &&
2023-08-10 10:48:18 +02:00
tsconfigPathPluginContent.compilerOptions?.paths
) {
2023-07-26 12:01:05 +02:00
// If we don't have pro, we cannot bundle backend-core.
// Otherwise, the main context will not be shared between libraries
2023-08-14 18:44:24 +02:00
delete tsconfigPathPluginContent?.compilerOptions?.paths?.[
"@budibase/backend-core"
]
2023-08-14 18:44:24 +02:00
delete tsconfigPathPluginContent?.compilerOptions?.paths?.[
"@budibase/backend-core/*"
]
}
2023-05-03 15:04:54 +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,
plugins: [
TsconfigPathsPlugin({ tsconfig: tsconfigPathPluginContent }),
nodeExternalsPlugin(),
],
preserveSymlinks: true,
2023-05-03 18:15:57 +02:00
loader: {
".svelte": "copy",
2024-02-06 10:18:58 +01:00
".ivm.bundle.js": "text",
2023-05-03 18:15:57 +02:00
},
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",
2023-10-31 15:21:40 +01:00
],
2024-02-13 17:43:42 +01:00
define: {
"process.env.BUNDLED": '"true"',
},
}
build({
...sharedConfig,
platform: "node",
2023-05-03 15:04:54 +02:00
outfile,
2023-06-16 15:16:32 +02:00
}).then(result => {
glob(`${process.cwd()}/src/**/*.hbs`, {}, (err, files) => {
for (const file of files) {
fs.copyFileSync(file, `${process.cwd()}/dist/${path.basename(file)}`)
}
2023-10-03 09:49:58 +02:00
console.log(
"\x1b[32m%s\x1b[0m",
`Build successfully in ${(Date.now() - start) / 1000} seconds`
)
})
2023-06-16 15:16:32 +02:00
2023-10-03 09:49:58 +02:00
fs.writeFileSync(
`dist/${path.basename(outfile)}.meta.json`,
JSON.stringify(result.metafile)
)
})
2023-03-02 16:34:52 +01: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)
} else {
module.exports = runBuild
}