Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-webpack): handle package.json files without config keys #1342

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});