Skip to content

Commit ec9470c

Browse files
PackageToJS: Fetch module from the default location if init is called without options
1 parent 736db53 commit ec9470c

File tree

13 files changed

+24
-55
lines changed

13 files changed

+24
-55
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ xcuserdata/
99
.vscode
1010
Examples/*/Bundle
1111
Examples/*/package-lock.json
12-
/Package.resolved
12+
Package.resolved

Diff for: Examples/ActorOnWebWorker/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<script type="module">
1010
import { init } from "./Bundle/index.js"
11-
init(fetch(new URL("./Bundle/main.wasm", import.meta.url)));
11+
init();
1212
</script>
1313
<h1>Full-text Search with Actor on Web Worker</h1>
1414

Diff for: Examples/Basic/.gitignore

-5
This file was deleted.

Diff for: Examples/Basic/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<script type="module">
1010
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
11-
await init(fetch("./.build/plugins/PackageToJS/outputs/Package/main.wasm"));
11+
init();
1212
</script>
1313
</body>
1414

Diff for: Examples/Embedded/.gitignore

-6
This file was deleted.

Diff for: Examples/Embedded/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<script type="module">
1010
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
11-
await init(fetch("./.build/plugins/PackageToJS/outputs/Package/main.wasm"));
11+
init();
1212
</script>
1313
</body>
1414

Diff for: Examples/Multithreading/.gitignore

-8
This file was deleted.

Diff for: Examples/Multithreading/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<body>
3030
<script type="module">
3131
import { init } from "./Bundle/index.js"
32-
init(fetch(new URL("./Bundle/main.wasm", import.meta.url)));
32+
init();
3333
</script>
3434
<h1>Threading Example</h1>
3535
<p>

Diff for: Examples/OffscrenCanvas/.gitignore

-8
This file was deleted.

Diff for: Examples/OffscrenCanvas/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<body>
7171
<script type="module">
7272
import { init } from "./Bundle/index.js"
73-
init(fetch(new URL("./Bundle/main.wasm", import.meta.url)));
73+
init();
7474
</script>
7575
<h1>OffscreenCanvas Example</h1>
7676
<p>

Diff for: Plugins/PackageToJS/Templates/index.d.ts

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
import type { Import, Export } from './instantiate.js'
1+
import type { Export, ModuleSource } from './instantiate.js'
22

33
export type Options = {
44
/**
5-
* The CLI arguments to pass to the WebAssembly module
5+
* The WebAssembly module to instantiate
6+
*
7+
* If not provided, the module will be fetched from the default path.
68
*/
7-
args?: string[]
8-
/* #if USE_SHARED_MEMORY */
9-
/**
10-
* The WebAssembly memory to use (must be 'shared')
11-
*/
12-
memory: WebAssembly.Memory
13-
/* #endif */
9+
module?: ModuleSource
1410
}
1511

1612
/**
17-
* Initialize the given WebAssembly module
13+
* Instantiate and initialize the module
1814
*
19-
* This is a convenience function that creates an instantiator and instantiates the module.
20-
* @param moduleSource - The WebAssembly module to instantiate
21-
* @param imports - The imports to add
22-
* @param options - The options
15+
* This is a convenience function for browser environments.
16+
* If you need a more flexible API, see `instantiate`.
2317
*/
24-
export declare function init(
25-
moduleSource: WebAssembly.Module | ArrayBufferView | ArrayBuffer | Response | PromiseLike<Response>
26-
): Promise<{
18+
export declare function init(options?: Options): Promise<{
2719
instance: WebAssembly.Instance,
2820
exports: Export
2921
}>

Diff for: Plugins/PackageToJS/Templates/index.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { instantiate } from './instantiate.js';
33
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';
44

55
/** @type {import('./index.d').init} */
6-
export async function init(moduleSource) {
7-
const options = await defaultBrowserSetup({
8-
module: moduleSource,
6+
export async function init(options = {}) {
7+
let module = options.module;
8+
if (!module) {
9+
module = fetch(new URL("@PACKAGE_TO_JS_MODULE_PATH@", import.meta.url))
10+
}
11+
const instantiateOptions = await defaultBrowserSetup({
12+
module,
913
/* #if USE_SHARED_MEMORY */
1014
spawnWorker: createDefaultWorkerFactory()
1115
/* #endif */
1216
})
13-
return await instantiate(options);
17+
return await instantiate(instantiateOptions);
1418
}

Diff for: Sources/JavaScriptKit/Documentation.docc/Tutorials/Hello-World/Resources/hello-world-2-2-index-html.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title>Swift Web App</title>
66
<script type="module">
77
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
8-
await init(fetch("./.build/plugins/PackageToJS/outputs/Package/main.wasm"));
8+
init();
99
</script>
1010
</head>
1111
<body>

0 commit comments

Comments
 (0)