Skip to content

Commit

Permalink
web: Don't unnecessarily duplicate the module for single WASM builds
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhjacobs committed Jan 14, 2025
1 parent 0f4ecc7 commit c7dd2e9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion web/packages/core/src/internal/builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuffleInstanceBuilder } from "../../dist/ruffle_web";
import type { RuffleInstanceBuilder } from "../../dist/ruffle_web-wasm_extensions";
import { BaseLoadOptions, Duration, SecsDuration } from "../public/config";

/**
Expand Down
2 changes: 1 addition & 1 deletion web/packages/core/src/internal/player/inner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuffleHandle, ZipWriter } from "../../../dist/ruffle_web";
import type { RuffleHandle, ZipWriter } from "../../../dist/ruffle_web-wasm_extensions";
import {
AutoPlay,
ContextMenu,
Expand Down
15 changes: 11 additions & 4 deletions web/packages/core/src/load-ruffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
signExtensions,
referenceTypes,
} from "wasm-feature-detect";
import type { RuffleInstanceBuilder, ZipWriter } from "../dist/ruffle_web";
import type {
RuffleInstanceBuilder,
ZipWriter,
} from "../dist/ruffle_web-wasm_extensions";
import { setPolyfillsOnLoad } from "./js-polyfills";

import { internalSourceApi } from "./internal/internal-source-api";
Expand Down Expand Up @@ -45,7 +48,10 @@ async function fetchRuffle(
])
).every(Boolean);

if (!extensionsSupported) {
// @ts-expect-error TS2367 %FALLBACK_WASM% gets replaced in set_version.ts.
// %FALLBACK_WASM% is "ruffle_web" if this is a dual-wasm build.
// We don't say we're falling back if we have only an extension build.
if (!extensionsSupported && "%FALLBACK_WASM%" === "ruffle_web") {
console.log(
"Some WebAssembly extensions are NOT available, falling back to the vanilla WebAssembly module",
);
Expand All @@ -63,11 +69,12 @@ async function fetchRuffle(
ZipWriter,
} = await (extensionsSupported
? import("../dist/ruffle_web-wasm_extensions")
: import("../dist/ruffle_web"));
: // @ts-expect-error TS2307 TypeScript compiler is trying to do the import.
import("../dist/%FALLBACK_WASM%"));
let response;
const wasmUrl = extensionsSupported
? new URL("../dist/ruffle_web-wasm_extensions_bg.wasm", import.meta.url)
: new URL("../dist/ruffle_web_bg.wasm", import.meta.url);
: new URL("../dist/%FALLBACK_WASM%_bg.wasm", import.meta.url);
const wasmResponse = await fetch(wasmUrl);
// The Pale Moon browser lacks full support for ReadableStream.
// However, ReadableStream itself is defined.
Expand Down
9 changes: 0 additions & 9 deletions web/packages/core/tools/build_wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ function buildWasm(
});
}
}
function copyStandIn(from: string, to: string) {
const suffixes = [`_bg.wasm`, `_bg.wasm.d.ts`, `.js`, `.d.ts`];
console.log(`Copying ${from} as a stand-in for ${to}...`);
for (const suffix of suffixes) {
copyFileSync(`dist/${from}${suffix}`, `dist/${to}${suffix}`);
}
}
function detectWasmOpt() {
try {
execFileSync("wasm-opt", ["--version"]);
Expand Down Expand Up @@ -176,6 +169,4 @@ buildWasm(
);
if (buildWasmMvp) {
buildWasm("web-vanilla-wasm", "ruffle_web", hasWasmOpt, false, wasmSource);
} else {
copyStandIn("ruffle_web-wasm_extensions", "ruffle_web");
}
15 changes: 14 additions & 1 deletion web/packages/core/tools/set_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ if (process.env["ENABLE_VERSION_SEAL"] === "true") {
}
}

const fallbackWasmName =
process.env["BUILD_WASM_MVP"] === "true"
? "ruffle_web"
: "ruffle_web-wasm_extensions";

const options = {
files: "dist/**",
from: [
Expand All @@ -72,8 +77,16 @@ const options = {
/%VERSION_CHANNEL%/g,
/%BUILD_DATE%/g,
/%COMMIT_HASH%/g,
/%FALLBACK_WASM%/g,
],
to: [
versionNumber,
versionName,
versionChannel,
buildDate,
commitHash,
fallbackWasmName,
],
to: [versionNumber, versionName, versionChannel, buildDate, commitHash],
};

replaceInFileSync(options);

0 comments on commit c7dd2e9

Please sign in to comment.