Skip to content

Commit 5e7cd98

Browse files
authored
Merge pull request #3003 from modernweb-dev/feat/transform-input-html
feat(rollup-plugin-html): suport transform on input html
2 parents d37ca46 + 44fff34 commit 5e7cd98

File tree

6 files changed

+69
-22
lines changed

6 files changed

+69
-22
lines changed

.changeset/light-comics-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/rollup-plugin-html': minor
3+
---
4+
5+
support transform on input html

packages/rollup-plugin-html/src/output/createHTMLOutput.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export interface CreateHTMLAssetParams {
1515
input: InputData;
1616
emittedAssets: EmittedAssets;
1717
generatedBundles: GeneratedBundle[];
18-
externalTransformHtmlFns: TransformHtmlFunction[];
18+
inputExternalTransformHtmlFns: TransformHtmlFunction[];
19+
outputExternalTransformHtmlFns: TransformHtmlFunction[];
1920
pluginOptions: RollupPluginHTMLOptions;
2021
defaultInjectDisabled: boolean;
2122
serviceWorkerPath: string;
@@ -30,7 +31,8 @@ export async function createHTMLAsset(params: CreateHTMLAssetParams): Promise<Em
3031
input,
3132
emittedAssets,
3233
generatedBundles,
33-
externalTransformHtmlFns,
34+
inputExternalTransformHtmlFns,
35+
outputExternalTransformHtmlFns,
3436
pluginOptions,
3537
defaultInjectDisabled,
3638
serviceWorkerPath,
@@ -57,7 +59,8 @@ export async function createHTMLAsset(params: CreateHTMLAssetParams): Promise<Em
5759
input,
5860
outputDir,
5961
emittedAssets,
60-
externalTransformHtmlFns,
62+
inputExternalTransformHtmlFns,
63+
outputExternalTransformHtmlFns,
6164
defaultInjectDisabled,
6265
serviceWorkerPath,
6366
injectServiceWorker,
@@ -73,7 +76,8 @@ export interface CreateHTMLAssetsParams {
7376
inputs: InputData[];
7477
emittedAssets: EmittedAssets;
7578
generatedBundles: GeneratedBundle[];
76-
externalTransformHtmlFns: TransformHtmlFunction[];
79+
inputExternalTransformHtmlFns: TransformHtmlFunction[];
80+
outputExternalTransformHtmlFns: TransformHtmlFunction[];
7781
pluginOptions: RollupPluginHTMLOptions;
7882
defaultInjectDisabled: boolean;
7983
serviceWorkerPath: string;

packages/rollup-plugin-html/src/output/getOutputHTML.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export interface GetOutputHTMLParams {
1919
emittedAssets: EmittedAssets;
2020
pluginOptions: RollupPluginHTMLOptions;
2121
entrypointBundles: Record<string, EntrypointBundle>;
22-
externalTransformHtmlFns?: TransformHtmlFunction[];
22+
inputExternalTransformHtmlFns?: TransformHtmlFunction[];
23+
outputExternalTransformHtmlFns?: TransformHtmlFunction[];
2324
defaultInjectDisabled: boolean;
2425
serviceWorkerPath: string;
2526
injectServiceWorker: boolean;
@@ -31,7 +32,8 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
3132
const {
3233
pluginOptions,
3334
entrypointBundles,
34-
externalTransformHtmlFns,
35+
inputExternalTransformHtmlFns,
36+
outputExternalTransformHtmlFns,
3537
input,
3638
outputDir,
3739
emittedAssets,
@@ -44,8 +46,20 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
4446
const { default: defaultBundle, ...multiBundles } = entrypointBundles;
4547
const { absoluteSocialMediaUrls = true, rootDir = process.cwd() } = pluginOptions;
4648

49+
let inputHtml = input.html;
50+
51+
// run transform functions on input HTML
52+
const inputTransforms = [...(inputExternalTransformHtmlFns ?? [])];
53+
for (const transform of inputTransforms) {
54+
inputHtml = await transform(inputHtml, {
55+
bundle: defaultBundle,
56+
bundles: multiBundles,
57+
htmlFileName: input.name,
58+
});
59+
}
60+
4761
// inject rollup output into HTML
48-
let document = parse(input.html);
62+
let document = parse(inputHtml);
4963
if (pluginOptions.extractAssets !== false) {
5064
injectedUpdatedAssetPaths({
5165
document,
@@ -75,17 +89,17 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
7589

7690
let outputHtml = serialize(document);
7791

78-
const transforms = [...(externalTransformHtmlFns ?? [])];
92+
const outputTransforms = [...(outputExternalTransformHtmlFns ?? [])];
7993
if (pluginOptions.transformHtml) {
8094
if (Array.isArray(pluginOptions.transformHtml)) {
81-
transforms.push(...pluginOptions.transformHtml);
95+
outputTransforms.push(...pluginOptions.transformHtml);
8296
} else {
83-
transforms.push(pluginOptions.transformHtml);
97+
outputTransforms.push(pluginOptions.transformHtml);
8498
}
8599
}
86100

87101
// run transform functions on output HTML
88-
for (const transform of transforms) {
102+
for (const transform of outputTransforms) {
89103
outputHtml = await transform(outputHtml, {
90104
bundle: defaultBundle,
91105
bundles: multiBundles,

packages/rollup-plugin-html/src/rollupPluginHTML.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import { emitAssets } from './output/emitAssets.js';
1818
export interface RollupPluginHtml extends Plugin {
1919
api: {
2020
getInputs(): InputData[];
21-
addHtmlTransformer(transformHtmlFunction: TransformHtmlFunction): void;
21+
addHtmlTransformer(
22+
transformHtmlFunction: TransformHtmlFunction,
23+
transformStage?: 'input' | 'output',
24+
): void;
2225
disableDefaultInject(): void;
2326
addOutput(name: string): Plugin;
2427
};
@@ -28,7 +31,8 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
2831
const multiOutputNames: string[] = [];
2932
let inputs: InputData[] = [];
3033
let generatedBundles: GeneratedBundle[] = [];
31-
let externalTransformHtmlFns: TransformHtmlFunction[] = [];
34+
let inputExternalTransformHtmlFns: TransformHtmlFunction[] = [];
35+
let outputExternalTransformHtmlFns: TransformHtmlFunction[] = [];
3236
let defaultInjectDisabled = false;
3337
let serviceWorkerPath = '';
3438
let injectServiceWorker = false;
@@ -38,7 +42,8 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
3842
function reset() {
3943
inputs = [];
4044
generatedBundles = [];
41-
externalTransformHtmlFns = [];
45+
inputExternalTransformHtmlFns = [];
46+
outputExternalTransformHtmlFns = [];
4247
}
4348

4449
return {
@@ -146,7 +151,8 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
146151
inputs,
147152
emittedAssets,
148153
generatedBundles,
149-
externalTransformHtmlFns,
154+
inputExternalTransformHtmlFns,
155+
outputExternalTransformHtmlFns,
150156
pluginOptions,
151157
defaultInjectDisabled,
152158
serviceWorkerPath,
@@ -165,8 +171,15 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
165171
return inputs;
166172
},
167173

168-
addHtmlTransformer(transformHtmlFunction: TransformHtmlFunction) {
169-
externalTransformHtmlFns.push(transformHtmlFunction);
174+
addHtmlTransformer(
175+
transformHtmlFunction: TransformHtmlFunction,
176+
transformStage: 'input' | 'output' = 'output',
177+
) {
178+
if (transformStage === 'input') {
179+
inputExternalTransformHtmlFns.push(transformHtmlFunction);
180+
} else {
181+
outputExternalTransformHtmlFns.push(transformHtmlFunction);
182+
}
170183
},
171184

172185
disableDefaultInject() {
@@ -205,7 +218,8 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
205218
inputs,
206219
emittedAssets,
207220
generatedBundles,
208-
externalTransformHtmlFns,
221+
inputExternalTransformHtmlFns,
222+
outputExternalTransformHtmlFns,
209223
pluginOptions,
210224
defaultInjectDisabled,
211225
serviceWorkerPath,

packages/rollup-plugin-html/test/rollup-plugin-html.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,16 @@ describe('rollup-plugin-html', () => {
626626
return false;
627627
});
628628
plugin!.api.addHtmlTransformer((html: string) =>
629-
html.replace('</body>', '<!-- injected --></body>'),
629+
html.replace('</body>', '<!-- injected to output --></body>'),
630+
);
631+
plugin!.api.addHtmlTransformer(
632+
(html: string) =>
633+
html.replace('<!-- injected to output -->', '<!-- injected to output 2 -->'),
634+
'output',
635+
);
636+
plugin!.api.addHtmlTransformer(
637+
(html: string) => html.replace('</body>', '<!-- injected to input --></body>'),
638+
'input',
630639
);
631640
},
632641
} as Plugin,
@@ -641,9 +650,10 @@ describe('rollup-plugin-html', () => {
641650
expect(entryB).to.include("console.log('entrypoint-b.js');");
642651
expect(stripNewlines(getAsset(output, 'index.html').source)).to.equal(
643652
'<html><head></head><body><h1>hello world</h1>' +
653+
'<!-- injected to input -->' +
644654
'<script type="module" src="./entrypoint-a.js"></script>' +
645655
'<script type="module" src="./entrypoint-b.js"></script>' +
646-
'<!-- injected --></body></html>',
656+
'<!-- injected to output 2 --></body></html>',
647657
);
648658
});
649659

packages/rollup-plugin-html/test/src/output/getOutputHTML.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ describe('getOutputHTML()', () => {
105105
);
106106
});
107107

108-
it('can combine external and regular transform functions', async () => {
108+
it('can combine external and regular output transform functions', async () => {
109109
const output = await getOutputHTML({
110110
...defaultOptions,
111111
pluginOptions: {
112112
...defaultOptions.pluginOptions,
113113
transformHtml: html => html.replace('Input HTML', 'Transformed Input HTML'),
114114
},
115-
externalTransformHtmlFns: [html => html.replace(/h1/g, 'h2')],
115+
outputExternalTransformHtmlFns: [html => html.replace(/h1/g, 'h2')],
116116
});
117117

118118
expect(output).to.equal(

0 commit comments

Comments
 (0)