-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve bundling of
web-viewer
package (#6659)
Co-authored-by: Jeremy Leibs <[email protected]>
- Loading branch information
Showing
12 changed files
with
364 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ index.d.ts | |
index.d.ts.map | ||
index.js | ||
index.js.map | ||
inlined.js | ||
inlined.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Script responsible for building the wasm and transforming the JS bindings for the web viewer. | ||
|
||
import * as child_process from "node:child_process"; | ||
import { fileURLToPath } from "node:url"; | ||
import * as path from "node:path"; | ||
import * as fs from "node:fs"; | ||
import * as util from "node:util"; | ||
|
||
const __filename = path.resolve(fileURLToPath(import.meta.url)); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const exec = (cmd) => { | ||
console.log(cmd); | ||
child_process.execSync(cmd, { cwd: __dirname, stdio: "inherit" }); | ||
}; | ||
|
||
function wasm(mode) { | ||
switch (mode) { | ||
case "debug": { | ||
return exec( | ||
"cargo run -p re_dev_tools -- build-web-viewer --debug --target no-modules-base -o rerun_js/web-viewer", | ||
); | ||
} | ||
case "release": { | ||
return exec( | ||
"cargo run -p re_dev_tools -- build-web-viewer --release -g --target no-modules-base -o rerun_js/web-viewer", | ||
); | ||
} | ||
default: | ||
throw new Error(`Unknown mode: ${mode}`); | ||
} | ||
} | ||
|
||
child_process.execSync( | ||
"cargo run -p re_dev_tools -- build-web-viewer --debug --target no-modules-base -o rerun_js/web-viewer", | ||
{ cwd: __dirname, stdio: "inherit" }, | ||
); | ||
|
||
function script() { | ||
let code = fs.readFileSync(path.join(__dirname, "re_viewer.js"), "utf-8"); | ||
|
||
// this transforms the module, wrapping it in a default-exported function. | ||
// calling the function produces a new "instance" of the module, because | ||
// all of the globals are scoped to the function, and become closure state | ||
// for any functions that reference them within the module. | ||
// | ||
// we do this so that we don't leak globals across web viewer instantiations: | ||
// https://github.com/rustwasm/wasm-bindgen/issues/3130 | ||
// | ||
// this is HIGHLY sensitive to the exact output of `wasm-bindgen`, so if | ||
// the output changes, this will need to be updated. | ||
|
||
const start = `let wasm_bindgen; | ||
(function() {`; | ||
const end = `wasm_bindgen = Object.assign(__wbg_init, { initSync }, __exports); | ||
})();`; | ||
code = code.replace(start, "").replace(end, ""); | ||
|
||
code = ` | ||
export default function() { | ||
${code} | ||
function deinit() { | ||
__wbg_init.__wbindgen_wasm_module = null; | ||
wasm = null; | ||
cachedFloat32Memory0 = null; | ||
cachedFloat64Memory0 = null; | ||
cachedInt32Memory0 = null; | ||
cachedUint32Memory0 = null; | ||
cachedUint8Memory0 = null; | ||
} | ||
return Object.assign(__wbg_init, { initSync, deinit }, __exports); | ||
} | ||
`; | ||
|
||
// Since we are nulling `wasm` we also have to patch the closure destructor code to let things be cleaned up fully. | ||
// Otherwise we end up with an exceptioon during closure destruction which prevents the references from all being | ||
// cleaned up properly. | ||
// TODO(jprochazk): Can we force these to run before we null `wasm` instead? | ||
const closure_dtors = `const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') | ||
? { register: () => {}, unregister: () => {} } | ||
: new FinalizationRegistry(state => { | ||
wasm.__wbindgen_export_3.get(state.dtor)(state.a, state.b)`; | ||
|
||
const closure_dtors_patch = `const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') | ||
? { register: () => {}, unregister: () => {} } | ||
: new FinalizationRegistry(state => { | ||
wasm?.__wbindgen_export_3.get(state.dtor)(state.a, state.b)`; | ||
|
||
code = code.replace(closure_dtors, closure_dtors_patch); | ||
|
||
fs.writeFileSync(path.join(__dirname, "re_viewer.js"), code); | ||
} | ||
|
||
function types() { | ||
let code = fs.readFileSync(path.join(__dirname, "re_viewer.d.ts"), "utf-8"); | ||
|
||
// this transformation just re-exports WebHandle and adds a default export inside the `.d.ts` file | ||
|
||
code = ` | ||
${code} | ||
export type WebHandle = wasm_bindgen.WebHandle; | ||
export default function(): wasm_bindgen; | ||
`; | ||
|
||
fs.writeFileSync(path.join(__dirname, "re_viewer.d.ts"), code); | ||
} | ||
|
||
const args = util.parseArgs({ | ||
options: { | ||
mode: { | ||
type: "string", | ||
}, | ||
}, | ||
}); | ||
|
||
if (!args.values.mode) { | ||
throw new Error("Missing required argument: mode"); | ||
} | ||
|
||
wasm(args.values.mode); | ||
script(); | ||
types(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @ts-check | ||
|
||
// Script responsible for taking the generated Wasm/JS, and transpiled TS | ||
// and producing a single file with everything inlined. | ||
|
||
import { fileURLToPath } from "node:url"; | ||
import * as path from "node:path"; | ||
import * as fs from "node:fs"; | ||
import * as util from "node:util"; | ||
|
||
const __filename = path.resolve(fileURLToPath(import.meta.url)); | ||
const __dirname = path.dirname(__filename); | ||
|
||
const wasm = fs.readFileSync(path.join(__dirname, "re_viewer_bg.wasm")); | ||
const js = fs.readFileSync(path.join(__dirname, "re_viewer.js"), "utf-8"); | ||
const index = fs.readFileSync(path.join(__dirname, "index.js"), "utf-8"); | ||
|
||
const INLINE_MARKER = "/*<INLINE-MARKER>*/"; | ||
|
||
/** @param {Buffer} buffer */ | ||
function buffer_to_data_url(buffer) { | ||
return `data:application/wasm;base64,${buffer.toString("base64")}`; | ||
} | ||
|
||
async function data_url_to_buffer(dataUrl) { | ||
const response = await fetch(dataUrl); | ||
return response.arrayBuffer(); | ||
} | ||
|
||
const inlined_js = js.replace("export default function", "return function"); | ||
|
||
const inlined_code = ` | ||
async function fetch_viewer_js() { | ||
${inlined_js} | ||
} | ||
async function fetch_viewer_wasm() { | ||
${data_url_to_buffer.toString()} | ||
const dataUrl = ${JSON.stringify(buffer_to_data_url(wasm))}; | ||
const buffer = await data_url_to_buffer(dataUrl); | ||
return new Response(buffer, { "headers": { "Content-Type": "application/wasm" } }); | ||
} | ||
`; | ||
|
||
// replace INLINE_MARKER, inclusive | ||
const inline_start = index.indexOf(INLINE_MARKER); | ||
if (inline_start === -1) { | ||
throw new Error("no inline marker in source file"); | ||
} | ||
let inline_end = index.indexOf(INLINE_MARKER, inline_start + 1); | ||
if (inline_end === -1) { | ||
throw new Error("no inline marker in source file"); | ||
} | ||
inline_end += INLINE_MARKER.length; | ||
|
||
const bundle = | ||
index.substring(0, inline_start) + inlined_code + index.substring(inline_end); | ||
|
||
fs.writeFileSync(path.join(__dirname, "inlined.js"), bundle); | ||
fs.copyFileSync( | ||
path.join(__dirname, "index.d.ts"), | ||
path.join(__dirname, "inlined.d.ts"), | ||
); |
Oops, something went wrong.