Skip to content

Commit

Permalink
feat(html): support js template without child compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Aug 28, 2024
1 parent 4e6f898 commit 6ad6f10
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
67 changes: 57 additions & 10 deletions packages/rspack/src/builtin-plugin/HtmlRspackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import type { Compiler } from "../Compiler";
import { validate } from "../util/validate";
import { create } from "./base";

type HtmlPluginTag = {
tagName: string;
attributes: Record<string, string>;
voidTag: boolean;
innerHTML?: string;
toString?: () => string;
};

const templateRenderFunction = z
.function()
.args(z.record(z.string(), z.any()))
Expand Down Expand Up @@ -104,6 +112,32 @@ export const HtmlRspackPlugin = create(
compilation = c;
});

function generateRenderData(data: string): Record<string, unknown> {
const json = JSON.parse(data);
if (typeof c.templateParameters !== "function") {
json.compilation = compilation;
}
const renderTag = function (this: HtmlPluginTag) {
return htmlTagObjectToString(this);
};
const renderTagList = function (this: HtmlPluginTag[]) {
return this.join("");
};
if (Array.isArray(json.htmlRspackPlugin?.tags?.headTags)) {
for (const tag of json.htmlRspackPlugin.tags.headTags) {
tag.toString = renderTag;
}
json.htmlRspackPlugin.tags.headTags.toString = renderTagList;
}
if (Array.isArray(json.htmlRspackPlugin?.tags?.bodyTags)) {
for (const tag of json.htmlRspackPlugin.tags.bodyTags) {
tag.toString = renderTag;
}
json.htmlRspackPlugin.tags.bodyTags.toString = renderTagList;
}
return json;
}

let templateContent = c.templateContent;
let templateFn = undefined;
if (typeof templateContent === "function") {
Expand All @@ -115,11 +149,7 @@ export const HtmlRspackPlugin = create(
if (c.templateParameters === false) {
return await renderer({});
}
const json = JSON.parse(data);
if (typeof c.templateParameters !== "function") {
json.compilation = compilation;
}
return await renderer(json);
return await renderer(generateRenderData(data));
} catch (e) {
const error = new Error(
`HtmlRspackPlugin: render template function failed, ${(e as Error).message}`
Expand Down Expand Up @@ -147,11 +177,7 @@ export const HtmlRspackPlugin = create(
if (c.templateParameters === false) {
return await renderer({});
}
const json = JSON.parse(data);
if (typeof c.templateParameters !== "function") {
json.compilation = compilation;
}
return await renderer(json);
return await renderer(generateRenderData(data));
} catch (e) {
const error = new Error(
`HtmlRspackPlugin: render template function failed, ${(e as Error).message}`
Expand Down Expand Up @@ -186,3 +212,24 @@ export const HtmlRspackPlugin = create(
};
}
);

function htmlTagObjectToString(tag: {
tagName: string;
attributes: Record<string, string>;
voidTag: boolean;
innerHTML?: string;
}) {
const attributes = Object.keys(tag.attributes || {})
.filter(
attributeName =>
tag.attributes[attributeName] === "" || tag.attributes[attributeName]
)
.map(attributeName => {
if (tag.attributes[attributeName] === "true") {
return attributeName;
}
return `${attributeName}="${tag.attributes[attributeName]}"`;
});
const res = `<${[tag.tagName].concat(attributes).join(" ")}${tag.voidTag && !tag.innerHTML ? "/" : ""}>${tag.innerHTML || ""}${tag.voidTag && !tag.innerHTML ? "" : `</${tag.tagName}>`}`;
return res;
}
8 changes: 4 additions & 4 deletions tests/plugin-test/html-plugin/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3600,8 +3600,8 @@ describe("HtmlWebpackPlugin", () => {
// `,
templateContent: ({ htmlRspackPlugin }) => `
<html>
<head>${htmlRspackPlugin.tags.headTagsHtml}</head>
<body>${htmlRspackPlugin.tags.bodyTagsHtml}</body>
<head>${htmlRspackPlugin.tags.headTags}</head>
<body>${htmlRspackPlugin.tags.bodyTags}</body>
</html>
`,
}),
Expand Down Expand Up @@ -3679,8 +3679,8 @@ describe("HtmlWebpackPlugin", () => {
// `,
templateContent: ({ htmlRspackPlugin }) => `
<html>
<head>${htmlRspackPlugin.tags.headTagsHtml}</head>
<body>${htmlRspackPlugin.tags.bodyTagsHtml}</body>
<head>${htmlRspackPlugin.tags.headTags}</head>
<body>${htmlRspackPlugin.tags.bodyTags}</body>
</html>
`,
}),
Expand Down

0 comments on commit 6ad6f10

Please sign in to comment.