Skip to content

Commit

Permalink
native: add cross-compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Jul 27, 2024
1 parent 995e9a4 commit 48b11f2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"dependencies": {
"@rcompat/fs": "^0.1.1",
"@rcompat/webview": "^0.4.0"
"@rcompat/webview": "^0.4.0",
"rcompat": "^0.18.0"
},
"type": "module",
"exports": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default async app => {
const app_js = $imports.find($import => $import.src.endsWith(".js"));

const assets_scripts = `
import Webview from "@rcompat/webview/worker/${app.build_target}";
import { join, text } from "@rcompat/fs";
import { stringify } from "rcompat/object";
import crypto from "rcompat/crypto";
Expand Down Expand Up @@ -75,6 +76,9 @@ export default async app => {
asset(pathname) {
return assets.find(asset => asset.src === pathname);
},
webview() {
return Webview;
}
};
const target = "desktop";
Expand Down
21 changes: 17 additions & 4 deletions packages/native/src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import Webview from "@rcompat/webview/worker";
import { desktop } from "./targets/exports.js";
import { dim } from "rcompat/colors";
import { execute } from "rcompat/stdio";
import desktop from "./desktop.js";
import targets from "./targets.js";

const command = "bun build build/serve.js --conditions=runtime --compile --minify";

export default ({
start = "/",
} = {}) => {
return {
name: "primate:native",
init(app, next) {
["windows", "linux", "darwin"]
.forEach(target => app.target(target, desktop));
Object.keys(targets).forEach(target => app.target(target, desktop));
return next(app);
},
build(app, next) {
app.done(async () => {
const { flags, exe } = targets[app.build_target];
const executable_path = dim(`${app.path.build}/${exe}`);
await execute(`${command} ${flags} --outfile build/${exe}`);
app.log.system(`executable written to ${executable_path}`);
});
return next(app);
},
async serve(app, next) {
if (app.build_target === "desktop") {
const Webview = app.loader.webview();
const webview = new Webview();
const { host, port } = app.get("http");
webview.navigate(`http://${host}:${port}${start}`);
Expand Down
31 changes: 31 additions & 0 deletions packages/native/src/targets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const desktop = {
flags: "",
};

const linux_x64 = {
flags: "--target=bun-linux-x64",
exe: "app",
};

const windows_x64 = {
flags: "--target=bun-windows-x64",
exe: "app.exe",
};

const darwin_x64 = {
flags: "--target=bun-darwin-x64",
exe: "app",
};

const darwin_arm64 = {
flags: "--target=bun-darwin-arm64",
exe: "app",
};

export default {
desktop,
"linux-x64": linux_x64,
"windows-x64": windows_x64,
"darwin-x64": darwin_x64,
"darwin-arm64": darwin_arm64,
};
1 change: 0 additions & 1 deletion packages/native/src/targets/exports.js

This file was deleted.

0 comments on commit 48b11f2

Please sign in to comment.