Skip to content

Commit

Permalink
fix(plugin-webpack): handle package.json files without config keys (#…
Browse files Browse the repository at this point in the history
…1342)

ISSUES CLOSED: #1326
  • Loading branch information
malept authored Dec 12, 2019
1 parent 51fbf71 commit 4db53f8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,18 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
});

init = (dir: string) => {
this.projectDir = dir;
this.baseDir = path.resolve(dir, '.webpack');
this.setDirectories(dir);

d('hooking process events');
process.on('exit', (_code) => this.exitHandler({ cleanup: true }));
process.on('SIGINT' as NodeJS.Signals, (_signal) => this.exitHandler({ exit: true }));
}

setDirectories = (dir: string) => {
this.projectDir = dir;
this.baseDir = path.resolve(dir, '.webpack');
}

private loggedOutputUrl = false;

getHook(name: string) {
Expand Down Expand Up @@ -173,7 +177,9 @@ Your packaged app may be larger than expected if you dont ignore everything othe

packageAfterCopy = async (_: any, buildPath: string) => {
const pj = await fs.readJson(path.resolve(this.projectDir, 'package.json'));
delete pj.config.forge;
if (pj.config) {
delete pj.config.forge;
}
pj.devDependencies = {};
pj.dependencies = {};
pj.optionalDependencies = {};
Expand Down
44 changes: 44 additions & 0 deletions packages/plugin/webpack/test/WebpackPlugin_spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { expect } from 'chai';
import * as fs from 'fs-extra';
import * as path from 'path';
import { tmpdir } from 'os';

import WebpackPlugin from '../src/WebpackPlugin';

describe('WebpackPlugin', () => {
const webpackTestDir = path.resolve(tmpdir(), 'electron-forge-plugin-webpack-test');

describe('PRELOAD_WEBPACK_ENTRY', () => {
it('should assign absolute preload script path in development', () => {
const p = new WebpackPlugin({
Expand Down Expand Up @@ -52,4 +57,43 @@ describe('WebpackPlugin', () => {
expect(defines.WINDOW_PRELOAD_WEBPACK_ENTRY).to.be.eq("require('path').resolve(__dirname, '../renderer', 'window', 'preload.js')");
});
});

describe('packageAfterCopy', () => {
const packageJSONPath = path.join(webpackTestDir, 'package.json');
const packagedPath = path.join(webpackTestDir, 'packaged');
const packagedPackageJSONPath = path.join(packagedPath, 'package.json');
let plugin: WebpackPlugin;

before(async () => {
await fs.ensureDir(packagedPath);
plugin = new WebpackPlugin({
mainConfig: {},
renderer: {
config: {},
entryPoints: [],
},
});
plugin.setDirectories(webpackTestDir);
});

it('should remove config.forge from package.json', async () => {
const packageJSON = { config: { forge: 'config.js' } };
await fs.writeJson(packageJSONPath, packageJSON);
await plugin.packageAfterCopy(null, packagedPath);
expect(await fs.pathExists(packagedPackageJSONPath)).to.equal(true);
expect((await fs.readJson(packagedPackageJSONPath)).config).to.not.have.property('forge');
});

it('should succeed if there is no config.forge', async () => {
const packageJSON = { name: 'test' };
await fs.writeJson(packageJSONPath, packageJSON);
await plugin.packageAfterCopy(null, packagedPath);
expect(await fs.pathExists(packagedPackageJSONPath)).to.equal(true);
expect((await fs.readJson(packagedPackageJSONPath))).to.not.have.property('config');
});

after(async () => {
await fs.remove(webpackTestDir);
});
});
});

0 comments on commit 4db53f8

Please sign in to comment.