Skip to content

Commit

Permalink
feat: added multiple preload scripts support #155
Browse files Browse the repository at this point in the history
  • Loading branch information
bennymeg committed Apr 20, 2022
1 parent 8958f32 commit 1adc422
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/nx-electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nx-electron",
"version": "13.0.0-alpha.2",
"version": "13.0.0-alpha.3",
"main": "src/index.js",
"description": "Electron Plugin for Nx",
"author": "Benny Megidish",
Expand Down
10 changes: 9 additions & 1 deletion packages/nx-electron/src/executors/build/executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join, resolve } from 'path';
import { map, tap } from 'rxjs/operators';
import { eachValueFrom } from 'rxjs-for-await';
import { readdirSync } from 'fs';

import { ExecutorContext } from '@nrwl/devkit';
import { runWebpack } from '../../utils/run-webpack';
Expand Down Expand Up @@ -64,8 +65,15 @@ export function executor(rawOptions: BuildElectronBuilderOptions, context: Execu
configuration: context.configurationName,
});
}
config.entry['preload'] = join(normalizedOptions.sourceRoot, 'app/api/preload.ts');

try {
const preloadFilesDirectory = join(normalizedOptions.sourceRoot, 'app/api');
readdirSync(preloadFilesDirectory, { withFileTypes: true })
.filter(entry => entry.isFile() && entry.name.match(/(.+[.])?preload.ts/))
.forEach(entry => config.entry[entry.name] = join(preloadFilesDirectory, entry.name));
} catch (error) {
console.warn('Failed to load preload scripts');
}

return eachValueFrom(
runWebpack(config).pipe(
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-electron/src/executors/package/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function _createBaseConfig(options: PackageElectronBuilderOptions, context: Exec
{
from: resolve(options.sourcePath, options.name),
to: options.name,
filter: ['main.js', 'preload.js', 'assets']
filter: ['main.js', '?(*.)preload.js', 'assets']
},
{
from: resolve(options.sourcePath, options.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class App {
App.mainWindow = new BrowserWindow({ width: width, height: height, show: false, webPreferences: {
contextIsolation: true,
backgroundThrottling: false,
preload: join(__dirname, 'preload.js')
preload: join(__dirname, 'main.preload.js')
}
});
App.mainWindow.setMenu(null);
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-electron/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export function getBaseWebpackPartial(options: BuildBuilderOptions): Configurati
const webpackConfig: Configuration = {
entry: {
main: [options.main],
// preload: [options.main.replace(/([.][a-z]+)$/, ".preload$1")],
...additionalEntryPoints,
// preload entries will be included dynamically
},
devtool: options.sourceMap ? 'source-map' : false,
mode: options.optimization ? 'production' : 'development',
Expand Down

0 comments on commit 1adc422

Please sign in to comment.