|
1 | 1 | import { walk } from "https://deno.land/std@0.224.0/fs/mod.ts"; |
2 | 2 |
|
3 | | -const DIRS_TO_SCAN = ["./nodejs", "./bundler", "./web"]; |
| 3 | +const DIRS_TO_SCAN = ["./nodejs", "./bundler", "./browser", "./web"]; |
4 | 4 | const FILES_TO_PROCESS = ["index.js", "index.d.ts"]; |
5 | 5 |
|
6 | 6 | async function replaceInFile(filePath: string) { |
@@ -73,7 +73,12 @@ async function rollupBase64() { |
73 | 73 |
|
74 | 74 | const base64IndexPath = "./base64/index.js"; |
75 | 75 | const content = await Deno.readTextFile(base64IndexPath); |
76 | | - const nextContent = injectBase64WasmBranch(content, base64IndexPath); |
| 76 | + let nextContent = injectBase64WasmBranch(content, base64IndexPath); |
| 77 | + nextContent = simplifyBase64WasmInitialization( |
| 78 | + nextContent, |
| 79 | + base64IndexPath, |
| 80 | + ); |
| 81 | + nextContent = patchBase64NodeRequires(nextContent, base64IndexPath); |
77 | 82 | await Deno.writeTextFile(base64IndexPath, nextContent); |
78 | 83 |
|
79 | 84 | await Deno.copyFile("./bundler/loro_wasm.d.ts", "./base64/loro_wasm.d.ts"); |
@@ -109,13 +114,74 @@ function injectBase64WasmBranch(content: string, filePath: string): string { |
109 | 114 | return content.replace(bunBranchPattern, base64Branch); |
110 | 115 | } |
111 | 116 |
|
| 117 | +function simplifyBase64WasmInitialization( |
| 118 | + content: string, |
| 119 | + filePath: string, |
| 120 | +): string { |
| 121 | + const startMarker = |
| 122 | + "// Normalize how bundlers expose the wasm module/exports."; |
| 123 | + const endMarker = `\n\n/** |
| 124 | + * @deprecated Please use LoroDoc |
| 125 | + */`; |
| 126 | + const start = content.indexOf(startMarker); |
| 127 | + const end = start === -1 ? -1 : content.indexOf(endMarker, start); |
| 128 | + if (start === -1 || end === -1) { |
| 129 | + throw new Error( |
| 130 | + `Could not locate wasm initialization block while patching ${filePath}`, |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + const replacement = `// Instantiate the inlined base64 wasm synchronously. |
| 135 | +const wasmModuleOrInstance = rawWasm.default({ |
| 136 | + "./loro_wasm_bg.js": imports, |
| 137 | +}); |
| 138 | +const wasmInstance = |
| 139 | + wasmModuleOrInstance instanceof WebAssembly.Instance |
| 140 | + ? wasmModuleOrInstance |
| 141 | + : new WebAssembly.Instance(wasmModuleOrInstance, { |
| 142 | + "./loro_wasm_bg.js": imports, |
| 143 | + }); |
| 144 | +__wbg_set_wasm(wasmInstance.exports ?? wasmInstance); |
| 145 | +if (typeof imports.__wbindgen_start === "function") { |
| 146 | + imports.__wbindgen_start(); |
| 147 | +}`; |
| 148 | + |
| 149 | + return content.slice(0, start) + replacement + content.slice(end); |
| 150 | +} |
| 151 | + |
| 152 | +function patchBase64NodeRequires(content: string, filePath: string): string { |
| 153 | + const directRequires = `var fs = require("fs"); |
| 154 | +var path = require("path");`; |
| 155 | + const indirectRequires = `var nodeRequire = typeof require === "function" ? require : null; |
| 156 | +var fs = nodeRequire && nodeRequire("fs"); |
| 157 | +var path = nodeRequire && nodeRequire("path");`; |
| 158 | + const browserSafeRequires = `var fs = null; |
| 159 | +var path = null;`; |
| 160 | + |
| 161 | + if (content.includes(browserSafeRequires)) { |
| 162 | + return content; |
| 163 | + } |
| 164 | + |
| 165 | + if (content.includes(directRequires)) { |
| 166 | + return content.replace(directRequires, browserSafeRequires); |
| 167 | + } |
| 168 | + |
| 169 | + if (content.includes(indirectRequires)) { |
| 170 | + return content.replace(indirectRequires, browserSafeRequires); |
| 171 | + } |
| 172 | + |
| 173 | + throw new Error( |
| 174 | + `Could not locate Node require block while patching ${filePath}`, |
| 175 | + ); |
| 176 | +} |
| 177 | + |
112 | 178 | async function main() { |
113 | 179 | for (const dir of DIRS_TO_SCAN) { |
114 | 180 | await transform(dir); |
115 | 181 | } |
116 | 182 |
|
117 | 183 | await rollupBase64(); |
118 | | - transform("./base64"); |
| 184 | + await transform("./base64"); |
119 | 185 | } |
120 | 186 |
|
121 | 187 | if (import.meta.main) { |
|
0 commit comments