1
1
import path from 'path'
2
+ import Promise from 'bluebird'
2
3
3
4
// Copied from html-webpack-plugin
4
5
function resolvePublicPath ( compilation , filename ) {
@@ -12,31 +13,28 @@ function resolvePublicPath (compilation, filename) {
12
13
return publicPath
13
14
}
14
15
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
+
15
29
export 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 ]
24
32
}
25
33
26
34
apply ( compiler ) {
27
35
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
-
32
36
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 ) )
40
38
. then ( ( ) => callback ( null , htmlPluginData ) )
41
39
} )
42
40
} )
0 commit comments