Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PackageToJS: Use the actual wasm filename in the final product #303

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ xcuserdata/
.vscode
Examples/*/Bundle
Examples/*/package-lock.json
/Package.resolved
Package.resolved
2 changes: 1 addition & 1 deletion Examples/ActorOnWebWorker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<script type="module">
import { init } from "./Bundle/index.js"
init(fetch(new URL("./Bundle/main.wasm", import.meta.url)));
init();
</script>
<h1>Full-text Search with Actor on Web Worker</h1>

Expand Down
5 changes: 0 additions & 5 deletions Examples/Basic/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion Examples/Basic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<script type="module">
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
await init(fetch("./.build/plugins/PackageToJS/outputs/Package/main.wasm"));
init();
</script>
</body>

Expand Down
6 changes: 0 additions & 6 deletions Examples/Embedded/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion Examples/Embedded/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<script type="module">
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
await init(fetch("./.build/plugins/PackageToJS/outputs/Package/main.wasm"));
init();
</script>
</body>

Expand Down
8 changes: 0 additions & 8 deletions Examples/Multithreading/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion Examples/Multithreading/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<body>
<script type="module">
import { init } from "./Bundle/index.js"
init(fetch(new URL("./Bundle/main.wasm", import.meta.url)));
init();
</script>
<h1>Threading Example</h1>
<p>
Expand Down
8 changes: 0 additions & 8 deletions Examples/OffscrenCanvas/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion Examples/OffscrenCanvas/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<body>
<script type="module">
import { init } from "./Bundle/index.js"
init(fetch(new URL("./Bundle/main.wasm", import.meta.url)));
init();
</script>
<h1>OffscreenCanvas Example</h1>
<p>
Expand Down
4 changes: 3 additions & 1 deletion Plugins/PackageToJS/Sources/PackageToJS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ struct PackagingPlanner {
/// The directory for intermediate files
let intermediatesDir: BuildPath
/// The filename of the .wasm file
let wasmFilename = "main.wasm"
let wasmFilename: String
/// The path to the .wasm product artifact
let wasmProductArtifact: BuildPath
/// The build configuration
Expand All @@ -374,6 +374,7 @@ struct PackagingPlanner {
selfPackageDir: BuildPath,
outputDir: BuildPath,
wasmProductArtifact: BuildPath,
wasmFilename: String,
configuration: String,
triple: String,
selfPath: BuildPath = BuildPath(absolute: #filePath),
Expand All @@ -384,6 +385,7 @@ struct PackagingPlanner {
self.selfPackageDir = selfPackageDir
self.outputDir = outputDir
self.intermediatesDir = intermediatesDir
self.wasmFilename = wasmFilename
self.selfPath = selfPath
self.wasmProductArtifact = wasmProductArtifact
self.configuration = configuration
Expand Down
17 changes: 14 additions & 3 deletions Plugins/PackageToJS/Sources/PackageToJSPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ struct PackageToJSPlugin: CommandPlugin {
)
let planner = PackagingPlanner(
options: buildOptions.packageOptions, context: context, selfPackage: selfPackage,
outputDir: outputDir, wasmProductArtifact: productArtifact)
outputDir: outputDir, wasmProductArtifact: productArtifact,
wasmFilename: productArtifact.lastPathComponent
)
let rootTask = try planner.planBuild(
make: &make, buildOptions: buildOptions)
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
Expand Down Expand Up @@ -193,7 +195,14 @@ struct PackageToJSPlugin: CommandPlugin {
)
let planner = PackagingPlanner(
options: testOptions.packageOptions, context: context, selfPackage: selfPackage,
outputDir: outputDir, wasmProductArtifact: productArtifact)
outputDir: outputDir, wasmProductArtifact: productArtifact,
// If the product artifact doesn't have a .wasm extension, add it
// to deliver it with the correct MIME type when serving the test
// files for browser tests.
wasmFilename: productArtifact.lastPathComponent.hasSuffix(".wasm")
? productArtifact.lastPathComponent
: productArtifact.lastPathComponent + ".wasm"
)
let (rootTask, binDir) = try planner.planTestBuild(
make: &make)
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
Expand Down Expand Up @@ -486,7 +495,8 @@ extension PackagingPlanner {
context: PluginContext,
selfPackage: Package,
outputDir: URL,
wasmProductArtifact: URL
wasmProductArtifact: URL,
wasmFilename: String
) {
let outputBaseName = outputDir.lastPathComponent
let (configuration, triple) = PackageToJS.deriveBuildConfiguration(wasmProductArtifact: wasmProductArtifact)
Expand All @@ -498,6 +508,7 @@ extension PackagingPlanner {
selfPackageDir: BuildPath(absolute: selfPackage.directoryURL.path),
outputDir: BuildPath(absolute: outputDir.path),
wasmProductArtifact: BuildPath(absolute: wasmProductArtifact.path),
wasmFilename: wasmFilename,
configuration: configuration,
triple: triple,
system: system
Expand Down
26 changes: 9 additions & 17 deletions Plugins/PackageToJS/Templates/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import type { Import, Export } from './instantiate.js'
import type { Export, ModuleSource } from './instantiate.js'

export type Options = {
/**
* The CLI arguments to pass to the WebAssembly module
* The WebAssembly module to instantiate
*
* If not provided, the module will be fetched from the default path.
*/
args?: string[]
/* #if USE_SHARED_MEMORY */
/**
* The WebAssembly memory to use (must be 'shared')
*/
memory: WebAssembly.Memory
/* #endif */
module?: ModuleSource
}

/**
* Initialize the given WebAssembly module
* Instantiate and initialize the module
*
* This is a convenience function that creates an instantiator and instantiates the module.
* @param moduleSource - The WebAssembly module to instantiate
* @param imports - The imports to add
* @param options - The options
* This is a convenience function for browser environments.
* If you need a more flexible API, see `instantiate`.
*/
export declare function init(
moduleSource: WebAssembly.Module | ArrayBufferView | ArrayBuffer | Response | PromiseLike<Response>
): Promise<{
export declare function init(options?: Options): Promise<{
instance: WebAssembly.Instance,
exports: Export
}>
12 changes: 8 additions & 4 deletions Plugins/PackageToJS/Templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { instantiate } from './instantiate.js';
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';

/** @type {import('./index.d').init} */
export async function init(moduleSource) {
const options = await defaultBrowserSetup({
module: moduleSource,
export async function init(options = {}) {
let module = options.module;
if (!module) {
module = fetch(new URL("@PACKAGE_TO_JS_MODULE_PATH@", import.meta.url))
}
const instantiateOptions = await defaultBrowserSetup({
module,
/* #if USE_SHARED_MEMORY */
spawnWorker: createDefaultWorkerFactory()
/* #endif */
})
return await instantiate(options);
return await instantiate(instantiateOptions);
}
3 changes: 2 additions & 1 deletion Plugins/PackageToJS/Templates/test.browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<body>
<script type="module">
import { testBrowserInPage } from "./test.js";
import { MODULE_PATH } from "./instantiate.js";
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory /* #endif */} from './platforms/browser.js';

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

const processInfo = await fetch("/process-info.json").then((response) => response.json());
const options = await defaultBrowserSetup({
module: await fetch("./main.wasm"),
module: await fetch(new URL(MODULE_PATH, import.meta.url)),
args: processInfo.args,
onStdoutLine: (line) => {
console.log(line);
Expand Down
2 changes: 1 addition & 1 deletion Plugins/PackageToJS/Tests/ExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ extension Trait where Self == ConditionTrait {
let process = Process()
process.executableURL = llvmCov
let profdata = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/default.profdata")
let wasm = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/main.wasm")
let wasm = packageDir.appending(path: ".build/plugins/PackageToJS/outputs/PackageTests/TestingPackageTests.wasm")
process.arguments = ["report", "-instr-profile", profdata.path, wasm.path]
process.standardOutput = FileHandle.nullDevice
try process.run()
Expand Down
2 changes: 2 additions & 0 deletions Plugins/PackageToJS/Tests/PackagingPlannerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Testing
selfPackageDir: BuildPath(prefix: "SELF_PACKAGE"),
outputDir: BuildPath(prefix: "OUTPUT"),
wasmProductArtifact: BuildPath(prefix: "WASM_PRODUCT_ARTIFACT"),
wasmFilename: "main.wasm",
configuration: configuration,
triple: "wasm32-unknown-wasi",
selfPath: BuildPath(prefix: "PLANNER_SOURCE_PATH"),
Expand Down Expand Up @@ -81,6 +82,7 @@ import Testing
selfPackageDir: BuildPath(prefix: "SELF_PACKAGE"),
outputDir: BuildPath(prefix: "OUTPUT"),
wasmProductArtifact: BuildPath(prefix: "WASM_PRODUCT_ARTIFACT"),
wasmFilename: "main.wasm",
configuration: "debug",
triple: "wasm32-unknown-wasi",
selfPath: BuildPath(prefix: "PLANNER_SOURCE_PATH"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>Swift Web App</title>
<script type="module">
import { init } from "./.build/plugins/PackageToJS/outputs/Package/index.js";
await init(fetch("./.build/plugins/PackageToJS/outputs/Package/main.wasm"));
init();
</script>
</head>
<body>
Expand Down