Skip to content

Commit

Permalink
native: enable autodetection on desktop target
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Aug 4, 2024
1 parent 2b51c1f commit 41d17af
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/build/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async (root, config) => {
postbuild: [],
bindings: {},
roots: [],
targets: { web },
targets: { web: { target: web } },
importmaps: {},
assets: [],
path,
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/build/hook/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import $router from "./router.js";
const html = /^.*.html$/u;

const pre = async (app, mode, target) => {
if (app.targets[target] === undefined) {
let target$ = target;
if (app.targets[target$] === undefined) {
throw new Error(`target ${dim(target)} does not exist`);
}
app.build_target = target;
log.system(`starting ${dim(target)} build in ${dim(mode)} mode`);
if (app.targets[target$].forward) {
target$ = app.targets[target$].forward;
}
app.build_target = target$;
log.system(`starting ${dim(target$)} build in ${dim(mode)} mode`);

app.build = new Build({
...exclude(app.get("build"), ["includes", "index"]),
Expand Down Expand Up @@ -147,7 +151,7 @@ const post = async (app, mode, target) => {
await app.build.start();

// a target needs to create an `assets.js` that exports assets
await app.targets[target](app);
await app.targets[app.build_target].target(app);

const build_number = crypto.randomUUID().slice(0, 8);
const build_directory = app.path.build.join(build_number);
Expand Down
2 changes: 1 addition & 1 deletion packages/native/src/desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default async app => {
return Webview;
}
};
const target = "desktop";
const target = "${app.build_target}";
export { assets, loader, target };
`;
Expand Down
8 changes: 4 additions & 4 deletions packages/native/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dim from "@rcompat/cli/color/dim";
import execute from "@rcompat/stdio/execute";
import desktop from "./desktop.js";
import targets from "./targets.js";
import log from "@primate/core/log";

const target_keys = Object.keys(targets);
const command = "bun build build/serve.js --conditions=runtime --compile --minify";

export default ({
Expand All @@ -12,11 +12,11 @@ export default ({
return {
name: "primate:native",
init(app, next) {
Object.keys(targets).forEach(target => app.target(target, desktop));
target_keys.forEach(target => app.target(target, targets[target]));
return next(app);
},
build(app, next) {
if (app.build_target === "linux-x64") {
if (target_keys.includes(app.build_target)) {
app.done(async () => {
const { flags, exe } = targets[app.build_target];
const executable_path = dim(`${app.path.build}/${exe}`);
Expand All @@ -27,7 +27,7 @@ export default ({
return next(app);
},
async serve(app, next) {
if (app.build_target === "desktop") {
if (target_keys.includes(app.build_target)) {
const Webview = app.loader.webview();
const webview = new Webview();
const { host, port } = app.get("http");
Expand Down
8 changes: 7 additions & 1 deletion packages/native/src/targets.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import target from "./desktop.js";

const desktop = {
flags: "",
forward: `${process.platform}-${process.arch}`,
};

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

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

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

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

export default {
Expand Down

0 comments on commit 41d17af

Please sign in to comment.