11import path from 'path'
2+ import Promise from 'bluebird'
23
34// Copied from html-webpack-plugin
45function resolvePublicPath ( compilation , filename ) {
@@ -12,31 +13,28 @@ function resolvePublicPath (compilation, filename) {
1213 return publicPath
1314}
1415
16+ function addFileToAssets ( htmlPluginData , compilation , { filename, typeOfAsset = 'js' , includeSourcemap = true } = { } ) {
17+ if ( ! filename ) return compilation . errors . push ( new Error ( 'No filename defined' ) )
18+
19+ return htmlPluginData . plugin . addFileToAssets ( filename , compilation )
20+ . then ( ( filename ) => htmlPluginData . assets [ typeOfAsset ] . unshift ( `${ resolvePublicPath ( compilation , filename ) } ${ filename } ` ) )
21+ . then ( ( ) => {
22+ if ( includeSourcemap ) {
23+ return htmlPluginData . plugin . addFileToAssets ( `${ filename } .map` , compilation )
24+ }
25+ return null
26+ } )
27+ }
28+
1529export default class AddAssetHtmlPlugin {
16- constructor ( {
17- filename,
18- includeSourcemap = false ,
19- typeOfAsset = 'js'
20- } = { } ) {
21- this . filename = filename
22- this . includeSourcemap = includeSourcemap
23- this . typeOfAsset = typeOfAsset
30+ constructor ( assets = [ ] ) {
31+ this . assets = Array . isArray ( assets ) ? assets . slice ( ) . reverse ( ) : [ assets ]
2432 }
2533
2634 apply ( compiler ) {
2735 compiler . plugin ( 'compilation' , ( compilation ) => {
28- if ( ! this . filename ) return compilation . errors . push ( new Error ( 'No filename defined' ) )
29-
30- const publicPath = resolvePublicPath ( compilation , this . filename )
31-
3236 compilation . plugin ( 'html-webpack-plugin-before-html-generation' , ( htmlPluginData , callback ) => {
33- htmlPluginData . plugin . addFileToAssets ( this . filename , compilation )
34- . then ( ( filename ) => htmlPluginData . assets [ this . typeOfAsset ] . unshift ( `${ publicPath } ${ filename } ` ) )
35- . then ( ( ) => {
36- if ( this . includeSourcemap ) {
37- return htmlPluginData . plugin . addFileToAssets ( `${ this . filename } .map` , compilation )
38- }
39- } )
37+ Promise . mapSeries ( this . assets , asset => addFileToAssets ( htmlPluginData , compilation , asset ) )
4038 . then ( ( ) => callback ( null , htmlPluginData ) )
4139 } )
4240 } )
0 commit comments