@@ -5,21 +5,19 @@ var path = require('path');
5
5
var slash = require ( 'slash' ) ;
6
6
var sourceMapUrl = require ( 'source-map-url' ) ;
7
7
8
- function HtmlWebpackInlineSourcePlugin ( options ) {
8
+ function HtmlWebpackInlineSourcePlugin ( htmlWebpackPlugin , options ) {
9
+ this . htmlWebpackPlugin = htmlWebpackPlugin ;
9
10
assert . equal ( options , undefined , 'The HtmlWebpackInlineSourcePlugin does not accept any options' ) ;
10
11
}
11
12
12
13
HtmlWebpackInlineSourcePlugin . prototype . apply = function ( compiler ) {
13
14
var self = this ;
14
15
15
16
// Hook into the html-webpack-plugin processing
16
-
17
- ( compiler . hooks
18
- ? compiler . hooks . compilation . tap . bind ( compiler . hooks . compilation , 'html-webpack-inline-source-plugin' )
19
- : compiler . plugin . bind ( compiler , 'compilation' ) ) ( function ( compilation ) {
20
- ( compilation . hooks
21
- ? compilation . hooks . htmlWebpackPluginAlterAssetTags . tapAsync . bind ( compilation . hooks . htmlWebpackPluginAlterAssetTags , 'html-webpack-inline-source-plugin' )
22
- : compilation . plugin . bind ( compilation , 'html-webpack-plugin-alter-asset-tags' ) ) ( function ( htmlPluginData , callback ) {
17
+ compiler . hooks . compilation . tap ( 'html-webpack-inline-source-plugin' , compilation => {
18
+ self . htmlWebpackPlugin
19
+ . getHooks ( compilation )
20
+ . alterAssetTagGroups . tapAsync ( 'html-webpack-inline-source-plugin' , ( htmlPluginData , callback ) => {
23
21
if ( ! htmlPluginData . plugin . options . inlineSource ) {
24
22
return callback ( null , htmlPluginData ) ;
25
23
}
@@ -29,28 +27,27 @@ HtmlWebpackInlineSourcePlugin.prototype.apply = function (compiler) {
29
27
var result = self . processTags ( compilation , regexStr , htmlPluginData ) ;
30
28
31
29
callback ( null , result ) ;
32
- } ) ;
33
- } ) ;
30
+ } )
31
+ } )
34
32
} ;
35
33
36
34
HtmlWebpackInlineSourcePlugin . prototype . processTags = function ( compilation , regexStr , pluginData ) {
37
35
var self = this ;
38
36
39
- var body = [ ] ;
40
- var head = [ ] ;
37
+ var bodyTags = [ ] ;
38
+ var headTags = [ ] ;
41
39
42
40
var regex = new RegExp ( regexStr ) ;
43
- var filename = pluginData . plugin . options . filename ;
44
41
45
- pluginData . head . forEach ( function ( tag ) {
46
- head . push ( self . processTag ( compilation , regex , tag , filename ) ) ;
42
+ pluginData . headTags . forEach ( function ( tag ) {
43
+ headTags . push ( self . processTag ( compilation , regex , tag ) ) ;
47
44
} ) ;
48
45
49
- pluginData . body . forEach ( function ( tag ) {
50
- body . push ( self . processTag ( compilation , regex , tag , filename ) ) ;
46
+ pluginData . bodyTags . forEach ( function ( tag ) {
47
+ bodyTags . push ( self . processTag ( compilation , regex , tag ) ) ;
51
48
} ) ;
52
49
53
- return { head : head , body : body , plugin : pluginData . plugin , chunks : pluginData . chunks , outputName : pluginData . outputName } ;
50
+ return { headTags : headTags , bodyTags : bodyTags , plugin : pluginData . plugin , outputName : pluginData . outputName } ;
54
51
} ;
55
52
56
53
HtmlWebpackInlineSourcePlugin . prototype . resolveSourceMaps = function ( compilation , assetName , asset ) {
@@ -84,11 +81,11 @@ HtmlWebpackInlineSourcePlugin.prototype.resolveSourceMaps = function (compilatio
84
81
} ) ;
85
82
} ;
86
83
87
- HtmlWebpackInlineSourcePlugin . prototype . processTag = function ( compilation , regex , tag , filename ) {
84
+ HtmlWebpackInlineSourcePlugin . prototype . processTag = function ( compilation , regex , tag ) {
88
85
var assetUrl ;
89
86
90
87
// inline js
91
- if ( tag . tagName === 'script' && tag . attributes && regex . test ( tag . attributes . src ) ) {
88
+ if ( tag . tagName === 'script' && regex . test ( tag . attributes . src ) ) {
92
89
assetUrl = tag . attributes . src ;
93
90
tag = {
94
91
tagName : 'script' ,
@@ -113,28 +110,13 @@ HtmlWebpackInlineSourcePlugin.prototype.processTag = function (compilation, rege
113
110
if ( assetUrl ) {
114
111
// Strip public URL prefix from asset URL to get Webpack asset name
115
112
var publicUrlPrefix = compilation . outputOptions . publicPath || '' ;
116
- // if filename is in subfolder, assetUrl should be prepended folder path
117
- if ( path . basename ( filename ) !== filename ) {
118
- assetUrl = path . dirname ( filename ) + '/' + assetUrl ;
119
- }
120
113
var assetName = path . posix . relative ( publicUrlPrefix , assetUrl ) ;
121
- var asset = getAssetByName ( compilation . assets , assetName ) ;
114
+ var asset = compilation . assets [ assetName ] ;
122
115
var updatedSource = this . resolveSourceMaps ( compilation , assetName , asset ) ;
123
116
tag . innerHTML = ( tag . tagName === 'script' ) ? updatedSource . replace ( / ( < ) ( \/ s c r i p t > ) / g, '\\x3C$2' ) : updatedSource ;
124
117
}
125
118
126
119
return tag ;
127
120
} ;
128
121
129
- function getAssetByName ( assests , assetName ) {
130
- for ( var key in assests ) {
131
- if ( assests . hasOwnProperty ( key ) ) {
132
- var processedKey = path . posix . relative ( '' , key ) ;
133
- if ( processedKey === assetName ) {
134
- return assests [ key ] ;
135
- }
136
- }
137
- }
138
- }
139
-
140
122
module . exports = HtmlWebpackInlineSourcePlugin ;
0 commit comments