fix: handle native modules in the main process correctly in the webpack plugin

This commit is contained in:
Samuel Attard 2019-05-23 17:54:54 -07:00
Родитель af0556b202
Коммит 8d688b8129
4 изменённых файлов: 28 добавлений и 8 удалений

Просмотреть файл

@ -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');