Skip to content

Commit 54a749b

Browse files
authored
chore: export InitInput and allow Promise (#726)
* allows Promise as input, so we can ```ts await init(fetch("yoga.wasm")); // instead of await init(await fetch("yoga.wasm")); ``` * exports `InitInput` type * adds `./yoga.wasm` in `package.json` exports
1 parent 9377f70 commit 54a749b

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
],
2121
"exports": {
2222
"./package.json": "./package.json",
23+
"./yoga.wasm": "./yoga.wasm",
2324
".": {
2425
"import": "./dist/index.js",
2526
"require": "./dist/index.cjs"

src/yoga.external.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@ const yogaPromise: Promise<Yoga> = new Promise((resolve, reject) => {
1616
})
1717

1818
export type InitInput =
19-
| RequestInfo
19+
| string
20+
| Request
2021
| URL
2122
| Response
2223
| BufferSource
2324
| Buffer
2425
| WebAssembly.Module
26+
| Promise<Response | BufferSource | Buffer | WebAssembly.Module>
2527

2628
async function loadWasm(
2729
input: InitInput,
2830
imports: WebAssembly.Imports
2931
): Promise<WebAssembly.WebAssemblyInstantiatedSource> {
30-
let source: Response | BufferSource | Buffer | WebAssembly.Module = input
32+
let source: Response | BufferSource | Buffer | WebAssembly.Module
3133

3234
if (
33-
typeof source === 'string' ||
34-
(typeof Request === 'function' && source instanceof Request) ||
35-
(typeof URL === 'function' && source instanceof URL)
35+
typeof input === 'string' ||
36+
(typeof Request === 'function' && input instanceof Request) ||
37+
(typeof URL === 'function' && input instanceof URL)
3638
) {
37-
source = await fetch(source)
39+
source = await fetch(input)
40+
} else {
41+
source = await input
3842
}
3943

4044
if (typeof Response === 'function' && source instanceof Response) {

src/yoga.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type Yoga } from 'yoga-layout/load'
22
import { type Node } from 'yoga-layout'
33
import { type InitInput } from './yoga.external.js'
44

5-
export { Yoga as TYoga, Node as YogaNode }
5+
export { Yoga as TYoga, Node as YogaNode, type InitInput }
66

77
export function init(input: InitInput) {
88
if (process.env.SATORI_STANDALONE === '1') {

0 commit comments

Comments
 (0)