Skip to content
Permalink
Browse files

fix: handle native modules in the main process correctly in the webpa…

…ck plugin
  • Loading branch information
MarshallOfSound committed May 24, 2019
1 parent af0556b commit 8d688b8129968ed345322c21fe5dbd8b78f54ca6
@@ -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
@@ -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';

@@ -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');
@@ -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({
@@ -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"
}
}
@@ -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');

0 comments on commit 8d688b8

Please sign in to comment.
You can’t perform that action at this time.