Skip to content

Commit

Permalink
feat(electron): generate a single electron preload script
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Electron apps using the generated preload scripts should now
load preload.js using webPreferences.preload. This script will load any other
scripts detected in the workspace.
  • Loading branch information
Kevin Schmidt committed Jul 12, 2021
1 parent 9c437cb commit b18f0b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion plugins/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const clone = require('clone');

var electronDeps = {};
var preloadScripts = [];
var preloadRequires = [];

const resolvePackages = function(pack, projectDir, packages) {
if (packages) {
Expand Down Expand Up @@ -115,19 +116,28 @@ const writer = function(thisPackage, outputDir) {
// copy each preload script to the target directory
return Promise.map(preloadScripts, function(script, idx, arr) {
// increment preload file names. Electron will load everything in the directory.
var dest = path.join(preloadDir, 'preload' + idx + '.js');
var destFile = 'preload' + idx + '.js';
var dest = path.join(preloadDir, destFile);
preloadRequires.push(destFile);

console.log('Writing Electron preload script: ' + dest);

return fs.copyFileAsync(script.path, dest, fs.constants.COPYFILE_EXCL);
});
})
.then(function() {
// write the master preload script that should be loaded via webPreferences.preload
var preloadSrc = preloadRequires.map((r) => `require('./${r}');`).join('\n');
var preloadFile = path.join(preloadDir, 'preload.js');
return fs.writeFileAsync(preloadFile, preloadSrc);
});
});
};

const clear = function() {
electronDeps = {};
preloadScripts = [];
preloadRequires = [];
};

module.exports = {
Expand Down

0 comments on commit b18f0b1

Please sign in to comment.