1- const pluginName = 'WebpackDevServerInjectScriptsPlugin' ;
2-
31const defaultOptions = {
42 ignoredPaths : [ ]
53} ;
64
7- // TODO: Add winston and some logging to aid debugging.
8-
95class WebpackDevServerInjectScriptsPlugin {
10- constructor ( compiler , options ) {
11- this . files = { } ;
6+ constructor ( devServer , options ) {
127 this . options = Object . assign ( { } , defaultOptions , options ) ;
13- this . publicPath = compiler . options . output . publicPath . endsWith ( '/' )
14- ? compiler . options . output . publicPath
15- : compiler . options . output . publicPath + '/' ;
16-
17- if ( this . publicPath . startsWith ( 'auto/' ) ) {
18- console . warn ( `(${ pluginName } ) - auto public path may not be handled correctly` )
19- // TODO: HtmlWebpackPlugin handle this, work it out
20- this . publicPath = this . publicPath . replace ( 'auto/' , '/' ) ;
21- }
22-
23- compiler . hooks . emit . tap ( pluginName , this . onWebpackEmit . bind ( this ) ) ;
24- }
258
26- onWebpackEmit ( compilation ) {
27- // TODO: HtmlWebpackPlugin file list better
28- // run compiler.options.entry through some function to establish output filenames.
9+ const entryNames = Object . keys ( devServer . compiler . options . entry ) ;
10+ const output = devServer . compiler . options . output ;
11+ const filename = devServer . compiler . options . output . filename ;
12+ const publicPath = output . publicPath == 'auto' ? '/' : output . publicPath ;
2913
30- const excludeHot = / \. h o t - u p d a t e \. j s $ / ;
31- const onlyJs = / \. j s $ / ;
32- compilation . chunks . forEach ( chunk => {
33- chunk . files . forEach ( filename => {
34- if ( excludeHot . test ( filename ) ) return ;
35- if ( ! onlyJs . test ( filename ) ) return ;
36- this . files [ `${ this . publicPath } ${ filename } ` ] = 1 ;
37- } ) ;
38- } ) ;
14+ this . scripts = entryNames
15+ . map ( x => `${ publicPath } ${ filename . replace ( / \[ n a m e \] / g, x ) } ` )
16+ . filter ( x => x . endsWith ( '.js' ) )
17+ . map ( x => `<script src="${ x } "></script>` )
18+ . join ( '' )
3919 }
4020
4121 shouldTransform ( request , response ) {
42- if ( response . statusCode != 200 ) {
43- return false ;
44- }
45-
4622 if ( ! / t e x t \/ h t m l / . test ( response . get ( 'Content-Type' ) ) ) {
4723 return false ;
4824 }
@@ -51,18 +27,16 @@ class WebpackDevServerInjectScriptsPlugin {
5127 const url = request . originalUrl ;
5228 for ( const i in this . options . ignoredPaths ) {
5329 const ignored = this . options . ignoredPaths [ i ] ;
54- if ( url . match ( ignored ) ) return false ;
30+ if ( url . match ( ignored ) ) {
31+ return false ;
32+ }
5533 }
5634
5735 return true ;
5836 }
5937
6038 transform ( body ) {
61- const scripts = Object . keys ( this . files )
62- . map ( x => `<script src="${ x } "></script>` )
63- . join ( '' ) ;
64-
65- return body . replace ( '</body>' , `${ scripts } </body>` )
39+ return body . replace ( '</body>' , `${ this . scripts } </body>` )
6640 }
6741}
6842
0 commit comments