Description
I'm trying to build .NET 8 library for VS Code web extension, where neither browser APIs (window/document), nor node modules are available. Previously (in .NET 6) I had to strip generated JS wrapper from various unconditional usages of such APIs. I hoped in .NET 8 it's solved (and I can see lots of changes in this direction), but unfortunately there are stilll issues. For example, createDotnetRuntime
function has following (from bundled library sources, so it's bit mangled):
document
is undefined in VS Code and require('url')
causes exception. I'm injecting all the requires resources (runtime.wasm, dotnet.runtime.js, etc) via the assets in config, so no IO should be required to boot the runtime.
Is it possible to guard browser/node-specific calls like this for .NET 8?
The Specific Offenders in .NET 8
Here is the link to the patch I'm using to strip all the offenders: https://github.com/elringus/bootsharp/blob/main/src/cs/Bootsharp.Publish/Pack/ModulePatcher/InternalPatcher.cs After the patch, bundlers are only complaining about module
and process
unresolved externals (tested with webpack, rollup, esbuild, vite) and the bundled runtime is working in browsers, node, deno, bun and VS Code web extensions.
Regarding module
and process
— would be optimal to guard them as well, though I wasn't able to find a straightforward way to do that (conditional usage still trigger bundlers). As a workaround, I'm marking them as global (eg, -g process,module
in rollup), which seem to satisfy the bundlers.