Skip to content

Commit 07f8368

Browse files
committed
feat(plugin-webpack): customizable HtmlWebpackPlugin options
This changeset adds options to entrypoints to support customized operation of the `HtmlWebpackPlugin`. See below for full changeset. Changes enclosed: - Add properties for `output`, `htmlPlugins`, and `htmlOptions` to `WebpackPluginEntryPoint` - Use new options from `Config.ts`, by merging them into their expected places - Add tests to cover new options Fixes and closes electron#2968.
1 parent 362099a commit 07f8368

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

packages/plugin/webpack/src/Config.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Configuration as RawWebpackConfiguration } from 'webpack';
1+
import { Configuration as RawWebpackConfiguration, WebpackPluginInstance } from 'webpack';
22
import WebpackDevServer from 'webpack-dev-server';
33
import { ConfigurationFactory as WebpackConfigurationFactory } from './WebpackConfig';
4+
import HtmlWebpackPlugin from 'html-webpack-plugin';
45

56
export interface WebpackPluginEntryPoint {
67
/**
@@ -48,6 +49,20 @@ export interface WebpackPluginEntryPoint {
4849
* for all entries.
4950
*/
5051
nodeIntegration?: boolean;
52+
/**
53+
* Custom options to merge into the configuration passed to `HtmlWebpackPlugin`.
54+
*/
55+
htmlOptions?: Partial<HtmlWebpackPlugin.Options>;
56+
/**
57+
* Plugins to include before `HtmlWebpackPlugin`; typically, HTML plugin add-ons will
58+
* need to be placed here.
59+
*/
60+
htmlPlugins?: WebpackPluginInstance[];
61+
/**
62+
* Additional options to merge into the Webpack `output` configuration for this entry-
63+
* point.
64+
*/
65+
output?: object;
5166
}
5267

5368
export interface WebpackPreloadEntryPoint {

packages/plugin/webpack/src/WebpackConfig.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export default class WebpackConfigGenerator {
195195
devtool: this.rendererSourceMapOption,
196196
mode: this.mode,
197197
output: {
198+
...(entryPoint.output || {}),
198199
path: path.resolve(this.webpackDir, 'renderer'),
199200
filename: '[name]/index.js',
200201
globalObject: 'self',
@@ -205,9 +206,11 @@ export default class WebpackConfigGenerator {
205206
__filename: false,
206207
},
207208
plugins: [
209+
...(entryPoint.htmlPlugins || []),
208210
...(entryPoint.html
209211
? [
210212
new HtmlWebpackPlugin({
213+
...(entryPoint.htmlOptions || {}),
211214
title: entryPoint.name,
212215
template: entryPoint.html,
213216
filename: `${entryPoint.name}/index.html`,

packages/plugin/webpack/test/WebpackConfig_spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,37 @@ describe('WebpackConfigGenerator', () => {
643643
await generator.getRendererConfig(config.renderer.entryPoints);
644644
expect(getInvokedCounter()).to.equal(2);
645645
});
646+
647+
it('honors custom entrypoint output options', async () => {
648+
const { MyWebpackConfigGenerator } = makeSubclass();
649+
650+
const config = {
651+
mainConfig: () => ({
652+
entry: 'main.js',
653+
...sampleWebpackConfig,
654+
}),
655+
renderer: {
656+
config: { ...sampleWebpackConfig },
657+
entryPoints: [
658+
{
659+
name: 'main',
660+
js: 'rendererScript.js',
661+
output: {
662+
crossorigin: 'anonymous',
663+
},
664+
},
665+
],
666+
},
667+
} as WebpackPluginConfig;
668+
669+
const generator = new MyWebpackConfigGenerator(config, mockProjectDir, false, 3000);
670+
671+
const rendererConfig = await generator.getRendererConfig(config.renderer.entryPoints);
672+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
673+
const outputSettings = rendererConfig[0].output as any;
674+
expect(outputSettings).not.to.be.undefined;
675+
expect(outputSettings['crossorigin']).to.equal('anonymous');
676+
});
646677
});
647678
});
648679
});

0 commit comments

Comments
 (0)