11import type { Rspack } from '@modern-js/builder' ;
2+ import { RUNTIME_CHUNK_REGEX } from '@modern-js/builder' ;
23
34export class HtmlAsyncChunkPlugin {
45 name : string ;
@@ -15,19 +16,37 @@ export class HtmlAsyncChunkPlugin {
1516 const hooks = this . htmlWebpackPlugin . getHooks ( compilation as any ) ;
1617
1718 hooks . alterAssetTagGroups . tap ( this . name , assets => {
18- const tags = [ ...assets . headTags , ...assets . bodyTags ] ;
19+ const headTags : typeof assets . headTags = [ ] ;
20+ const bodyTags : typeof assets . bodyTags = [ ] ;
1921
20- for ( const tag of tags ) {
22+ const processScriptTag = ( tag : ( typeof assets . headTags ) [ 0 ] ) => {
23+ const { attributes } = tag ;
24+
25+ // Convert defer to async
26+ if ( attributes && attributes . defer === true ) {
27+ attributes . async = true ;
28+ delete attributes . defer ;
29+ }
30+
31+ const src = attributes ?. src as string | undefined ;
32+ const isRuntimeChunk = src && RUNTIME_CHUNK_REGEX . test ( src ) ;
33+
34+ return isRuntimeChunk ? bodyTags : headTags ;
35+ } ;
36+
37+ for ( const tag of [ ...assets . headTags , ...assets . bodyTags ] ) {
2138 if ( tag . tagName === 'script' ) {
22- const { attributes } = tag ;
23- if ( attributes && attributes . defer === true ) {
24- attributes . async = true ;
25- delete attributes . defer ;
26- }
39+ processScriptTag ( tag ) . push ( tag ) ;
40+ } else {
41+ ( assets . headTags . includes ( tag ) ? headTags : bodyTags ) . push ( tag ) ;
2742 }
2843 }
2944
30- return assets ;
45+ return {
46+ ...assets ,
47+ headTags,
48+ bodyTags,
49+ } ;
3150 } ) ;
3251 } ) ;
3352 }
0 commit comments