Skip to content

Commit 21b6a1b

Browse files
authored
Merge pull request #3006 from modernweb-dev/fx/revert-transform-input-html
fix(rollup-plugin-html): revert suport transform on input html
2 parents ea49352 + bd12d3c commit 21b6a1b

File tree

6 files changed

+27
-64
lines changed

6 files changed

+27
-64
lines changed

.changeset/tall-experts-wait.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': patch
3+
---
4+
5+
revert support transform on input html

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ export interface CreateHTMLAssetParams {
1515
input: InputData;
1616
emittedAssets: EmittedAssets;
1717
generatedBundles: GeneratedBundle[];
18-
inputExternalTransformHtmlFns: TransformHtmlFunction[];
19-
outputExternalTransformHtmlFns: TransformHtmlFunction[];
18+
externalTransformHtmlFns: TransformHtmlFunction[];
2019
pluginOptions: RollupPluginHTMLOptions;
2120
defaultInjectDisabled: boolean;
2221
serviceWorkerPath: string;
@@ -31,8 +30,7 @@ export async function createHTMLAsset(params: CreateHTMLAssetParams): Promise<Em
3130
input,
3231
emittedAssets,
3332
generatedBundles,
34-
inputExternalTransformHtmlFns,
35-
outputExternalTransformHtmlFns,
33+
externalTransformHtmlFns,
3634
pluginOptions,
3735
defaultInjectDisabled,
3836
serviceWorkerPath,
@@ -59,8 +57,7 @@ export async function createHTMLAsset(params: CreateHTMLAssetParams): Promise<Em
5957
input,
6058
outputDir,
6159
emittedAssets,
62-
inputExternalTransformHtmlFns,
63-
outputExternalTransformHtmlFns,
60+
externalTransformHtmlFns,
6461
defaultInjectDisabled,
6562
serviceWorkerPath,
6663
injectServiceWorker,
@@ -76,8 +73,7 @@ export interface CreateHTMLAssetsParams {
7673
inputs: InputData[];
7774
emittedAssets: EmittedAssets;
7875
generatedBundles: GeneratedBundle[];
79-
inputExternalTransformHtmlFns: TransformHtmlFunction[];
80-
outputExternalTransformHtmlFns: TransformHtmlFunction[];
76+
externalTransformHtmlFns: TransformHtmlFunction[];
8177
pluginOptions: RollupPluginHTMLOptions;
8278
defaultInjectDisabled: boolean;
8379
serviceWorkerPath: string;

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export interface GetOutputHTMLParams {
1919
emittedAssets: EmittedAssets;
2020
pluginOptions: RollupPluginHTMLOptions;
2121
entrypointBundles: Record<string, EntrypointBundle>;
22-
inputExternalTransformHtmlFns?: TransformHtmlFunction[];
23-
outputExternalTransformHtmlFns?: TransformHtmlFunction[];
22+
externalTransformHtmlFns?: TransformHtmlFunction[];
2423
defaultInjectDisabled: boolean;
2524
serviceWorkerPath: string;
2625
injectServiceWorker: boolean;
@@ -32,8 +31,7 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
3231
const {
3332
pluginOptions,
3433
entrypointBundles,
35-
inputExternalTransformHtmlFns,
36-
outputExternalTransformHtmlFns,
34+
externalTransformHtmlFns,
3735
input,
3836
outputDir,
3937
emittedAssets,
@@ -46,20 +44,8 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
4644
const { default: defaultBundle, ...multiBundles } = entrypointBundles;
4745
const { absoluteSocialMediaUrls = true, rootDir = process.cwd() } = pluginOptions;
4846

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-
6147
// inject rollup output into HTML
62-
let document = parse(inputHtml);
48+
let document = parse(input.html);
6349
if (pluginOptions.extractAssets !== false) {
6450
injectedUpdatedAssetPaths({
6551
document,
@@ -89,17 +75,17 @@ export async function getOutputHTML(params: GetOutputHTMLParams) {
8975

9076
let outputHtml = serialize(document);
9177

92-
const outputTransforms = [...(outputExternalTransformHtmlFns ?? [])];
78+
const transforms = [...(externalTransformHtmlFns ?? [])];
9379
if (pluginOptions.transformHtml) {
9480
if (Array.isArray(pluginOptions.transformHtml)) {
95-
outputTransforms.push(...pluginOptions.transformHtml);
81+
transforms.push(...pluginOptions.transformHtml);
9682
} else {
97-
outputTransforms.push(pluginOptions.transformHtml);
83+
transforms.push(pluginOptions.transformHtml);
9884
}
9985
}
10086

10187
// run transform functions on output HTML
102-
for (const transform of outputTransforms) {
88+
for (const transform of transforms) {
10389
outputHtml = await transform(outputHtml, {
10490
bundle: defaultBundle,
10591
bundles: multiBundles,

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import { emitAssets } from './output/emitAssets.js';
1818
export interface RollupPluginHtml extends Plugin {
1919
api: {
2020
getInputs(): InputData[];
21-
addHtmlTransformer(
22-
transformHtmlFunction: TransformHtmlFunction,
23-
transformStage?: 'input' | 'output',
24-
): void;
21+
addHtmlTransformer(transformHtmlFunction: TransformHtmlFunction): void;
2522
disableDefaultInject(): void;
2623
addOutput(name: string): Plugin;
2724
};
@@ -31,8 +28,7 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
3128
const multiOutputNames: string[] = [];
3229
let inputs: InputData[] = [];
3330
let generatedBundles: GeneratedBundle[] = [];
34-
let inputExternalTransformHtmlFns: TransformHtmlFunction[] = [];
35-
let outputExternalTransformHtmlFns: TransformHtmlFunction[] = [];
31+
let externalTransformHtmlFns: TransformHtmlFunction[] = [];
3632
let defaultInjectDisabled = false;
3733
let serviceWorkerPath = '';
3834
let injectServiceWorker = false;
@@ -42,8 +38,7 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
4238
function reset() {
4339
inputs = [];
4440
generatedBundles = [];
45-
inputExternalTransformHtmlFns = [];
46-
outputExternalTransformHtmlFns = [];
41+
externalTransformHtmlFns = [];
4742
}
4843

4944
return {
@@ -151,8 +146,7 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
151146
inputs,
152147
emittedAssets,
153148
generatedBundles,
154-
inputExternalTransformHtmlFns,
155-
outputExternalTransformHtmlFns,
149+
externalTransformHtmlFns,
156150
pluginOptions,
157151
defaultInjectDisabled,
158152
serviceWorkerPath,
@@ -171,15 +165,8 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
171165
return inputs;
172166
},
173167

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-
}
168+
addHtmlTransformer(transformHtmlFunction: TransformHtmlFunction) {
169+
externalTransformHtmlFns.push(transformHtmlFunction);
183170
},
184171

185172
disableDefaultInject() {
@@ -218,8 +205,7 @@ export function rollupPluginHTML(pluginOptions: RollupPluginHTMLOptions = {}): R
218205
inputs,
219206
emittedAssets,
220207
generatedBundles,
221-
inputExternalTransformHtmlFns,
222-
outputExternalTransformHtmlFns,
208+
externalTransformHtmlFns,
223209
pluginOptions,
224210
defaultInjectDisabled,
225211
serviceWorkerPath,

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -626,16 +626,7 @@ describe('rollup-plugin-html', () => {
626626
return false;
627627
});
628628
plugin!.api.addHtmlTransformer((html: string) =>
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',
629+
html.replace('</body>', '<!-- injected --></body>'),
639630
);
640631
},
641632
} as Plugin,
@@ -650,10 +641,9 @@ describe('rollup-plugin-html', () => {
650641
expect(entryB).to.include("console.log('entrypoint-b.js');");
651642
expect(stripNewlines(getAsset(output, 'index.html').source)).to.equal(
652643
'<html><head></head><body><h1>hello world</h1>' +
653-
'<!-- injected to input -->' +
654644
'<script type="module" src="./entrypoint-a.js"></script>' +
655645
'<script type="module" src="./entrypoint-b.js"></script>' +
656-
'<!-- injected to output 2 --></body></html>',
646+
'<!-- injected --></body></html>',
657647
);
658648
});
659649

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 output transform functions', async () => {
108+
it('can combine external and regular 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-
outputExternalTransformHtmlFns: [html => html.replace(/h1/g, 'h2')],
115+
externalTransformHtmlFns: [html => html.replace(/h1/g, 'h2')],
116116
});
117117

118118
expect(output).to.equal(

0 commit comments

Comments
 (0)