1- import { AlterAssetTagsData , HtmlTag , HtmlWebpackPlugin } from 'html-webpack-plugin'
2- import { compilation , Compiler , Plugin } from 'webpack'
1+ import { getHooks , HtmlTagObject } from 'html-webpack-plugin'
2+ import { Chunk , ChunkGroup , Compilation , Compiler , Plugin } from 'webpack'
33
44import { BabelTarget } from './babel-target'
5- import { getAlterAssetTags , getHeadTags , getBodyTags } from './html-webpack-plugin.polyfill'
65import { PLUGIN_NAME } from './plugin.name'
76import { TargetedChunkMap } from './targeted.chunk'
8- import Chunk = compilation . Chunk
9- import ChunkGroup = compilation . ChunkGroup
10- import Compilation = compilation . Compilation
117
128// Works with HtmlWebpackPlugin to make sure the targeted assets are referenced correctly
139// Tags for assets whose target has `esModule` set are updated with the `"type"="module"` attribute
@@ -20,15 +16,15 @@ export class BabelMultiTargetHtmlUpdater implements Plugin {
2016
2117 constructor ( private targets : BabelTarget [ ] ) { }
2218
23- public updateScriptTags ( chunkMap : TargetedChunkMap , tags : HtmlTag [ ] ) : void {
19+ public updateScriptTags ( chunkMap : TargetedChunkMap , tags : HtmlTagObject [ ] ) : void {
2420
2521 tags
26- . forEach ( ( tag : HtmlTag ) => {
22+ . forEach ( ( tag : HtmlTagObject ) => {
2723 if ( tag . tagName !== 'script' ) {
2824 return
2925 }
3026
31- const targetedChunks = chunkMap . get ( tag . attributes . src )
27+ const targetedChunks = chunkMap . get ( tag . attributes . src as string )
3228 // chunks that are added outside of an entry point (e.g. by HtmlWebpackIncludeAssetsPlugin) will not be targeted
3329 if ( ! targetedChunks ) {
3430 return
@@ -83,7 +79,7 @@ export class BabelMultiTargetHtmlUpdater implements Plugin {
8379 public apply ( compiler : Compiler ) : void {
8480
8581 compiler . hooks . afterPlugins . tap ( PLUGIN_NAME , ( ) => {
86- const htmlWebpackPlugin : HtmlWebpackPlugin = compiler . options . plugins
82+ const htmlWebpackPlugin = compiler . options . plugins
8783 // instanceof can act wonky since we don't actually keep our own dependency on html-webpack-plugin
8884 // should we?
8985 . find ( plugin => plugin . constructor . name === 'HtmlWebpackPlugin' ) as any
@@ -94,18 +90,18 @@ export class BabelMultiTargetHtmlUpdater implements Plugin {
9490
9591 // not sure if this is a problem since webpack will wait for dependencies to load, but sorting
9692 // by auto/dependency will result in a cyclic dependency error for lazy-loaded routes
97- htmlWebpackPlugin . options . chunksSortMode = 'none' as any
93+ htmlWebpackPlugin . userOptions . chunksSortMode = 'none' as any
9894
99- if ( ( htmlWebpackPlugin . options . chunks as any ) !== 'all' &&
100- htmlWebpackPlugin . options . chunks &&
101- htmlWebpackPlugin . options . chunks . length
95+ if ( ( htmlWebpackPlugin . userOptions . chunks as any ) !== 'all' &&
96+ htmlWebpackPlugin . userOptions . chunks &&
97+ htmlWebpackPlugin . userOptions . chunks . length
10298 ) {
103- htmlWebpackPlugin . options . chunks = this . mapChunkNames ( htmlWebpackPlugin . options . chunks as string [ ] )
99+ htmlWebpackPlugin . userOptions . chunks = this . mapChunkNames ( htmlWebpackPlugin . userOptions . chunks as string [ ] )
104100 }
105101
106- if ( htmlWebpackPlugin . options . excludeChunks &&
107- htmlWebpackPlugin . options . excludeChunks . length ) {
108- htmlWebpackPlugin . options . excludeChunks = this . mapChunkNames ( htmlWebpackPlugin . options . excludeChunks )
102+ if ( htmlWebpackPlugin . userOptions . excludeChunks &&
103+ htmlWebpackPlugin . userOptions . excludeChunks . length ) {
104+ htmlWebpackPlugin . userOptions . excludeChunks = this . mapChunkNames ( htmlWebpackPlugin . userOptions . excludeChunks )
109105 }
110106
111107 compiler . hooks . compilation . tap ( PLUGIN_NAME , ( compilation : Compilation ) => {
@@ -114,10 +110,8 @@ export class BabelMultiTargetHtmlUpdater implements Plugin {
114110 return
115111 }
116112
117- const hook = getAlterAssetTags ( compilation )
118-
119- hook . tapPromise ( `${ PLUGIN_NAME } update asset tags` ,
120- async ( htmlPluginData : AlterAssetTagsData ) => {
113+ getHooks ( compilation ) . alterAssetTagGroups . tapPromise ( `${ PLUGIN_NAME } update asset tags` ,
114+ async ( htmlPluginData ) => {
121115 const chunkMap : TargetedChunkMap = compilation . chunkGroups . reduce ( ( result : TargetedChunkMap , chunkGroup : ChunkGroup ) => {
122116 chunkGroup . chunks . forEach ( ( chunk : Chunk ) => {
123117 chunk . files . forEach ( ( file : string ) => {
@@ -126,8 +120,8 @@ export class BabelMultiTargetHtmlUpdater implements Plugin {
126120 } )
127121 return result
128122 } , new TargetedChunkMap ( compiler . options . output . publicPath ) )
129- this . updateScriptTags ( chunkMap , getHeadTags ( htmlPluginData ) )
130- this . updateScriptTags ( chunkMap , getBodyTags ( htmlPluginData ) )
123+ this . updateScriptTags ( chunkMap , htmlPluginData . headTags )
124+ this . updateScriptTags ( chunkMap , htmlPluginData . bodyTags )
131125 return htmlPluginData
132126 } )
133127
0 commit comments