Debian Codegraph @

wasm/browser/script/inline-webworker.js

raw
1 import { readFileSync } from "fs";
2 import { basename, resolve } from "path";
3 import { createFilter } from "rollup-pluginutils";
4
5 const inlineCode = () => `
6 // http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string
7 function __inlineWorker(content) {
8 var URL = window.URL || window.webkitURL;
9 try {
10 try {
11 var blob;
12
13 try {
14 // BlobBuilder = Deprecated, but widely implemented
15 var BlobBuilder =
16 window.BlobBuilder ||
17 window.WebKitBlobBuilder ||
18 window.MozBlobBuilder ||
19 window.MSBlobBuilder;
20
21 blob = new BlobBuilder();
22
23 blob.append(content);
24
25 blob = blob.getBlob();
26 } catch (e) {
27 // The proposed API
28 blob = new Blob([content]);
29 }
30
31 return new Worker(URL.createObjectURL(blob));
32 } catch (e) {
33 return new Worker(
34 "object-src 'self' blob: data:application/javascript," + encodeURIComponent(content)
35 );
36 }
37 } catch (e) {
38 throw Error('Inline worker is not supported');
39
40 return new Worker(url);
41 }
42 };
43 `;
44
45 export default function inlineWebworker(options = {}) {
46 const filter = createFilter(options.include, options.exclude);
47 const dataUrl = options.dataUrl;
48 return {
49 name: "inline-webworker",
50 load(id) {
51 if (filter(id)) {
52 const filename = basename(id);
53 id = resolve("./dist", `__compiled.${filename}`);
54 return {
55 code: dataUrl
56 ? `
57 export default () => "data:application/javascript;base64,${readFileSync(id, {
58 encoding: "base64",
59 })}";
60 `
61 : `
62 export default () => (window.URL || window.webkitURL).createObjectURL(new Blob([${JSON.stringify(
63 readFileSync(id).toString("utf8"),
64 )}]));`,
65 map: { mappings: "" },
66 };
67 }
68 },
69 };
70 }