@@ -10,15 +10,16 @@ import { readFile } from 'node:fs/promises';
1010import { join } from 'node:path' ;
1111import { NormalizedCachedOptions } from '../normalize-cache' ;
1212import { NormalizedOptimizationOptions } from '../normalize-optimization' ;
13+ import { addEventDispatchContract } from './add-event-dispatch-contract' ;
1314import { CrossOriginValue , Entrypoint , FileInfo , augmentIndexHtml } from './augment-index-html' ;
1415import { InlineCriticalCssProcessor } from './inline-critical-css' ;
1516import { InlineFontsProcessor } from './inline-fonts' ;
16- import { addStyleNonce } from './style- nonce' ;
17+ import { addNonce } from './nonce' ;
1718
1819type IndexHtmlGeneratorPlugin = (
1920 html : string ,
2021 options : IndexHtmlGeneratorProcessOptions ,
21- ) => Promise < string | IndexHtmlTransformResult > ;
22+ ) => Promise < string | IndexHtmlPluginTransformResult > | string ;
2223
2324export type HintMode = 'prefetch' | 'preload' | 'modulepreload' | 'preconnect' | 'dns-prefetch' ;
2425
@@ -40,45 +41,78 @@ export interface IndexHtmlGeneratorOptions {
4041 optimization ?: NormalizedOptimizationOptions ;
4142 cache ?: NormalizedCachedOptions ;
4243 imageDomains ?: string [ ] ;
44+ generateDedicatedSSRContent ?: boolean ;
4345}
4446
4547export type IndexHtmlTransform = ( content : string ) => Promise < string > ;
4648
47- export interface IndexHtmlTransformResult {
49+ export interface IndexHtmlPluginTransformResult {
4850 content : string ;
4951 warnings : string [ ] ;
5052 errors : string [ ] ;
5153}
5254
55+ export interface IndexHtmlProcessResult {
56+ csrContent : string ;
57+ ssrContent ?: string ;
58+ warnings : string [ ] ;
59+ errors : string [ ] ;
60+ }
61+
5362export class IndexHtmlGenerator {
5463 private readonly plugins : IndexHtmlGeneratorPlugin [ ] ;
64+ private readonly csrPlugins : IndexHtmlGeneratorPlugin [ ] = [ ] ;
65+ private readonly ssrPlugins : IndexHtmlGeneratorPlugin [ ] = [ ] ;
5566
5667 constructor ( readonly options : IndexHtmlGeneratorOptions ) {
57- const extraPlugins : IndexHtmlGeneratorPlugin [ ] = [ ] ;
58- if ( this . options . optimization ?. fonts . inline ) {
59- extraPlugins . push ( inlineFontsPlugin ( this ) ) ;
68+ const extraCommonPlugins : IndexHtmlGeneratorPlugin [ ] = [ ] ;
69+ if ( options ? .optimization ?. fonts . inline ) {
70+ extraCommonPlugins . push ( inlineFontsPlugin ( this ) , addNonce ) ;
6071 }
6172
62- if ( this . options . optimization ?. styles . inlineCritical ) {
63- extraPlugins . push ( inlineCriticalCssPlugin ( this ) ) ;
73+ // Common plugins
74+ this . plugins = [ augmentIndexHtmlPlugin ( this ) , ...extraCommonPlugins , postTransformPlugin ( this ) ] ;
75+
76+ // CSR plugins
77+ if ( options ?. optimization ?. styles ?. inlineCritical ) {
78+ this . csrPlugins . push ( inlineCriticalCssPlugin ( this ) ) ;
6479 }
6580
66- this . plugins = [
67- augmentIndexHtmlPlugin ( this ) ,
68- ...extraPlugins ,
69- // Runs after the `extraPlugins` to capture any nonce or
70- // `style` tags that might've been added by them.
71- addStyleNoncePlugin ( ) ,
72- postTransformPlugin ( this ) ,
73- ] ;
81+ // SSR plugins
82+ if ( options . generateDedicatedSSRContent ) {
83+ this . ssrPlugins . push ( addEventDispatchContractPlugin ( ) , addNoncePlugin ( ) ) ;
84+ }
7485 }
7586
76- async process ( options : IndexHtmlGeneratorProcessOptions ) : Promise < IndexHtmlTransformResult > {
87+ async process ( options : IndexHtmlGeneratorProcessOptions ) : Promise < IndexHtmlProcessResult > {
7788 let content = await this . readIndex ( this . options . indexPath ) ;
7889 const warnings : string [ ] = [ ] ;
7990 const errors : string [ ] = [ ] ;
8091
81- for ( const plugin of this . plugins ) {
92+ content = await this . runPlugins ( content , this . plugins , options , warnings , errors ) ;
93+ const [ csrContent , ssrContent ] = await Promise . all ( [
94+ this . runPlugins ( content , this . csrPlugins , options , warnings , errors ) ,
95+ this . ssrPlugins . length
96+ ? this . runPlugins ( content , this . ssrPlugins , options , warnings , errors )
97+ : undefined ,
98+ ] ) ;
99+
100+ return {
101+ ssrContent,
102+ csrContent,
103+ warnings,
104+ errors,
105+ } ;
106+ }
107+
108+ private async runPlugins (
109+ content : string ,
110+ plugins : IndexHtmlGeneratorPlugin [ ] ,
111+ options : IndexHtmlGeneratorProcessOptions ,
112+ warnings : string [ ] ,
113+ errors : string [ ] ,
114+ ) : Promise < string > {
115+ for ( const plugin of plugins ) {
82116 const result = await plugin ( content , options ) ;
83117 if ( typeof result === 'string' ) {
84118 content = result ;
@@ -95,11 +129,7 @@ export class IndexHtmlGenerator {
95129 }
96130 }
97131
98- return {
99- content,
100- warnings,
101- errors,
102- } ;
132+ return content ;
103133 }
104134
105135 async readAsset ( path : string ) : Promise < string > {
@@ -160,10 +190,14 @@ function inlineCriticalCssPlugin(generator: IndexHtmlGenerator): IndexHtmlGenera
160190 inlineCriticalCssProcessor . process ( html , { outputPath : options . outputPath } ) ;
161191}
162192
163- function addStyleNoncePlugin ( ) : IndexHtmlGeneratorPlugin {
164- return ( html ) => addStyleNonce ( html ) ;
193+ function addNoncePlugin ( ) : IndexHtmlGeneratorPlugin {
194+ return ( html ) => addNonce ( html ) ;
165195}
166196
167197function postTransformPlugin ( { options } : IndexHtmlGenerator ) : IndexHtmlGeneratorPlugin {
168198 return async ( html ) => ( options . postTransform ? options . postTransform ( html ) : html ) ;
169199}
200+
201+ function addEventDispatchContractPlugin ( ) : IndexHtmlGeneratorPlugin {
202+ return ( html ) => addEventDispatchContract ( html ) ;
203+ }
0 commit comments