@@ -29,6 +29,45 @@ function wrapComment(str) {
2929 *[N] the N-th match obtained from matching the current file name against options.regExp
3030 */
3131
32+ const REGEXP_HASH = / \[ h a s h : ? ( \d + ) ? \] / gi,
33+ REGEXP_CHUNKHASH = / \[ c h u n k h a s h (?: : ( \d + ) ) ? \] / gi,
34+ REGEXP_NAME = / \[ n a m e \] / gi,
35+ REGEXP_ID = / \[ i d \] / gi,
36+ REGEXP_FILE = / \[ f i l e \] / gi,
37+ REGEXP_QUERY = / \[ q u e r y \] / gi;
38+
39+ // withHashLength, getReplacer from lib/TemplatedPathPlugin.js
40+ const withHashLength = ( replacer , handlerFn ) => {
41+ return function ( _ , hashLength ) {
42+ const length = hashLength && parseInt ( hashLength , 10 ) ;
43+ if ( length && handlerFn ) {
44+ return handlerFn ( length ) ;
45+ }
46+
47+ const hash = replacer . apply ( this , arguments ) ;
48+ return length ? hash . slice ( 0 , length ) : hash ;
49+ } ;
50+ } ;
51+
52+ const getReplacer = ( value , allowEmpty ) => {
53+ return function ( match ) {
54+ // last argument in replacer is the entire input string
55+ const input = arguments [ arguments . length - 1 ] ;
56+ if ( value === null || value === undefined ) {
57+ if ( ! allowEmpty ) throw new Error ( `Path variable ${ match } not implemented in this context: ${ input } ` ) ;
58+ return "" ;
59+ } else {
60+ return `${ value } ` ;
61+ }
62+ } ;
63+ } ;
64+
65+
66+ const interpolate = ( banner , file , hash , chunkHash ) => {
67+ return banner
68+ . replace ( REGEXP_HASH , withHashLength ( getReplacer ( hash ) ) )
69+ }
70+
3271class BannerPlugin {
3372 constructor ( options ) {
3473 if ( arguments . length > 1 )
@@ -42,21 +81,19 @@ class BannerPlugin {
4281 }
4382
4483 apply ( compiler ) {
45- const options = this . options ;
46- const banner = this . banner ;
84+ const options = this . options ,
85+ banner = this . banner ;
4786
4887 compiler . plugin ( "compilation" , ( compilation ) => {
49- console . log ( 'compilation: ' , JSON . stringify ( compilation ) ) ;
5088 compilation . plugin ( "optimize-chunk-assets" , ( chunks , callback ) => {
5189 chunks . forEach ( ( chunk ) => {
5290 if ( options . entryOnly && ! chunk . isInitial ( ) ) return ;
53-
5491 chunk . files
5592 . filter ( ModuleFilenameHelpers . matchObject . bind ( undefined , options ) )
5693 . forEach ( ( file ) => {
57- return compilation . assets [ file ] = new ConcatSource (
58- banner , "\n" , compilation . assets [ file ]
59- )
94+ const chunkHash = chunk . renderedHash || chunk . hash
95+ const comment = interpolate ( banner , file , compilation . hash , chunkHash )
96+ return compilation . assets [ file ] = new ConcatSource ( comment , "\n" , compilation . assets [ file ] )
6097 } ) ;
6198 } ) ;
6299 callback ( ) ;
0 commit comments