Implement JQHTML function cache ID system and fix bundle compilation Implement underscore prefix for system tables Fix JS syntax linter to support decorators and grant exception to Task system SPA: Update planning docs and wishlists with remaining features SPA: Document Navigation API abandonment and future enhancements Implement SPA browser integration with History API (Phase 1) Convert contacts view page to SPA action Convert clients pages to SPA actions and document conversion procedure SPA: Merge GET parameters and update documentation Implement SPA route URL generation in JavaScript and PHP Implement SPA bootstrap controller architecture Add SPA routing manual page (rsx:man spa) Add SPA routing documentation to CLAUDE.md Phase 4 Complete: Client-side SPA routing implementation Update get_routes() consumers for unified route structure Complete SPA Phase 3: PHP-side route type detection and is_spa flag Restore unified routes structure and Manifest_Query class Refactor route indexing and add SPA infrastructure Phase 3 Complete: SPA route registration in manifest Implement SPA Phase 2: Extract router code and test decorators Rename Jqhtml_Component to Component and complete SPA foundation setup 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
147 lines
4.4 KiB
JavaScript
147 lines
4.4 KiB
JavaScript
/*
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
Author Tobias Koppers @sokra
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
const RuntimeModule = require("../RuntimeModule");
|
|
const Template = require("../Template");
|
|
|
|
/** @typedef {import("../Chunk")} Chunk */
|
|
/** @typedef {import("../Compilation")} Compilation */
|
|
|
|
/**
|
|
* @typedef {object} AsyncWasmLoadingRuntimeModuleOptions
|
|
* @property {((wasmModuleSrcPath: string) => string)=} generateBeforeLoadBinaryCode
|
|
* @property {(wasmModuleSrcPath: string) => string} generateLoadBinaryCode
|
|
* @property {(() => string)=} generateBeforeInstantiateStreaming
|
|
* @property {boolean} supportsStreaming
|
|
*/
|
|
|
|
class AsyncWasmLoadingRuntimeModule extends RuntimeModule {
|
|
/**
|
|
* @param {AsyncWasmLoadingRuntimeModuleOptions} options options
|
|
*/
|
|
constructor({
|
|
generateLoadBinaryCode,
|
|
generateBeforeLoadBinaryCode,
|
|
generateBeforeInstantiateStreaming,
|
|
supportsStreaming
|
|
}) {
|
|
super("wasm loading", RuntimeModule.STAGE_NORMAL);
|
|
this.generateLoadBinaryCode = generateLoadBinaryCode;
|
|
this.generateBeforeLoadBinaryCode = generateBeforeLoadBinaryCode;
|
|
this.generateBeforeInstantiateStreaming =
|
|
generateBeforeInstantiateStreaming;
|
|
this.supportsStreaming = supportsStreaming;
|
|
}
|
|
|
|
/**
|
|
* @returns {string | null} runtime code
|
|
*/
|
|
generate() {
|
|
const compilation = /** @type {Compilation} */ (this.compilation);
|
|
const chunk = /** @type {Chunk} */ (this.chunk);
|
|
const { outputOptions, runtimeTemplate } = compilation;
|
|
const fn = RuntimeGlobals.instantiateWasm;
|
|
const wasmModuleSrcPath = compilation.getPath(
|
|
JSON.stringify(outputOptions.webassemblyModuleFilename),
|
|
{
|
|
hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
|
|
hashWithLength: (length) =>
|
|
`" + ${RuntimeGlobals.getFullHash}}().slice(0, ${length}) + "`,
|
|
module: {
|
|
id: '" + wasmModuleId + "',
|
|
hash: '" + wasmModuleHash + "',
|
|
hashWithLength(length) {
|
|
return `" + wasmModuleHash.slice(0, ${length}) + "`;
|
|
}
|
|
},
|
|
runtime: chunk.runtime
|
|
}
|
|
);
|
|
|
|
const loader = this.generateLoadBinaryCode(wasmModuleSrcPath);
|
|
const fallback = [
|
|
`.then(${runtimeTemplate.returningFunction("x.arrayBuffer()", "x")})`,
|
|
`.then(${runtimeTemplate.returningFunction(
|
|
"WebAssembly.instantiate(bytes, importsObj)",
|
|
"bytes"
|
|
)})`,
|
|
`.then(${runtimeTemplate.returningFunction(
|
|
"Object.assign(exports, res.instance.exports)",
|
|
"res"
|
|
)})`
|
|
];
|
|
const getStreaming = () => {
|
|
/**
|
|
* @param {string[]} text text
|
|
* @returns {string} merged text
|
|
*/
|
|
const concat = (...text) => text.join("");
|
|
return [
|
|
this.generateBeforeLoadBinaryCode
|
|
? this.generateBeforeLoadBinaryCode(wasmModuleSrcPath)
|
|
: "",
|
|
`var req = ${loader};`,
|
|
`var fallback = ${runtimeTemplate.returningFunction(
|
|
Template.asString(["req", Template.indent(fallback)])
|
|
)};`,
|
|
concat(
|
|
"return req.then(",
|
|
runtimeTemplate.basicFunction("res", [
|
|
'if (typeof WebAssembly.instantiateStreaming === "function") {',
|
|
Template.indent(
|
|
this.generateBeforeInstantiateStreaming
|
|
? this.generateBeforeInstantiateStreaming()
|
|
: ""
|
|
),
|
|
Template.indent([
|
|
"return WebAssembly.instantiateStreaming(res, importsObj)",
|
|
Template.indent([
|
|
".then(",
|
|
Template.indent([
|
|
`${runtimeTemplate.returningFunction(
|
|
"Object.assign(exports, res.instance.exports)",
|
|
"res"
|
|
)},`,
|
|
runtimeTemplate.basicFunction("e", [
|
|
'if(res.headers.get("Content-Type") !== "application/wasm") {',
|
|
Template.indent([
|
|
'console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\\n", e);',
|
|
"return fallback();"
|
|
]),
|
|
"}",
|
|
"throw e;"
|
|
])
|
|
]),
|
|
");"
|
|
])
|
|
]),
|
|
"}",
|
|
"return fallback();"
|
|
]),
|
|
");"
|
|
)
|
|
];
|
|
};
|
|
|
|
return `${fn} = ${runtimeTemplate.basicFunction(
|
|
"exports, wasmModuleId, wasmModuleHash, importsObj",
|
|
this.supportsStreaming
|
|
? getStreaming()
|
|
: [
|
|
this.generateBeforeLoadBinaryCode
|
|
? this.generateBeforeLoadBinaryCode(wasmModuleSrcPath)
|
|
: "",
|
|
`return ${loader}`,
|
|
`${Template.indent(fallback)};`
|
|
]
|
|
)};`;
|
|
}
|
|
}
|
|
|
|
module.exports = AsyncWasmLoadingRuntimeModule;
|