@farm.js/wasm 0.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Farm.js Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @farm.js/wasm
2
+
3
+ Import prebuilt WebAssembly modules in Farm browser code and browser workers.
4
+
5
+ ```sh
6
+ pnpm add @farm.js/wasm
7
+ ```
8
+
9
+ ```ts
10
+ import { defineConfig } from "@farm.js/core";
11
+ import { wasm } from "@farm.js/wasm";
12
+
13
+ export default defineConfig({
14
+ plugins: [wasm()],
15
+ });
16
+ ```
17
+
18
+ The plugin installs `vite-plugin-wasm` in both build pipelines, uses ES-module workers, and selects a browser target that supports native top-level await. The default is Chrome/Edge 89, Firefox 114, and Safari 15 unless your app supplies `vite.build.target`. Override it explicitly with `wasm({ target: ["chrome120", "firefox120", "safari17"] })`.
19
+
20
+ This is a browser loading/build plugin, not a Rust compiler, worker pool, or universal server/edge adapter. Import the JavaScript entry point supplied by your Wasm package when it has one. Keep browser-only imports inside client event handlers or effects.
21
+
22
+ - [Documentation](https://farmjs.dev/docs/plugins/wasm)
23
+ - [Runnable example](https://github.com/farming-labs/farm.js/tree/main/examples/wasm-demo)
@@ -0,0 +1,12 @@
1
+ export interface WasmOptions {
2
+ /**
3
+ * Browser compilation target. Must support native top-level await and module workers.
4
+ * Overrides vite.build.target for browser builds only; server targets are unchanged.
5
+ * Uses vite.build.target when supplied, otherwise Chrome/Edge 89,
6
+ * Firefox 114, and Safari 15. Does not polyfill browser runtime limitations.
7
+ */
8
+ target?: string | string[];
9
+ }
10
+ export declare const DEFAULT_WASM_TARGET: string[];
11
+ export declare function resolveWasmOptions(options: WasmOptions): WasmOptions;
12
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC1B;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC5B;AAED,eAAO,MAAM,mBAAmB,UAAmD,CAAC;AAEpF,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,CAuBpE"}
package/dist/config.js ADDED
@@ -0,0 +1,20 @@
1
+ export const DEFAULT_WASM_TARGET = ["chrome89", "edge89", "firefox114", "safari15"];
2
+ export function resolveWasmOptions(options) {
3
+ if (!options || typeof options !== "object" || Array.isArray(options)) {
4
+ throw new TypeError("[farm:wasm] Options must be an object");
5
+ }
6
+ for (const key of Object.keys(options)) {
7
+ if (key !== "target") {
8
+ throw new TypeError(`[farm:wasm] Unknown option ${JSON.stringify(key)}. The supported option is target.`);
9
+ }
10
+ }
11
+ const target = options.target;
12
+ if (target === undefined)
13
+ return {};
14
+ const targets = Array.isArray(target) ? target : [target];
15
+ if (targets.length === 0 ||
16
+ targets.some((value) => typeof value !== "string" || value.trim() === "")) {
17
+ throw new TypeError("[farm:wasm] target must be a non-empty browser target or array of browser targets");
18
+ }
19
+ return { target: Array.isArray(target) ? [...target] : target };
20
+ }
@@ -0,0 +1,6 @@
1
+ import { type FarmPlugin } from "@farm.js/core/plugin";
2
+ import { type WasmOptions } from "./config.js";
3
+ export type { WasmOptions };
4
+ /** Configure WebAssembly ESM imports in Farm's browser and browser-worker builds. */
5
+ export declare function wasm(options?: WasmOptions): FarmPlugin;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGrE,OAAO,EAA2C,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAExF,YAAY,EAAE,WAAW,EAAE,CAAC;AAE5B,qFAAqF;AACrF,wBAAgB,IAAI,CAAC,OAAO,GAAE,WAAgB,GAAG,UAAU,CAuE1D"}
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ import { definePlugin } from "@farm.js/core/plugin";
2
+ import wasmPlugin from "vite-plugin-wasm";
3
+ import { DEFAULT_WASM_TARGET, resolveWasmOptions } from "./config.js";
4
+ /** Configure WebAssembly ESM imports in Farm's browser and browser-worker builds. */
5
+ export function wasm(options = {}) {
6
+ const resolved = resolveWasmOptions(options);
7
+ function plugins() {
8
+ const upstream = wasmPlugin();
9
+ // The upstream helper uses a root URL. Farm handles page requests before
10
+ // Vite, so give this virtual module Vite's standard null-prefixed identity.
11
+ const helper = "/__vite-plugin-wasm-helper";
12
+ const virtualHelper = `\0${helper}`;
13
+ return [
14
+ {
15
+ ...upstream,
16
+ resolveId(id, ...args) {
17
+ if (id === helper)
18
+ return virtualHelper;
19
+ return upstream.resolveId.call(this, id, ...args);
20
+ },
21
+ load(id, ...args) {
22
+ return upstream.load.call(this, id === virtualHelper ? helper : id, ...args);
23
+ },
24
+ },
25
+ ];
26
+ }
27
+ return definePlugin({
28
+ name: "farm:wasm",
29
+ enforce: "pre",
30
+ configure(config) {
31
+ if ((config.plugins ?? []).filter((plugin) => plugin.name === "farm:wasm").length > 1) {
32
+ throw new Error("[farm:wasm] Configure one wasm() plugin instance");
33
+ }
34
+ // Install worker handling through a Vite config hook as well as the public
35
+ // config. Farm constructs separate production Vite configs for each build.
36
+ const workerPlugin = {
37
+ name: "farm:wasm-workers",
38
+ config(viteConfig, environment) {
39
+ const worker = { ...config.vite?.worker, ...viteConfig.worker };
40
+ const existing = worker.plugins;
41
+ if (worker.format === "iife") {
42
+ throw new Error('[farm:wasm] WebAssembly imports require worker.format: "es". Remove the iife override.');
43
+ }
44
+ if (environment.command === "build" && !viteConfig.build?.ssr) {
45
+ // Vite concatenates arrays returned from config hooks. Replace the
46
+ // target so Farm's older default cannot remain in the target list.
47
+ viteConfig.build = {
48
+ ...viteConfig.build,
49
+ target: resolved.target ?? config.vite?.build?.target ?? [...DEFAULT_WASM_TARGET],
50
+ };
51
+ }
52
+ return {
53
+ worker: {
54
+ ...worker,
55
+ format: "es",
56
+ plugins: (...args) => [
57
+ ...plugins(),
58
+ ...(typeof existing === "function" ? existing(...args) : (existing ?? [])),
59
+ ],
60
+ },
61
+ };
62
+ },
63
+ };
64
+ return {
65
+ ...config,
66
+ vite: {
67
+ ...config.vite,
68
+ plugins: [workerPlugin, ...plugins(), ...(config.vite?.plugins ?? [])],
69
+ },
70
+ };
71
+ },
72
+ });
73
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@farm.js/wasm",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "WebAssembly imports and browser workers for Farm.js applications",
5
+ "keywords": [
6
+ "farm.js",
7
+ "vite",
8
+ "wasm",
9
+ "webassembly",
10
+ "worker"
11
+ ],
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/farming-labs/farm.js",
16
+ "directory": "packages/farm-wasm"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "type": "module",
22
+ "main": "./dist/index.js",
23
+ "module": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ }
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "dependencies": {
35
+ "vite-plugin-wasm": "^3.6.0",
36
+ "@farm.js/core": "0.1.0-beta.96"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^20.10.5",
40
+ "typescript": "^5.3.3",
41
+ "vite": "^5.0.10",
42
+ "vitest": "^3.2.7",
43
+ "wabt": "^1.0.39"
44
+ },
45
+ "engines": {
46
+ "node": ">=22.13.0"
47
+ },
48
+ "scripts": {
49
+ "build": "tsc",
50
+ "dev": "tsc --watch",
51
+ "type-check": "tsc --noEmit",
52
+ "test": "vitest run"
53
+ }
54
+ }