@@ -282,7 +282,9 @@ function handleParseError(
282
282
* Compiles index.html into an entry js module
283
283
*/
284
284
export function buildHtmlPlugin ( config : ResolvedConfig ) : Plugin {
285
- const [ preHooks , postHooks ] = resolveHtmlTransforms ( config . plugins )
285
+ const [ preHooks , normalHooks , postHooks ] = resolveHtmlTransforms (
286
+ config . plugins
287
+ )
286
288
preHooks . unshift ( preImportMapHook ( config ) )
287
289
postHooks . push ( postImportMapHook ( ) )
288
290
const processedHtml = new Map < string , string > ( )
@@ -786,12 +788,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
786
788
if ( s ) {
787
789
result = s . toString ( )
788
790
}
789
- result = await applyHtmlTransforms ( result , postHooks , {
790
- path : '/' + relativeUrlPath ,
791
- filename : id ,
792
- bundle,
793
- chunk
794
- } )
791
+ result = await applyHtmlTransforms (
792
+ result ,
793
+ [ ...normalHooks , ...postHooks ] ,
794
+ {
795
+ path : '/' + relativeUrlPath ,
796
+ filename : id ,
797
+ bundle,
798
+ chunk
799
+ }
800
+ )
795
801
// resolve asset url references
796
802
result = result . replace ( assetUrlRE , ( _ , fileHash , postfix = '' ) => {
797
803
return toOutputAssetFilePath ( this . getFileName ( fileHash ) ) + postfix
@@ -863,9 +869,24 @@ export type IndexHtmlTransformHook = (
863
869
export type IndexHtmlTransform =
864
870
| IndexHtmlTransformHook
865
871
| {
872
+ order ?: 'pre' | 'post' | null
873
+ /**
874
+ * @deprecated renamed to `order`
875
+ */
866
876
enforce ?: 'pre' | 'post'
877
+ /**
878
+ * @deprecated renamed to `handler`
879
+ */
867
880
transform : IndexHtmlTransformHook
868
881
}
882
+ | {
883
+ order ?: 'pre' | 'post' | null
884
+ /**
885
+ * @deprecated renamed to `order`
886
+ */
887
+ enforce ?: 'pre' | 'post'
888
+ handler : IndexHtmlTransformHook
889
+ }
869
890
870
891
export function preImportMapHook (
871
892
config : ResolvedConfig
@@ -914,24 +935,40 @@ export function postImportMapHook(): IndexHtmlTransformHook {
914
935
915
936
export function resolveHtmlTransforms (
916
937
plugins : readonly Plugin [ ]
917
- ) : [ IndexHtmlTransformHook [ ] , IndexHtmlTransformHook [ ] ] {
938
+ ) : [
939
+ IndexHtmlTransformHook [ ] ,
940
+ IndexHtmlTransformHook [ ] ,
941
+ IndexHtmlTransformHook [ ]
942
+ ] {
918
943
const preHooks : IndexHtmlTransformHook [ ] = [ ]
944
+ const normalHooks : IndexHtmlTransformHook [ ] = [ ]
919
945
const postHooks : IndexHtmlTransformHook [ ] = [ ]
920
946
921
947
for ( const plugin of plugins ) {
922
948
const hook = plugin . transformIndexHtml
923
- if ( hook ) {
924
- if ( typeof hook === 'function' ) {
925
- postHooks . push ( hook )
926
- } else if ( hook . enforce === 'pre' ) {
927
- preHooks . push ( hook . transform )
949
+ if ( ! hook ) continue
950
+
951
+ if ( typeof hook === 'function' ) {
952
+ normalHooks . push ( hook )
953
+ } else {
954
+ // `enforce` had only two possible values for the `transformIndexHtml` hook
955
+ // `'pre'` and `'post'` (the default). `order` now works with three values
956
+ // to align with other hooks (`'pre'`, normal, and `'post'`). We map
957
+ // both `enforce: 'post'` to `order: undefined` to avoid a breaking change
958
+ const order = hook . order ?? ( hook . enforce === 'pre' ? 'pre' : undefined )
959
+ // @ts -expect-error union type
960
+ const handler = hook . handler ?? hook . transform
961
+ if ( order === 'pre' ) {
962
+ preHooks . push ( handler )
963
+ } else if ( order === 'post' ) {
964
+ postHooks . push ( handler )
928
965
} else {
929
- postHooks . push ( hook . transform )
966
+ normalHooks . push ( handler )
930
967
}
931
968
}
932
969
}
933
970
934
- return [ preHooks , postHooks ]
971
+ return [ preHooks , normalHooks , postHooks ]
935
972
}
936
973
937
974
export async function applyHtmlTransforms (
0 commit comments