@hunghg255/napi-zig-cli 1.7.0 → 1.9.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/README.md CHANGED
@@ -40,7 +40,7 @@ binary for the platform first, then `wasm32-wasi-threads`, then
40
40
  itself; `"napi": { "wasm": { "initialMemory": ..., "maxMemory": ... } }`
41
41
  (bytes) still overrides them, but is no longer needed (1.7).
42
42
 
43
- Browsers and bundlers (1.7, experimental): each wasm package also carries
43
+ Browsers and bundlers (1.7; stable from 1.8): each wasm package also carries
44
44
  `<name>.<target>.browser.mjs`, an ES module that fetches the `.wasm` with
45
45
  `new URL('./<file>.wasm', import.meta.url)` (which webpack, Vite, Rollup and
46
46
  Parcel turn into an asset), instantiates it on import and exports
@@ -54,8 +54,10 @@ a module worker (`<name>.<target>.browser-worker.mjs`), needs
54
54
  `SharedArrayBuffer` - serve the page with `Cross-Origin-Opener-Policy:
55
55
  same-origin` and `Cross-Origin-Embedder-Policy: require-corp` - and should
56
56
  be imported in a worker, as the page's main thread may not block. New
57
- projects get a `browser.mjs` that re-exports the `wasm32-wasi` package and
58
- an `exports` map that keeps Node.js on `index.js`. Tested in Chromium
57
+ projects get a `browser.mjs` that re-exports a wasm package and an
58
+ `exports` map that keeps Node.js on `index.js`; from 1.8 `artifacts` points
59
+ it at `wasm32-wasi` if that is in `napi.targets`, else at
60
+ `wasm32-wasi-threads` (a line edited by hand is left alone). Tested in Chromium
59
61
  without a bundler; no bundler is tested yet (esbuild does not handle
60
62
  `new Worker(new URL(...))`, so the threaded flavour needs a bundler that
61
63
  does, or `load({ onCreateWorker })`).
package/lib/artifacts.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs'
1
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
2
2
  import { join, resolve } from 'node:path'
3
3
  import { loadProject } from './project.mjs'
4
4
  import { binaryName, findTarget } from './targets.mjs'
@@ -83,8 +83,41 @@ export function artifacts(options) {
83
83
  ...Object.fromEntries(targets.map((id) => [platformPackageName(pkg, id), pkg.version])),
84
84
  }
85
85
  writeFileSync(project.packagePath, JSON.stringify(pkg, null, 2) + '\n')
86
+ const browser = updateBrowserEntry(project)
87
+ if (browser) console.log(`napi-zig: browser.mjs now loads ${browser}`)
86
88
 
87
89
  for (const dir of created) console.log(`napi-zig: prepared ${dir}`)
88
90
  for (const binary of missing) console.warn(`napi-zig: warning: ${binary} has not been built yet`)
89
91
  return { created, missing }
90
92
  }
93
+
94
+ /**
95
+ * The WebAssembly flavour the browser entry should load, from
96
+ * "napi.targets": `wasm32-wasi` if it is built, else `wasm32-wasi-threads`
97
+ * (which needs a cross-origin isolated page), else none. (1.8)
98
+ */
99
+ export function browserFlavour(targets) {
100
+ if (targets.includes('wasm32-wasi')) return 'wasm32-wasi'
101
+ if (targets.includes('wasm32-wasi-threads')) return 'wasm32-wasi-threads'
102
+ return null
103
+ }
104
+
105
+ /**
106
+ * Points the project's browser.mjs, as `napi-zig new` wrote it (a re-export
107
+ * of one wasm package), at the flavour in "napi.targets". A browser.mjs
108
+ * changed by hand is left alone. Returns the package it now loads, if it
109
+ * changed.
110
+ */
111
+ export function updateBrowserEntry(project) {
112
+ const path = join(project.dir, 'browser.mjs')
113
+ const flavour = browserFlavour(project.targets)
114
+ if (!flavour || !existsSync(path)) return null
115
+ const text = readFileSync(path, 'utf8')
116
+ const escaped = project.pkg.name.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')
117
+ const generated = new RegExp(`^export \\{ default, load \\} from '${escaped}-(wasm32-wasi(?:-threads)?)'$`, 'm')
118
+ const match = text.match(generated)
119
+ if (!match || match[1] === flavour) return null
120
+ const target = platformPackageName(project.pkg, flavour)
121
+ writeFileSync(path, text.replace(generated, `export { default, load } from '${target}'`))
122
+ return target
123
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hunghg255/napi-zig-cli",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "Command line tool for napi-zig: scaffold, build, cross-compile and publish Node.js native addons written in Zig",
5
5
  "keywords": [
6
6
  "napi",
@@ -5,8 +5,8 @@
5
5
  //
6
6
  // import addon from '{{PACKAGE_NAME}}'
7
7
  //
8
- // This needs "wasm32-wasi" in "napi.targets" (built and published with the
9
- // rest). The threaded build, "wasm32-wasi-threads", needs a cross-origin
10
- // isolated page and is best loaded in a worker: import
11
- // '{{PACKAGE_NAME}}-wasm32-wasi-threads' there (or point this file at it).
8
+ // It loads the WebAssembly package of "napi.targets": `napi-zig artifacts`
9
+ // points the line below at "wasm32-wasi" if that is a target, else at
10
+ // "wasm32-wasi-threads" (which needs a cross-origin isolated page and is
11
+ // best loaded in a worker). Edit the line by hand and it is left alone.
12
12
  export { default, load } from '{{PACKAGE_NAME}}-wasm32-wasi'
@@ -32,7 +32,7 @@
32
32
  "test": "node --test"
33
33
  },
34
34
  "devDependencies": {
35
- "@hunghg255/napi-zig-cli": "^1.7.0"
35
+ "@hunghg255/napi-zig-cli": "^1.9.0"
36
36
  },
37
37
  "license": "MIT"
38
38
  }