Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

bundler plugins

Preetham Pemmasani99b5ccb8138e
diff --git a/bindings/node/bundle.js b/bindings/node/bundle.js
new file mode 100644
index 0000000..7f5261d
--- /dev/null
+++ b/bindings/node/bundle.js
@@ -0,0 +1,61 @@
+// `bui`'s bundler, run on Bun (`bun bundle.js <options as JSON>`): the app and
+// its packages in one script for the QuickJS runtime, or one Bun executable.
+//
+// Framework bindings plug in here: a package in the `@blender-ui` scope with
+// `"bui": { "bunPlugin": "./plugin.js" }` in its package.json (a Bun plugin
+// as the default export) is used for every app that can import it. That's
+// how `.svelte` files compile; a future binding adds its own the same way.
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+'use strict'
+const fs = require('node:fs')
+const path = require('node:path')
+const { pathToFileURL } = require('node:url')
+
+/** The Bun plugins of the `@blender-ui/*` packages resolvable from `dir`. */
+function frameworkPlugins (dir) {
+ const found = new Map()
+ for (;; dir = path.dirname(dir)) {
+ const scope = path.join(dir, 'node_modules', '@blender-ui')
+ for (const name of fs.existsSync(scope) ? fs.readdirSync(scope) : []) {
+ const pkg = path.join(scope, name, 'package.json')
+ if (found.has(name) || !fs.existsSync(pkg)) continue
+ const plugin = JSON.parse(fs.readFileSync(pkg, 'utf8')).bui?.bunPlugin
+ if (plugin) found.set(name, path.join(scope, name, plugin))
+ }
+ if (path.dirname(dir) === dir) break
+ }
+ return Promise.all([...found.values()].map(async (file) => (await import(pathToFileURL(file).href)).default))
+}
+
+async function main ({ entry, out, bun, dev, console: keepConsole }) {
+ const define = { 'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production') }
+ const plugins = await frameworkPlugins(path.dirname(entry))
+ const r = await Bun.build(bun
+ ? {
+ entrypoints: [entry], minify: true, define, plugins,
+ compile: { outfile: out, windows: { hideConsole: !keepConsole } },
+ }
+ : {
+ // an ES module importing the entry: Bun's IIFE output runs a CommonJS
+ // entry only when something imports it
+ entrypoints: [writeWrapper(entry, out)], target: 'browser', format: 'iife', minify: true, define, plugins,
+ external: ['node:*', '*.node'],
+ })
+ if (!r.success) {
+ for (const log of r.logs) console.error(log)
+ process.exit(1)
+ }
+ if (!bun) fs.writeFileSync(out, await r.outputs[0].text())
+}
+
+function writeWrapper (entry, out) {
+ const wrapper = path.join(path.dirname(out), 'entry.mjs')
+ fs.writeFileSync(wrapper, `import ${JSON.stringify(path.resolve(entry).split(path.sep).join('/'))}\n`)
+ return wrapper
+}
+
+main(JSON.parse(process.argv[2])).catch((err) => {
+ console.error(err)
+ process.exit(1)
+})
diff --git a/bindings/node/cli.js b/bindings/node/cli.js
index 6daeb21..4483886 100644
--- a/bindings/node/cli.js
+++ b/bindings/node/cli.js
@@ -9,7 +9,7 @@
// -o, --out <file> output file (default: the entry's name, .exe on Windows)
// --bun compile with `bun build --compile` instead of QuickJS
// --console keep a console window (Windows; default: none, for apps)
-// --dev React's development build (default: production)
+// --dev development builds (React's, Svelte's dev mode)
//
// Bundling uses Bun (https://bun.sh), which must be installed.
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -72,17 +72,16 @@ ${r.stderr}` : ''}`)
return r
}
+/** Bundle with Bun (bundle.js: the framework bindings' plugins included). */
+function bundleWith (options) {
+ bun([path.join(__dirname, 'bundle.js'), JSON.stringify(options)], { quiet: true })
+}
+
/** One script for QuickJS: the app and its packages, React in production mode. */
function bundle (entry, dev) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bui-'))
- // an ES module importing the entry: Bun's IIFE output runs a CommonJS
- // entry only when something imports it
- const wrapper = path.join(dir, 'entry.mjs')
- fs.writeFileSync(wrapper, `import ${JSON.stringify(path.resolve(entry).split(path.sep).join('/'))}\n`)
const out = path.join(dir, 'app.js')
- bun(['build', wrapper, '--target=browser', '--format=iife', '--minify',
- `--define=process.env.NODE_ENV="${dev ? 'development' : 'production'}"`,
- '--external=node:*', '--external=*.node', `--outfile=${out}`], { quiet: true })
+ bundleWith({ entry: path.resolve(entry), out, dev })
const js = fs.readFileSync(out)
fs.rmSync(dir, { recursive: true, force: true })
return js
@@ -101,10 +100,7 @@ function build (o) {
const out = path.resolve(o.out ?? `${name}${EXE}`)
const t0 = Date.now()
if (o.bun) {
- const args = ['build', '--compile', '--minify', o.entry, `--outfile=${out}`,
- `--define=process.env.NODE_ENV="${o.dev ? 'development' : 'production'}"`]
- if (process.platform === 'win32' && !o.console) args.push('--windows-hide-console')
- bun(args)
+ bundleWith({ entry: path.resolve(o.entry), out, bun: true, dev: o.dev, console: o.console || process.platform !== 'win32' })
} else {
const js = bundle(o.entry, o.dev)
const runtime = fs.readFileSync(runtimePath())
diff --git a/bindings/node/package.json b/bindings/node/package.json
index ad4aacc..63def16 100644
--- a/bindings/node/package.json
+++ b/bindings/node/package.json
@@ -23,6 +23,7 @@
"index.js",
"dom.js",
"host.js",
+ "bundle.js",
"signals.js",
"index.d.ts",
"host.d.ts",