|
| 1 | +import { build, rspackOnlyTest } from '@e2e/helper'; |
| 2 | +import { expect } from '@playwright/test'; |
| 3 | +import type { RsbuildPlugin } from '@rsbuild/core'; |
| 4 | + |
| 5 | +rspackOnlyTest('should allow plugin to modify HTML content', async () => { |
| 6 | + const myPlugin: RsbuildPlugin = { |
| 7 | + name: 'my-plugin', |
| 8 | + setup(api) { |
| 9 | + api.modifyHTML((html, { compilation, filename }) => { |
| 10 | + return html.replace( |
| 11 | + '<body>', |
| 12 | + `<body> |
| 13 | + <div>${filename}</div> |
| 14 | + <div>assets: ${Object.keys(compilation.assets).length}</div> |
| 15 | + `, |
| 16 | + ); |
| 17 | + }); |
| 18 | + }, |
| 19 | + }; |
| 20 | + |
| 21 | + const rsbuild = await build({ |
| 22 | + cwd: __dirname, |
| 23 | + rsbuildConfig: { |
| 24 | + plugins: [myPlugin], |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + const files = await rsbuild.getDistFiles(); |
| 29 | + const indexHTML = Object.keys(files).find( |
| 30 | + (file) => file.includes('index') && file.endsWith('.html'), |
| 31 | + ); |
| 32 | + |
| 33 | + const html = files[indexHTML!]; |
| 34 | + |
| 35 | + expect(html.includes('<div>index.html</div>')).toBeTruthy(); |
| 36 | + expect(html.includes('<div>assets: 2</div>')).toBeTruthy(); |
| 37 | +}); |
| 38 | + |
| 39 | +rspackOnlyTest( |
| 40 | + 'should run modifyHTML hook after modifyHTMLTags hook', |
| 41 | + async () => { |
| 42 | + const myPlugin: RsbuildPlugin = { |
| 43 | + name: 'my-plugin', |
| 44 | + setup(api) { |
| 45 | + api.modifyHTMLTags((tags) => { |
| 46 | + tags.bodyTags.push({ |
| 47 | + tag: 'div', |
| 48 | + children: 'foo', |
| 49 | + }); |
| 50 | + return tags; |
| 51 | + }); |
| 52 | + api.modifyHTML((html) => { |
| 53 | + return html.replace('foo', 'bar'); |
| 54 | + }); |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + const rsbuild = await build({ |
| 59 | + cwd: __dirname, |
| 60 | + rsbuildConfig: { |
| 61 | + plugins: [myPlugin], |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + const files = await rsbuild.getDistFiles(); |
| 66 | + const indexHTML = Object.keys(files).find( |
| 67 | + (file) => file.includes('index') && file.endsWith('.html'), |
| 68 | + ); |
| 69 | + |
| 70 | + const html = files[indexHTML!]; |
| 71 | + |
| 72 | + expect(html.includes('foo')).toBeFalsy(); |
| 73 | + expect(html.includes('bar')).toBeTruthy(); |
| 74 | + }, |
| 75 | +); |
0 commit comments