Skip to content

Latest commit

 

History

History

parcel-optimizer-electron-require

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Parcel Optimizer Electron Require

npm: @wuespace/parcel-optimizer-electron-require

Maintainability Test Coverage GitHub Workflow Status (with branch) GitHub Node current PNPM current Twitter Follow

Parcel Optimizer Plugin that polyfills the require function in an electron context. (especially in preload)

Installation

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!

Implementation Details

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:

https://github.com/parcel-bundler/parcel/blob/10a56a7832c400025f23b6df6fcce3099a4d6302/packages/packagers/js/src/dev-prelude.js#L30-L33

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 };
}

Contributing

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.

Contributors

Thank you to all contributors of this repository:

Contributors

Made with contributors-img.

About

This is part of Telestion, a project by WüSpace e.V..