Skip to content

Instantly share code, notes, and snippets.

@aryanshridhar
Last active September 9, 2022 00:01
Show Gist options
  • Save aryanshridhar/14f7b919befa003254568fe69fde0888 to your computer and use it in GitHub Desktop.
Save aryanshridhar/14f7b919befa003254568fe69fde0888 to your computer and use it in GitHub Desktop.

Background

For migrating the Node APIs from the renderer process to the main process, the IPCRenderer API is to be moved to preload scripts and then exposed in the renderer process via the contextBridge API.
The below diagram explains it better -
Image


Issue

The above image shows the expected architecture wherein nodeIntegrationInWorker is to be set false before proceeding ahead. However, nodeIntegrationInWorker is required by us for the monao web workers to function properly in the production build of fiddle. The error can be confirmed with the logs -

Uncaught ReferenceError: require is not defined
    at ts.worker.js:2:2658562
    at ts.worker.js:2:4313768
3index.js:2 Uncaught Error: Unexpected usage

Error: Unexpected usage
    at at.loadForeignModule (index.js:2:3849383)
    at index.js:2:3858113
    at index.js:2:1626096
    at i (index.js:2:968071)
3index.js:2 Uncaught (in promise) Error: Unexpected usage
    at at.loadForeignModule (index.js:2:3849383)
    at index.js:2:3858113

Reason

The issue is because html-webpack-plugin doesn't support the __dirname API, due to which webpack-asset-relocator-loader is being monkey patched in forge for it to function properly. This can be confirmed by modifying the monaco-editor getWorkerUrl to point it to compiled web workers. By doing so the fiddle production build throws an error saying - __dirname is not defined.
For a more detailed reasoning - corresponding slack conversation link.

Possible solution

jantimon/html-webpack-plugin#1650 was created to add the support for __dirname but it was closed by a bot for being stale. Probably, it can be re-opened and work upon again. If by any chance the upstream repository is reluctant to add these changes we can probably maintain a fork of html-webpack-plugin and webpack-asset-relocator-loader to modify electron forge in order to avoid monkey patching (However, this will cost us time and extra efforts to maintain them)

My commitment

Unfortunately, the offical summer period is about to be over. However, I will still be unofficially working to resolve this bug and eventually proceed with the pr i.e to migrate all the Node APIs in renderer process to main process. (Apologies, if I wasn't able to complete this task before specified time)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment