sommark 5.0.4 → 5.1.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/rollup.js ADDED
@@ -0,0 +1,79 @@
1
+ import { readFileSync, existsSync } from "node:fs";
2
+ import nodePath from "node:path";
3
+
4
+ /**
5
+ * Rollup plugin for SomMark.
6
+ *
7
+ * Fixes two issues:
8
+ * 1. Prevents Rollup from tree-shaking QuickJS modules (they have side effects
9
+ * that Rollup incorrectly marks as removable).
10
+ * 2. Handles QuickJS WASM assets referenced via new URL("*.wasm", import.meta.url).
11
+ * Rollup does not copy or rewrite these automatically, so this plugin emits
12
+ * them as assets and rewrites the URLs to point to the output paths.
13
+ *
14
+ * @example
15
+ * // rollup.config.js
16
+ * import { sommarkRollup } from "sommark/rollup";
17
+ * export default { plugins: [sommarkRollup(), commonjs(), nodeResolve({ browser: true })] };
18
+ */
19
+ export function sommarkRollup() {
20
+ let emitted = new Map();
21
+
22
+ return {
23
+ name: "sommark",
24
+
25
+ options(opts) {
26
+ const prev = opts.treeshake;
27
+ return {
28
+ ...opts,
29
+ treeshake: {
30
+ ...(prev && typeof prev === "object" ? prev : {}),
31
+ moduleSideEffects(id, external) {
32
+ // quickjs-emscripten uses side-effectful env/importObject setup that
33
+ // tree-shaking removes incorrectly without this guard
34
+ if (id.includes("quickjs") || id.includes("@jitl/")) return true;
35
+ const fn = prev?.moduleSideEffects;
36
+ return typeof fn === "function" ? fn(id, external) : (fn ?? true);
37
+ },
38
+ },
39
+ };
40
+ },
41
+
42
+ buildStart() {
43
+ emitted = new Map();
44
+ },
45
+
46
+ transform(code, id) {
47
+ if (!code.includes(".wasm")) return null;
48
+
49
+ const dir = nodePath.dirname(id);
50
+ let changed = false;
51
+ let result = code;
52
+
53
+ const pattern = /new URL\(['"]([^'"]+\.wasm)['"]\s*,\s*import\.meta\.url\)/g;
54
+ let match;
55
+ while ((match = pattern.exec(code)) !== null) {
56
+ const [full, wasmFile] = match;
57
+ const wasmPath = nodePath.resolve(dir, wasmFile);
58
+ if (!existsSync(wasmPath)) continue;
59
+
60
+ let refId;
61
+ if (emitted.has(wasmPath)) {
62
+ refId = emitted.get(wasmPath);
63
+ } else {
64
+ const source = readFileSync(wasmPath);
65
+ refId = this.emitFile({ type: "asset", name: nodePath.basename(wasmPath), source });
66
+ emitted.set(wasmPath, refId);
67
+ }
68
+
69
+ result = result.replace(
70
+ full,
71
+ `new URL(import.meta.ROLLUP_FILE_URL_${refId}, import.meta.url)`
72
+ );
73
+ changed = true;
74
+ }
75
+
76
+ return changed ? { code: result, map: null } : null;
77
+ },
78
+ };
79
+ }
package/vite.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Vite plugin for SomMark.
3
+ *
4
+ * @example
5
+ * // vite.config.js
6
+ * import { sommarkVite } from "sommark/vite";
7
+ * export default defineConfig({ plugins: [sommarkVite()] });
8
+ */
9
+ export function sommarkVite() {
10
+ return {
11
+ name: "sommark",
12
+ config() {
13
+ return {
14
+ optimizeDeps: {
15
+ exclude: ["quickjs-emscripten"],
16
+ },
17
+ };
18
+ },
19
+ };
20
+ }