Skip to content

Commit 6565ca7

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 6565ca7

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

packages/plugin/webpack/src/Config.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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';
5+
6+
type ExtraHtmlPluginOptions = Omit<HtmlWebpackPlugin.Options, 'title' | 'template' | 'filename' | 'chunks'>;
47

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

5370
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)