Skip to content

Commit

Permalink
fix(plugin-webpack): validate that the correct entry point is used (#…
Browse files Browse the repository at this point in the history
…2522)

* chore(plugin-webpack): simplify test script
  • Loading branch information
malept authored Sep 12, 2021
1 parent 0deb74b commit 3de904b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/plugin/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"main": "dist/WebpackPlugin.js",
"typings": "dist/WebpackPlugin.d.ts",
"scripts": {
"test": "xvfb-maybe mocha --config ../../../.mocharc.js test/**/*_spec.ts test/**/**/*_spec.ts"
"test": "xvfb-maybe mocha --config ../../../.mocharc.js test/**/*_spec.ts"
},
"devDependencies": {
"@malept/cross-spawn-promise": "^2.0.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {

private isProd = false;

// The root of the Electron app
private projectDir!: string;

// Where the Webpack output is generated. Usually `$projectDir/.webpack`
private baseDir!: string;

private _configGenerator!: WebpackConfigGenerator;
Expand Down Expand Up @@ -208,6 +210,13 @@ Your packaged app may be larger than expected if you dont ignore everything othe

packageAfterCopy = async (_forgeConfig: ForgeConfig, buildPath: string): Promise<void> => {
const pj = await fs.readJson(path.resolve(this.projectDir, 'package.json'));

if (!pj.main?.endsWith('.webpack/main')) {
throw new Error(`Electron Forge is configured to use the Webpack plugin. The plugin expects the
"main" entry point in "package.json" to be ".webpack/main" (where the plugin outputs
the generated files). Instead, it is ${JSON.stringify(pj.main)}`);
}

if (pj.config) {
delete pj.config.forge;
}
Expand Down
16 changes: 14 additions & 2 deletions packages/plugin/webpack/test/WebpackPlugin_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,33 @@ describe('WebpackPlugin', () => {
});

it('should remove config.forge from package.json', async () => {
const packageJSON = { config: { forge: 'config.js' } };
const packageJSON = { main: './.webpack/main', config: { forge: 'config.js' } };
await fs.writeJson(packageJSONPath, packageJSON);
await plugin.packageAfterCopy({} as ForgeConfig, 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' };
const packageJSON = { main: '.webpack/main' };
await fs.writeJson(packageJSONPath, packageJSON);
await plugin.packageAfterCopy({} as ForgeConfig, packagedPath);
expect(await fs.pathExists(packagedPackageJSONPath)).to.equal(true);
expect(await fs.readJson(packagedPackageJSONPath)).to.not.have.property('config');
});

it('should fail if there is no main key in package.json', async () => {
const packageJSON = {};
await fs.writeJson(packageJSONPath, packageJSON);
await expect(plugin.packageAfterCopy({} as ForgeConfig, packagedPath)).to.eventually.be.rejectedWith(/entry point/);
});

it('should fail if main in package.json does not end with .webpack/main', async () => {
const packageJSON = { main: 'src/main.js' };
await fs.writeJson(packageJSONPath, packageJSON);
await expect(plugin.packageAfterCopy({} as ForgeConfig, packagedPath)).to.eventually.be.rejectedWith(/entry point/);
});

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

0 comments on commit 3de904b

Please sign in to comment.