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): validate that the correct entry point is used #2522

Merged
merged 2 commits into from
Sep 12, 2021
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
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