@napi-rs/cli 3.7.3 → 3.8.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.
Files changed (51) hide show
  1. package/README.md +33 -0
  2. package/cli.mjs +9 -12
  3. package/dist/cli.js +9277 -1313
  4. package/dist/index.cjs +9266 -1302
  5. package/dist/index.d.cts +176 -158
  6. package/dist/index.d.ts +176 -158
  7. package/dist/index.js +9277 -1313
  8. package/docs/artifacts.md +33 -0
  9. package/docs/build.md +58 -0
  10. package/docs/create-npm-dirs.md +32 -0
  11. package/docs/new.md +39 -0
  12. package/docs/pre-publish.md +37 -0
  13. package/docs/rename.md +37 -0
  14. package/docs/universalize.md +31 -0
  15. package/docs/version.md +31 -0
  16. package/docs/wasi.md +161 -0
  17. package/package.json +11 -10
  18. package/src/api/__tests__/__snapshots__/templates.spec.ts.md +1019 -114
  19. package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
  20. package/src/api/__tests__/artifacts.spec.ts +17 -0
  21. package/src/api/__tests__/create-npm-dirs.spec.ts +14 -15
  22. package/src/api/__tests__/new.spec.ts +41 -5
  23. package/src/api/artifacts.ts +934 -108
  24. package/src/api/build.ts +1325 -194
  25. package/src/api/create-npm-dirs.ts +472 -101
  26. package/src/api/new.ts +354 -35
  27. package/src/api/pre-publish.ts +2845 -99
  28. package/src/api/rename.ts +1638 -124
  29. package/src/api/templates/js-binding.ts +216 -38
  30. package/src/api/templates/load-wasi-template.ts +1641 -111
  31. package/src/api/templates/wasi-worker-template.ts +32 -3
  32. package/src/commands/rename.ts +8 -3
  33. package/src/def/artifacts.ts +2 -2
  34. package/src/utils/__tests__/__fixtures__/optional-napi-derive/Cargo.toml +3 -0
  35. package/src/utils/__tests__/__fixtures__/optional-napi-derive/main-crate/Cargo.toml +8 -0
  36. package/src/utils/__tests__/__fixtures__/optional-napi-derive/main-crate/src/lib.rs +1 -0
  37. package/src/utils/__tests__/__fixtures__/optional-napi-derive/napi-derive/Cargo.toml +5 -0
  38. package/src/utils/__tests__/__fixtures__/optional-napi-derive/napi-derive/src/lib.rs +1 -0
  39. package/src/utils/__tests__/__fixtures__/optional-napi-derive/with-optional-derive/Cargo.toml +11 -0
  40. package/src/utils/__tests__/__fixtures__/optional-napi-derive/with-optional-derive/src/lib.rs +1 -0
  41. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +2 -2
  42. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  43. package/src/utils/__tests__/metadata.spec.ts +43 -2
  44. package/src/utils/__tests__/misc.spec.ts +124 -3
  45. package/src/utils/__tests__/version.spec.ts +47 -1
  46. package/src/utils/config.ts +15 -2
  47. package/src/utils/metadata.ts +206 -7
  48. package/src/utils/misc.ts +6079 -0
  49. package/src/utils/target.ts +66 -8
  50. package/src/utils/typegen.ts +1223 -63
  51. package/src/utils/version.ts +96 -0
@@ -31,7 +31,7 @@ export const AVAILABLE_TARGETS = [
31
31
  'riscv64gc-unknown-linux-gnu',
32
32
  'powerpc64le-unknown-linux-gnu',
33
33
  's390x-unknown-linux-gnu',
34
- 'wasm32-wasi-preview1-threads',
34
+ 'wasm32-wasip1',
35
35
  'wasm32-wasip1-threads',
36
36
  ] as const
37
37
 
@@ -111,6 +111,51 @@ export interface Target {
111
111
  abi: string | null
112
112
  }
113
113
 
114
+ export type WasiFlavor = 'single' | 'threads'
115
+
116
+ export interface WasiTarget {
117
+ canonicalTriple: string
118
+ flavor: WasiFlavor
119
+ platformArchABI: string
120
+ }
121
+
122
+ /**
123
+ * Resolve historical WASI spellings to one build target and one artifact
124
+ * identity. `wasm32-wasi` historically produced the threaded package, so it
125
+ * must never be inferred as threadless merely because its name lacks the
126
+ * `-threads` suffix.
127
+ */
128
+ export function getWasiTarget(
129
+ target: string | Pick<Target, 'triple'>,
130
+ ): WasiTarget | undefined {
131
+ const triple = typeof target === 'string' ? target : target.triple
132
+
133
+ switch (triple) {
134
+ case 'wasm32-wasi':
135
+ case 'wasm32-wasi-preview1-threads':
136
+ case 'wasm32-wasip1-threads':
137
+ return {
138
+ canonicalTriple: 'wasm32-wasip1-threads',
139
+ flavor: 'threads',
140
+ platformArchABI: 'wasm32-wasi',
141
+ }
142
+ case 'wasm32-wasip1':
143
+ return {
144
+ canonicalTriple: 'wasm32-wasip1',
145
+ flavor: 'single',
146
+ platformArchABI: 'wasm32-wasip1',
147
+ }
148
+ default:
149
+ return
150
+ }
151
+ }
152
+
153
+ export function wasiTargetHasThreads(
154
+ target: string | Pick<Target, 'triple'>,
155
+ ): boolean {
156
+ return getWasiTarget(target)?.flavor === 'threads'
157
+ }
158
+
114
159
  /**
115
160
  * A triple is a specific format for specifying a target architecture.
116
161
  * Triples may be referred to as a target triple which is the architecture for the artifact produced, and the host triple which is the architecture that the compiler is running on.
@@ -122,19 +167,21 @@ export interface Target {
122
167
  * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
123
168
  */
124
169
  export function parseTriple(rawTriple: string): Target {
125
- if (
126
- rawTriple === 'wasm32-wasi' ||
127
- rawTriple === 'wasm32-wasi-preview1-threads' ||
128
- rawTriple.startsWith('wasm32-wasip')
129
- ) {
170
+ const wasiTarget = getWasiTarget(rawTriple)
171
+ if (wasiTarget) {
130
172
  return {
131
- triple: rawTriple,
132
- platformArchABI: 'wasm32-wasi',
173
+ triple: wasiTarget.canonicalTriple,
174
+ platformArchABI: wasiTarget.platformArchABI,
133
175
  platform: 'wasi',
134
176
  arch: 'wasm32',
135
177
  abi: 'wasi',
136
178
  }
137
179
  }
180
+ if (/^wasm32-(?:wasip|wasi(?:-|$))/.test(rawTriple)) {
181
+ throw new TypeError(
182
+ `Unsupported WASI target ${rawTriple}. Supported targets are wasm32-wasip1, wasm32-wasip1-threads, wasm32-wasi, and wasm32-wasi-preview1-threads.`,
183
+ )
184
+ }
138
185
  const triple = rawTriple.endsWith('eabi')
139
186
  ? `${rawTriple.slice(0, -4)}-eabi`
140
187
  : rawTriple
@@ -188,6 +235,17 @@ export function getTargetLinker(target: string): string | undefined {
188
235
  return TARGET_LINKER[target]
189
236
  }
190
237
 
238
+ /**
239
+ * Loader-file suffix for a WASI flavor, derived from its `platformArchABI`:
240
+ * the legacy threaded flavor keeps the historical `wasi` stem
241
+ * (`<binaryName>.wasi.cjs`, `<binaryName>.wasi-browser.js`), while each
242
+ * distinctly named non-threaded flavor derives its own
243
+ * (`<binaryName>.wasip1.cjs`, `<binaryName>.wasip1-browser.js`, ...).
244
+ */
245
+ export function wasiLoaderSuffix(platformArchABI: string): string {
246
+ return platformArchABI.replace(/^wasm32-/, '')
247
+ }
248
+
191
249
  export function targetToEnvVar(target: string): string {
192
250
  return target.replace(/-/g, '_').toUpperCase()
193
251
  }