Skip to content

Commit

Permalink
HMR
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi committed May 1, 2024
1 parent e3ff655 commit 5abee2b
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 10 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<link rel="stylesheet" type="text/css" href="/style.css">
<script type="module">
import { load } from "/src/loader/load.ts";
load("/.build/wasm32-unknown-wasi/debug/StringCounter.wasm");
import wasmURL from "/.build/wasm32-unknown-wasi/debug/StringCounter.wasm?url";
load(wasmURL);
</script>
</head>
<body>
Expand Down
175 changes: 175 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"path-browserify": "^1.0.1",
"typescript": "^5.4.5",
"vite": "^5.2.10"
},
"devDependencies": {
"chokidar": "^3.6.0"
}
}
36 changes: 36 additions & 0 deletions src/build/SwiftPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Plugin } from 'vite';
import { watch } from 'chokidar';
import { exec } from 'child_process';
import { promisify } from 'util';

const execAsync = promisify(exec);

const SwiftPlugin = (): Plugin => {
return {
name: "vite-plugin-swift",
configureServer(server) {
const watcher = watch("Sources/**/*.swift", {
persistent: true
});

watcher.on("change", async (path) => {
console.log(`${path} changed, rebuilding swift...`);
try {
const { stdout, stderr } = await execAsync("bin/build");
console.log(stdout);
if(stderr) {
console.error(stderr);
}
} catch (error) {
console.error("build failed", error);
}
});

server.httpServer?.on("close", () => {
watcher.close();
});
}
}
};

export default SwiftPlugin;
8 changes: 4 additions & 4 deletions src/loader/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import { WasmRunner } from "./WasmRunner";
import { SwiftRuntime } from "../JavaScriptKit/Runtime/src/index";

const startWasiTask = async (exePath: string) => {
const startWasiTask = async (wasmURL: string) => {
// Fetch our Wasm File
const response = await fetch(exePath);
const response = await fetch(wasmURL);
const responseArrayBuffer = await response.arrayBuffer();

const wasmRunner = WasmRunner(false, SwiftRuntime);
Expand All @@ -34,9 +34,9 @@ function handleError(e: any) {
}
}

export function load(exePath: string) {
export function load(wasmURL: string) {
try {
startWasiTask(exePath).catch(handleError);
startWasiTask(wasmURL).catch(handleError);
} catch (e) {
handleError(e);
}
Expand Down
5 changes: 0 additions & 5 deletions vite.config.js

This file was deleted.

8 changes: 8 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vite';
import SwiftPlugin from './src/build/SwiftPlugin';

export default defineConfig({
plugins: [
SwiftPlugin()
]
});

0 comments on commit 5abee2b

Please sign in to comment.