| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author sheo13666q
- */
- "use strict";
- const RuntimeGlobals = require("../RuntimeGlobals");
- const RuntimeModule = require("../RuntimeModule");
- const Template = require("../Template");
- /** @import Compilation from "../Compilation" */
- /**
- * Provides a single object URL (one per base URI) for the worklet bootstrap
- * script. A worklet global scope has no `self` and no `location`; this script
- * sets them up before the emitted chunks — which assume a worker-like scope —
- * are added via `addModule`. Deduping by base URI avoids leaking a new object
- * URL per worklet.
- */
- class GetWorkletBootstrapRuntimeModule extends RuntimeModule {
- constructor() {
- super("get worklet bootstrap", RuntimeModule.STAGE_ATTACH);
- }
- /**
- * Returns true, if the runtime module should get it's own scope.
- * When false, `generate()` must emit complete statements ending with `;`
- * so a following runtime IIFE is not parsed as a call (ASI).
- * @returns {boolean} true, if the runtime module should get it's own scope
- */
- shouldIsolate() {
- return false;
- }
- /**
- * Generates runtime code for this runtime module.
- * @returns {string | null} runtime code
- */
- generate() {
- const { getWorkletBootstrap, baseURI } = RuntimeGlobals;
- const { runtimeTemplate } = /** @type {Compilation} */ (this.compilation);
- // The arrow inside the blob string is fine — it runs in the worklet (always
- // a module scope); the surrounding functions must respect `output.environment`.
- const getter = runtimeTemplate.basicFunction("", [
- `var base = ${baseURI};`,
- "if (!cache.has(base)) {",
- Template.indent([
- "var script = [",
- Template.indent([
- // worklet scopes have no `self`; chunks assume a worker-like scope.
- // `||=` is newer than worklets themselves, so it stays gated.
- `${JSON.stringify(
- `${runtimeTemplate.assignOr("globalThis.self", "globalThis")};`
- )},`,
- // some runtime modules read `location`; forward the document base
- '"globalThis.location = " + JSON.stringify(base) + ";"'
- ]),
- '].join("\\n");',
- 'cache.set(base, URL.createObjectURL(new Blob([script], { type: "text/javascript" })));'
- ]),
- "}",
- "return cache.get(base);"
- ]);
- return `${getWorkletBootstrap} = (${runtimeTemplate.basicFunction("", [
- "var cache = new Map();",
- `return ${getter};`
- ])})();`;
- }
- }
- module.exports = GetWorkletBootstrapRuntimeModule;
|