npm: @wuespace/parcel-optimizer-electron-require
Parcel Optimizer Plugin that polyfills the require function in an electron context. (especially in preload)
If you're using
the @wuespace/telestion-client-template
the
plugin is already installed and integrated in Parcel.
If that's not the case, install the plugin as development dependency:
pnpm add -D @wuespace/parcel-optimizer-electron-require
# or
npm install --save-dev @wuespace/parcel-optimizer-electron-require
Then add it to your .parcelrc
configuration:
{
optimizers: {
// add optimizer to fix require in electron preload
'*.js': ['@wuespace/parcel-optimizer-electron-require']
}
}
That's it!
In Electron preload contexts a global require
function exists, but no module
object.
In Parcel's watch mode, the following code gets used to handle CommonJS imports:
This optimizer adds a prefix to every packaged JavaScript file that adds the module
object with the require
function
to circumvent Electron not providing a module
object:
// Fix Electron renderer "require()" without a module issue
// see: https://github.com/parcel-bundler/parcel/issues/2492
if (typeof require !== 'undefined' && typeof module === 'undefined') {
var module = { require: require };
}
If you want to contribute to this package, please take a look at the Telestion Client monorepo that manages this package, among other Telestion Client packages.
Thank you to all contributors of this repository:
Made with contributors-img.
This is part of Telestion, a project by WüSpace e.V..