blockbench/build.js

82 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2025-03-02 21:16:55 +01:00
import * as esbuild from 'esbuild'
2025-03-06 22:42:41 +01:00
import { glsl } from "esbuild-plugin-glsl";
2025-03-02 21:16:55 +01:00
import { createRequire } from "module";
import commandLineArgs from 'command-line-args'
import path from 'path';
2025-03-05 14:32:30 +01:00
import { writeFileSync } from 'fs';
2025-03-02 21:16:55 +01:00
const pkg = createRequire(import.meta.url)("./package.json");
const options = commandLineArgs([
{name: 'target', type: String},
{name: 'watch', type: Boolean},
{name: 'serve', type: Boolean},
2025-03-05 14:32:30 +01:00
{name: 'analyze', type: Boolean},
2025-03-02 21:16:55 +01:00
])
function conditionalImportPlugin(config) {
return {
name: 'conditional-import-plugin',
setup(build) {
build.onResolve({ filter: /desktop.js$/ }, args => {
return { path: path.join(args.resolveDir, config.file) };
});
}
};
};
2025-03-06 22:42:41 +01:00
const isApp = options.target == 'electron';
const dev_mode = options.watch || options.serve;
const minify = !dev_mode;
2025-03-02 21:16:55 +01:00
2025-03-02 23:31:06 +01:00
/**
* @typedef {esbuild.BuildOptions} BuildOptions
*/
2025-03-02 21:16:55 +01:00
const config = {
entryPoints: ['./js/main.js'],
define: {
isApp: isApp.toString(),
appVersion: `"${pkg.version}"`,
},
platform: 'node',
target: 'es2020',
format: 'esm',
bundle: true,
2025-03-06 22:42:41 +01:00
minify,
2025-03-02 21:16:55 +01:00
outfile: './dist/bundle.js',
2025-03-05 14:32:30 +01:00
mainFields: ['module', 'main'],
2025-03-02 21:16:55 +01:00
external: [
'electron',
],
plugins: [
conditionalImportPlugin({
file: isApp ? 'desktop.js' : 'web.js'
2025-03-06 22:42:41 +01:00
}),
glsl({
minify
2025-03-02 21:16:55 +01:00
})
],
sourcemap: true,
}
if (options.watch || options.serve) {
let ctx = await esbuild.context(config);
if (isApp) {
2025-03-02 21:16:55 +01:00
await ctx.watch({});
} else {
const host = 'localhost';
const port = 3000;
await ctx.serve({
servedir: import.meta.dirname,
host,
port
});
console.log(`Hosting app at http://${host}:${port}`)
2025-03-02 21:16:55 +01:00
}
} else {
2025-03-05 14:32:30 +01:00
if (options.analyze) config.metafile = true;
let result = await esbuild.build(config);
if (options.analyze) {
writeFileSync('./dist/esbuild-metafile.json', JSON.stringify(result.metafile))
}
2025-03-02 21:16:55 +01:00
}