Skip to content

Commit 1b8ae31

Browse files
committed
feat: add ability to customize 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 1b8ae31

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
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: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,32 @@ export default class WebpackConfigGenerator {
194194
target: this.rendererTarget(entryPoint),
195195
devtool: this.rendererSourceMapOption,
196196
mode: this.mode,
197-
output: {
197+
output: Object.assign(entryPoint.output || {}, {
198198
path: path.resolve(this.webpackDir, 'renderer'),
199199
filename: '[name]/index.js',
200200
globalObject: 'self',
201201
...(this.isProd ? {} : { publicPath: '/' }),
202-
},
202+
}),
203203
node: {
204204
__dirname: false,
205205
__filename: false,
206206
},
207207
plugins: [
208+
...(entryPoint.htmlPlugins || []),
208209
...(entryPoint.html
209210
? [
210-
new HtmlWebpackPlugin({
211-
title: entryPoint.name,
212-
template: entryPoint.html,
213-
filename: `${entryPoint.name}/index.html`,
214-
chunks: [entryPoint.name].concat(entryPoint.additionalChunks || []),
215-
}) as WebpackPluginInstance,
211+
new HtmlWebpackPlugin(
212+
Object.assign(
213+
{},
214+
{
215+
title: entryPoint.name,
216+
template: entryPoint.html,
217+
filename: `${entryPoint.name}/index.html`,
218+
chunks: [entryPoint.name].concat(entryPoint.additionalChunks || []),
219+
},
220+
entryPoint.htmlOptions || {}
221+
)
222+
) as WebpackPluginInstance,
216223
]
217224
: []),
218225
new webpack.DefinePlugin(defines),

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)