Skip to content

Commit fa6e321

Browse files
committed
Improve template code to better support node
1. Fixed missing type export (platforms/node.d.ts) - Exported DefaultNodeSetupOptions type that is referenced in index.js 2. Updated index.d.ts to support both platforms - Made init() function accept DefaultNodeSetupOptions for node platform - Made init() function accept Options for browser platform 3. Fixed conditional compilation in node.js - Wrapped getImports() in HAS_IMPORTS conditional (platforms/node.js)
1 parent f60bfd6 commit fa6e321

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Plugins/PackageToJS/Templates/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import type { Exports, Imports, ModuleSource } from './instantiate.js'
2+
/* #if TARGET_PLATFORM_NODE */
3+
import type { DefaultNodeSetupOptions } from './platforms/node.js'
4+
/* #endif */
25

36
export type Options = {
47
/**
@@ -18,10 +21,21 @@ export type Options = {
1821
/**
1922
* Instantiate and initialize the module
2023
*
24+
/* #if TARGET_PLATFORM_NODE */
25+
* This is a convenience function for Node.js environments.
26+
/* #else */
2127
* This is a convenience function for browser environments.
28+
/* #endif */
2229
* If you need a more flexible API, see `instantiate`.
2330
*/
31+
/* #if TARGET_PLATFORM_NODE */
32+
export declare function init(options?: DefaultNodeSetupOptions): Promise<{
33+
instance: WebAssembly.Instance,
34+
exports: Exports
35+
}>
36+
/* #else */
2437
export declare function init(options?: Options): Promise<{
2538
instance: WebAssembly.Instance,
2639
exports: Exports
2740
}>
41+
/* #endif */
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import type { InstantiateOptions } from "../instantiate.js"
22
import type { Worker } from "node:worker_threads"
33

4-
export function defaultNodeSetup(options: {
4+
export type DefaultNodeSetupOptions = {
55
/* #if IS_WASI */
66
args?: string[],
77
/* #endif */
88
onExit?: (code: number) => void,
99
/* #if USE_SHARED_MEMORY */
1010
spawnWorker: (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker,
1111
/* #endif */
12-
}): Promise<InstantiateOptions>
12+
}
13+
14+
export function defaultNodeSetup(options: DefaultNodeSetupOptions): Promise<InstantiateOptions>
1315

1416
export function createDefaultWorkerFactory(preludeScript: string): (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker

Plugins/PackageToJS/Templates/platforms/node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ export async function defaultNodeSetup(options) {
139139

140140
return {
141141
module,
142+
/* #if HAS_IMPORTS */
142143
getImports() { return {} },
144+
/* #endif */
143145
/* #if IS_WASI */
144146
wasi: Object.assign(wasi, {
145147
setInstance(instance) {

0 commit comments

Comments
 (0)