import HtmlWebpackPlugin , { Options as HTMLOptions } from 'html-webpack-plugin' ; import { BuildEntry } from '@reskript/settings' ; import { AppEntry , BuildContext } from '../interface' ; const getHTMLConfig = ( filename , entry , env ) : HTMLOptions => { const { mode , projectSettings : { build : { appTitle , favicon , appContainerId , } , } , buildVersion , buildTime , buildTarget , } = env ; const { name , config , template } = entry ; const buildIdentifier = `${ buildVersion }/${ buildTarget }@${ buildTime }` ; const baseConfig = { favicon , filename , appContainerId , mode , buildVersion , buildTime , buildTarget , buildIdentifier , title : appTitle , meta : { viewport : 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' , 'app-version' : buildIdentifier , } , chunks : [ name ] , minify : { collapseBooleanAttributes : true , collapseInlineTagWhitespace : true , collapseWhitespace : true , conservativeCollapse : true , minifyCSS : true , minifyJS : true , removeAttributeQuotes : false , removeEmptyAttributes : true , removeOptionalTags : true , removeRedundantAttributes : true , removeScriptTypeAttributes : true , removeStyleLinkTypeAttributes : true , } , ... config . html , } ; return template ? Object . assign ( baseConfig , { template } ) : baseConfig ; } ; const createHTMLPluginWith = ( buildEntry ) => { const { usage } = buildEntry ; return ( suffix ) => ( entry ) : HtmlWebpackPlugin => { const filename = ( usage === 'build' ? '../' : '' ) + `${ entry . name }${ suffix }.html` ; const pluginConfig = getHTMLConfig ( filename , entry , buildEntry ) ; return new HtmlWebpackPlugin ( pluginConfig ) ; } ; } ; export const createHTMLPluginInstances = ( buildContext ) : HtmlWebpackPlugin [ ] => { const { isDefaultTarget , buildTarget , entries } = buildContext ; const createInstanceWithSuffix = createHTMLPluginWith ( buildContext ) ; const pluginsWithinTarget = entries . map ( createInstanceWithSuffix ( '-' + buildTarget ) ) ; const pluginsOfDefault = isDefaultTarget ? entries . map ( createInstanceWithSuffix ( '' ) ) : [ ] ; return [ ... pluginsWithinTarget , ... pluginsOfDefault ] ; } ;