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

feat: allow to read htmlWebpackPlugin.options.title in template #960

Merged
merged 1 commit into from
Dec 15, 2023
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
20 changes: 20 additions & 0 deletions e2e/cases/html/title/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ test('should generate title correctly when using custom HTML template', async ()
expect(html).toContain('<title>foo</title>');
});

test('should generate title correctly when using htmlWebpackPlugin.options.title', async () => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
source: {
entry: { foo: path.resolve(__dirname, './src/foo.js') },
},
html: {
title: 'foo',
template: path.resolve(__dirname, './src/plugin-options-title.html'),
},
},
});
const files = await rsbuild.unwrapOutputJSON();

const html =
files[Object.keys(files).find((file) => file.endsWith('foo.html'))!];
expect(html).toContain('<title>foo</title>');
});

test('should generate title via function correctly', async () => {
const rsbuild = await build({
cwd: __dirname,
Expand Down
5 changes: 5 additions & 0 deletions e2e/cases/html/title/src/plugin-options-title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<head>
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ exports[`webpackConfig > should allow to append and prepend plugins 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -979,16 +978,15 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export const pluginHtml = (): RsbuildPlugin => ({

const title = getTitle(entryName, config);
if (title) {
htmlInfo.title = title;
pluginOptions.title = title;
}

const favicon = getFavicon(entryName, config);
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/rspack/HtmlBasicPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
import type { Compiler, Compilation } from '@rspack/core';

export type HtmlInfo = {
title?: string;
favicon?: string;
templateContent?: string;
};
Expand All @@ -27,9 +26,8 @@ export class HtmlBasicPlugin {
apply(compiler: Compiler) {
const addTitleTag = (
headTags: HtmlWebpackPlugin.HtmlTagObject[],
entryName: string,
title?: string,
) => {
const { title } = this.options.info[entryName];
if (title) {
headTags.unshift({
tagName: 'title',
Expand Down Expand Up @@ -74,7 +72,7 @@ export class HtmlBasicPlugin {
const { templateContent } = this.options.info[entryName];

if (!hasTitle(templateContent)) {
addTitleTag(headTags, entryName);
addTitleTag(headTags, data.plugin.options?.title);
}

addFavicon(headTags, entryName);
Expand Down
5 changes: 2 additions & 3 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -646,16 +646,15 @@ exports[`should use rspack as default bundler > apply rspack correctly 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down
54 changes: 21 additions & 33 deletions packages/core/tests/plugins/__snapshots__/html.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports[`plugin-html > should add one tags plugin instance 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
Expand All @@ -50,19 +51,16 @@ exports[`plugin-html > should add one tags plugin instance 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"foo": {
"title": "Rsbuild App",
},
"main": {
"title": "Rsbuild App",
},
"foo": {},
"main": {},
},
},
},
Expand Down Expand Up @@ -118,6 +116,7 @@ exports[`plugin-html > should add tags plugin instances for each entries 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
Expand All @@ -139,19 +138,16 @@ exports[`plugin-html > should add tags plugin instances for each entries 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"foo": {
"title": "Rsbuild App",
},
"main": {
"title": "Rsbuild App",
},
"foo": {},
"main": {},
},
},
},
Expand Down Expand Up @@ -221,9 +217,7 @@ exports[`plugin-html > should allow to modify plugin options by tools.htmlPlugin
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -258,16 +252,15 @@ exports[`plugin-html > should allow to set favicon by html.favicon option 1`] =
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -301,16 +294,15 @@ exports[`plugin-html > should allow to set inject by html.inject option 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -363,16 +355,15 @@ exports[`plugin-html > should enable minify in production 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -406,16 +397,15 @@ exports[`plugin-html > should register html plugin correctly 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -452,6 +442,7 @@ exports[`plugin-html > should support multi entry 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
Expand All @@ -473,19 +464,16 @@ exports[`plugin-html > should support multi entry 1`] = `
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"foo": {
"title": "Rsbuild App",
},
"main": {
"title": "Rsbuild App",
},
"foo": {},
"main": {},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,15 @@ exports[`plugin-inline-chunk > should use proper plugin options when inlineScrip
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down Expand Up @@ -119,16 +118,15 @@ exports[`plugin-inline-chunk > should use proper plugin options when inlineStyle
"scriptLoading": "defer",
"template": "<ROOT>/packages/core/static/template.html",
"templateParameters": [Function],
"title": "Rsbuild App",
},
"version": 5,
},
HtmlBasicPlugin {
"name": "HtmlBasicPlugin",
"options": {
"info": {
"index": {
"title": "Rsbuild App",
},
"index": {},
},
},
},
Expand Down
Loading