Skip to content

Commit

Permalink
fix: handle native modules in the main process correctly in the webpa…
Browse files Browse the repository at this point in the history
…ck plugin
  • Loading branch information
MarshallOfSound committed May 24, 2019
1 parent af0556b commit 8d688b8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
4 changes: 0 additions & 4 deletions packages/api/core/src/api/init-scripts/init-custom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { asyncOra } from '@electron-forge/async-ora';
import debug from 'debug';
import fs from 'fs-extra';
import glob from 'glob';
import resolvePackage from 'resolve-package';
import path from 'path';

import { ForgeTemplate } from '@electron-forge/shared-types';
import { copy } from './init-starter-files';
import installDepList, { DepType } from '../../util/install-dependencies';
// https://github.com/benmosher/eslint-plugin-import/issues/1120
// eslint-disable-next-line import/named
Expand Down
27 changes: 25 additions & 2 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const d = debug('electron-forge:plugin:webpack');
const DEFAULT_PORT = 3000;
const DEFAULT_LOGGER_PORT = 9000;

type EntryType = string | string[] | Record<string, string | string[]>;

export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
name = 'webpack';

Expand Down Expand Up @@ -209,8 +211,14 @@ Your packaged app may be larger than expected if you dont ignore everything othe
getDefines = (inRendererDir = true) => {
const defines: { [key: string]: string; } = {
ASSET_RELOCATOR_BASE_DIR: this.isProd
? 'process.resourcesPath + "/" + (__filename.indexOf(".asar") === -1 ? "app" : "app.asar") + "/.webpack/renderer/native_modules"'
: JSON.stringify(path.resolve(this.baseDir, 'renderer', 'native_modules')),
? `process.resourcesPath + "/" + (__filename.indexOf(".asar") === -1 ? "app" : "app.asar") + "/.webpack/${inRendererDir ? 'main' : 'renderer/any_folder'}"`
: JSON.stringify(
path.resolve(
this.baseDir,
inRendererDir ? 'main' : 'renderer',
inRendererDir ? '.' : 'any_folder',
),
),
};
if (!this.config.renderer.entryPoints || !Array.isArray(this.config.renderer.entryPoints)) {
throw new Error('Required config option "renderer.entryPoints" has not been defined');
Expand All @@ -237,6 +245,21 @@ Your packaged app may be larger than expected if you dont ignore everything othe
if (!mainConfig.entry) {
throw new Error('Required config option "entry" has not been defined');
}
const fix = (item: EntryType): EntryType => {
if (typeof item === 'string') return (fix([item]) as string[])[0];
if (Array.isArray(item)) {
return item.map((val) => {
if (val.indexOf('./') === 0) return path.resolve(this.projectDir, val);
return val;
});
}
const ret: Record<string, string | string[]> = {};
for (const key of Object.keys(item)) {
ret[key] = fix(item[key]) as string | string[];
}
return ret;
};
mainConfig.entry = fix(mainConfig.entry as EntryType);

const defines = this.getDefines();
return merge.smart({
Expand Down
3 changes: 2 additions & 1 deletion packages/template/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.38",
"@electron-forge/shared-types": "6.0.0-beta.38"
"@electron-forge/shared-types": "6.0.0-beta.38",
"fs-extra": "^7.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/template/webpack/src/WebpackTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WebpackTemplate implements ForgeTemplate {
await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.rules.js'), path.resolve(directory, 'webpack.rules.js'));
await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'renderer.js'), path.resolve(directory, 'src', 'renderer.js'));
let indexContents = await fs.readFile(path.resolve(directory, 'src', 'index.js'), 'utf8');
indexContents = indexContents.split('\n').map(line => {
indexContents = indexContents.split('\n').map((line) => {
if (line.includes('mainWindow.loadURL')) return ' mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);';
return line;
}).join('\n');
Expand Down

0 comments on commit 8d688b8

Please sign in to comment.