Prefix calls to require
as window.require
for node and electron built-in modules. This allows them to work as you would expect in electron processes.
npm install electron-renderify
This module is to be used as a browserify transform.
Depending on what is in your bundle, and how you are setting up Electron, you may need to apply some or all of the following browserify settings:
{
builtins: [],
commonDir: false,
detectGlobals: false,
ignoreMissing: true,
insertGlobalVars: 'global',
browserField: false
}
The best place to apply the settings is in your package.json
. That way they will take effect with either CLI or JS api use.
browserify -t electron-renderify sample.js > bundle.js
var browserify = require('browserify')
var renderify = require('electron-renderify')
var path = require('path')
browserify()
.transform(renderify)
.add(path.join(__dirname, 'sample.js'))
.bundle()
.pipe(process.stdout)
You can modify the behaviour of electron-renderify
by passing options to the transform, like this:
browserify()
.transform(renderify, opts)
The following options are available:
An array of strings, each of which specifies a module that should use window.require
instead of plain require
. This might be required for any native modules (although you should consider moving any such dependencies to the main process).
Example:
var browserify = require('browserify')
var renderify = require('electron-renderify')
var path = require('path')
var renderifyOpts = {
windowRequire: ['leveldown']
}
browserify()
.transform(renderify, renderifyOpts)
.add('somefile-requiring-leveldown.js')
.bundle()
.pipe(process.stdout)
To the extent possible by law, we transfer any rights we have in this code to the public domain. Specifically, we do so using the CC0 1.0 Universal Public Domain Dedication.
You can do whatever you want with this code. No need to credit us, link to us, include any license, or anything else. But if you want to do those things, you're free to do that too.