Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: `harper.js` is now a single file

Elijah Pottere45366ec1f59
diff --git a/justfile b/justfile
index 45c84577c..4801d4784 100644
--- a/justfile
+++ b/justfile
@@ -11,6 +11,8 @@ build-harperjs:
#! /bin/bash
set -eo pipefail
just build-wasm web
+
+ sed -i 's/new URL(.*)/new URL()/g' "{{justfile_directory()}}/harper-wasm/pkg/harper_wasm.js"
cd "{{justfile_directory()}}/packages/harper.js"
yarn install -f
diff --git a/packages/harper.js/package.json b/packages/harper.js/package.json
index 8961e2807..78ae3c1a5 100644
--- a/packages/harper.js/package.json
+++ b/packages/harper.js/package.json
@@ -17,6 +17,7 @@
"typescript": "~5.6.2",
"vite": "^5.1.8",
"vite-plugin-dts": "^4.3.0",
+ "vite-plugin-virtual": "^0.3.0",
"vitest": "^2.1.8"
},
"main": "dist/harper.js",
diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.ts
index cee04a2d2..d9b609da7 100644
--- a/packages/harper.js/src/WorkerLinter/index.ts
+++ b/packages/harper.js/src/WorkerLinter/index.ts
@@ -1,7 +1,8 @@
import { DeserializedRequest, deserializeArg, serialize } from './communication';
import type { Lint, Suggestion, Span } from 'wasm';
import Linter from '../Linter';
-import Worker from './worker.js?worker';
+import Worker from './worker.js?worker&inline';
+import { getWasmUri } from '../loadWasm';
/** The data necessary to complete a request once the worker has responded. */
type RequestItem = {
@@ -24,8 +25,12 @@ export default class WorkerLinter implements Linter {
this.worker = new Worker();
this.requestQueue = [];
+ // Fires when the worker sends 'ready'.
this.worker.onmessage = () => {
this.setupMainEventListeners();
+
+ this.worker.postMessage(getWasmUri());
+
this.working = false;
this.submitRemainingRequests();
};
diff --git a/packages/harper.js/src/WorkerLinter/worker.js b/packages/harper.js/src/WorkerLinter/worker.js
index d6bf952bf..38e17541c 100644
--- a/packages/harper.js/src/WorkerLinter/worker.js
+++ b/packages/harper.js/src/WorkerLinter/worker.js
@@ -1,3 +1,4 @@
+import { setWasmUri } from '../loadWasm';
import LocalLinter from '../LocalLinter';
import { deserialize, serializeArg } from './communication';
@@ -12,7 +13,11 @@ async function processRequest(v) {
}
self.onmessage = function (e) {
- processRequest(e.data);
+ setWasmUri(e.data);
+
+ self.onmessage = function (e) {
+ processRequest(e.data);
+ };
};
// Notify the main thread that we are ready
diff --git a/packages/harper.js/src/loadWasm.ts b/packages/harper.js/src/loadWasm.ts
index e15a4ba7d..fbca1481e 100644
--- a/packages/harper.js/src/loadWasm.ts
+++ b/packages/harper.js/src/loadWasm.ts
@@ -1,7 +1,24 @@
-/** Load the WebAssembly manually and dynamically, making sure to setup infrastructure. */
+// @ts-ignore
+import wasmUri from 'virtual:wasm';
+
+let curWasmUri = wasmUri;
+
+/** Get the currently set data URI for the WebAssembly module. */
+export function getWasmUri(): string {
+ return curWasmUri;
+}
+
+/** Set the data URI for the WebAssembly module. */
+export function setWasmUri(uri: string) {
+ curWasmUri = uri;
+}
+
+/** Load the WebAssembly manually and dynamically, making sure to setup infrastructure.
+ * You can use an optional data URL for the WebAssembly file if the module is being loaded from a Web Worker.
+ * */
export default async function loadWasm() {
const wasm = await import('wasm');
- await wasm.default();
+ await wasm.default(getWasmUri());
return wasm;
}
diff --git a/packages/harper.js/vite.config.js b/packages/harper.js/vite.config.js
index d8c33004d..87c3ee7c5 100644
--- a/packages/harper.js/vite.config.js
+++ b/packages/harper.js/vite.config.js
@@ -1,6 +1,7 @@
import { resolve } from 'path';
import dts from 'vite-plugin-dts';
import { defineConfig } from 'vite';
+import virtual from 'vite-plugin-virtual';
export default defineConfig({
build: {
@@ -17,11 +18,19 @@ export default defineConfig({
}
},
base: './',
- plugins: [dts({ rollupTypes: true, tsconfigPath: './tsconfig.json' })],
+ plugins: [
+ dts({ rollupTypes: true, tsconfigPath: './tsconfig.json' }),
+ virtual({
+ 'virtual:wasm': `import wasmUri from 'wasm/harper_wasm_bg.wasm?inline'; export default wasmUri`
+ })
+ ],
worker: {
- plugins: [],
+ plugins: [
+ virtual({
+ 'virtual:wasm': `export default ''`
+ })
+ ],
format: 'es',
-
rollupOptions: {
output: {
inlineDynamicImports: true
diff --git a/packages/yarn.lock b/packages/yarn.lock
index cf25f97cd..f45da8679 100644
--- a/packages/yarn.lock
+++ b/packages/yarn.lock
@@ -5500,6 +5500,11 @@ vite-plugin-top-level-await@^1.4.1:
"@swc/core" "^1.3.100"
uuid "^9.0.1"
+vite-plugin-virtual@^0.3.0:
+ version "0.3.0"
+ resolved "https://registry.yarnpkg.com/vite-plugin-virtual/-/vite-plugin-virtual-0.3.0.tgz#5831af99864db65b9c46b970a919ec9fc17b374d"
+ integrity sha512-TOtrWw6jKrJNXfxhGRUiQzfAP1gRkYkVzMkJNjHUJ8idLuxf8eeeDKZKZHhdeYfaCc/87rv+KvWE2iCy1QInWA==
+
vite-plugin-wasm@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/vite-plugin-wasm/-/vite-plugin-wasm-3.3.0.tgz#2908ef2529bf8f33f4e549c8c6fda26ad273ca15"