Skip to content

Commit 1a33c51

Browse files
Merge pull request #303 from swiftwasm/yt/module-name
PackageToJS: Use the actual wasm filename in the final product
2 parents 736db53 + 81fe6c8 commit 1a33c51

File tree

18 files changed

+46
-61
lines changed

18 files changed

+46
-61
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/Sources/PackageToJS.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ struct PackagingPlanner {
357357
/// The directory for intermediate files
358358
let intermediatesDir: BuildPath
359359
/// The filename of the .wasm file
360-
let wasmFilename = "main.wasm"
360+
let wasmFilename: String
361361
/// The path to the .wasm product artifact
362362
let wasmProductArtifact: BuildPath
363363
/// The build configuration
@@ -374,6 +374,7 @@ struct PackagingPlanner {
374374
selfPackageDir: BuildPath,
375375
outputDir: BuildPath,
376376
wasmProductArtifact: BuildPath,
377+
wasmFilename: String,
377378
configuration: String,
378379
triple: String,
379380
selfPath: BuildPath = BuildPath(absolute: #filePath),
@@ -384,6 +385,7 @@ struct PackagingPlanner {
384385
self.selfPackageDir = selfPackageDir
385386
self.outputDir = outputDir
386387
self.intermediatesDir = intermediatesDir
388+
self.wasmFilename = wasmFilename
387389
self.selfPath = selfPath
388390
self.wasmProductArtifact = wasmProductArtifact
389391
self.configuration = configuration

Diff for: Plugins/PackageToJS/Sources/PackageToJSPlugin.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ struct PackageToJSPlugin: CommandPlugin {
119119
)
120120
let planner = PackagingPlanner(
121121
options: buildOptions.packageOptions, context: context, selfPackage: selfPackage,
122-
outputDir: outputDir, wasmProductArtifact: productArtifact)
122+
outputDir: outputDir, wasmProductArtifact: productArtifact,
123+
wasmFilename: productArtifact.lastPathComponent
124+
)
123125
let rootTask = try planner.planBuild(
124126
make: &make, buildOptions: buildOptions)
125127
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
@@ -193,7 +195,14 @@ struct PackageToJSPlugin: CommandPlugin {
193195
)
194196
let planner = PackagingPlanner(
195197
options: testOptions.packageOptions, context: context, selfPackage: selfPackage,
196-
outputDir: outputDir, wasmProductArtifact: productArtifact)
198+
outputDir: outputDir, wasmProductArtifact: productArtifact,
199+
// If the product artifact doesn't have a .wasm extension, add it
200+
// to deliver it with the correct MIME type when serving the test
201+
// files for browser tests.
202+
wasmFilename: productArtifact.lastPathComponent.hasSuffix(".wasm")
203+
? productArtifact.lastPathComponent
204+
: productArtifact.lastPathComponent + ".wasm"
205+
)
197206
let (rootTask, binDir) = try planner.planTestBuild(
198207
make: &make)
199208
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
@@ -486,7 +495,8 @@ extension PackagingPlanner {
486495
context: PluginContext,
487496
selfPackage: Package,
488497
outputDir: URL,
489-
wasmProductArtifact: URL
498+
wasmProductArtifact: URL,
499+
wasmFilename: String
490500
) {
491501
let outputBaseName = outputDir.lastPathComponent
492502
let (configuration, triple) = PackageToJS.deriveBuildConfiguration(wasmProductArtifact: wasmProductArtifact)
@@ -498,6 +508,7 @@ extension PackagingPlanner {
498508
selfPackageDir: BuildPath(absolute: selfPackage.directoryURL.path),
499509
outputDir: BuildPath(absolute: outputDir.path),
500510
wasmProductArtifact: BuildPath(absolute: wasmProductArtifact.path),
511+
wasmFilename: wasmFilename,
501512
configuration: configuration,
502513
triple: triple,
503514
system: system

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: Plugins/PackageToJS/Templates/test.browser.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
<body>
55
<script type="module">
66
import { testBrowserInPage } from "./test.js";
7+
import { MODULE_PATH } from "./instantiate.js";
78
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';
89

910
const logElement = document.createElement("pre");
1011
document.body.appendChild(logElement);
1112

1213
const processInfo = await fetch("/process-info.json").then((response) => response.json());
1314
const options = await defaultBrowserSetup({
14-
module: await fetch("./main.wasm"),
15+
module: await fetch(new URL(MODULE_PATH, import.meta.url)),
1516
args: processInfo.args,
1617
onStdoutLine: (line) => {
1718
console.log(line);

Diff for: Plugins/PackageToJS/Tests/ExampleTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ extension Trait where Self == ConditionTrait {
182182
let process = Process()
183183
process.executableURL = llvmCov
184184
let profdata = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/default.profdata")
185-
let wasm = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/main.wasm")
185+
let wasm = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/TestingPackageTests.wasm")
186186
process.arguments = ["report", "-instr-profile", profdata.path, wasm.path]
187187
process.standardOutput = FileHandle.nullDevice
188188
try process.run()

Diff for: Plugins/PackageToJS/Tests/PackagingPlannerTests.swift

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import Testing
5353
selfPackageDir: BuildPath(prefix: "SELF_PACKAGE"),
5454
outputDir: BuildPath(prefix: "OUTPUT"),
5555
wasmProductArtifact: BuildPath(prefix: "WASM_PRODUCT_ARTIFACT"),
56+
wasmFilename: "main.wasm",
5657
configuration: configuration,
5758
triple: "wasm32-unknown-wasi",
5859
selfPath: BuildPath(prefix: "PLANNER_SOURCE_PATH"),
@@ -81,6 +82,7 @@ import Testing
8182
selfPackageDir: BuildPath(prefix: "SELF_PACKAGE"),
8283
outputDir: BuildPath(prefix: "OUTPUT"),
8384
wasmProductArtifact: BuildPath(prefix: "WASM_PRODUCT_ARTIFACT"),
85+
wasmFilename: "main.wasm",
8486
configuration: "debug",
8587
triple: "wasm32-unknown-wasi",
8688
selfPath: BuildPath(prefix: "PLANNER_SOURCE_PATH"),

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)