File tree 4 files changed +49
-16
lines changed
test/configCases/plugins/banner-plugin
4 files changed +49
-16
lines changed Original file line number Diff line number Diff line change @@ -22,21 +22,33 @@ const wrapComment = str => {
22
22
23
23
class BannerPlugin {
24
24
constructor ( options ) {
25
- if ( arguments . length > 1 )
25
+ if ( arguments . length > 1 ) {
26
26
throw new Error (
27
27
"BannerPlugin only takes one argument (pass an options object)"
28
28
) ;
29
+ }
29
30
30
31
validateOptions ( schema , options , "Banner Plugin" ) ;
31
32
32
- if ( typeof options === "string" )
33
+ if ( typeof options === "string" || typeof options === "function" ) {
33
34
options = {
34
35
banner : options
35
36
} ;
37
+ }
38
+
36
39
this . options = options || { } ;
37
- this . banner = this . options . raw
38
- ? options . banner
39
- : wrapComment ( options . banner ) ;
40
+
41
+ if ( typeof options . banner === "function" ) {
42
+ const getBanner = this . options . banner ;
43
+ this . banner = this . options . raw
44
+ ? getBanner
45
+ : data => wrapComment ( getBanner ( data ) ) ;
46
+ } else {
47
+ const banner = this . options . raw
48
+ ? this . options . banner
49
+ : wrapComment ( this . options . banner ) ;
50
+ this . banner = ( ) => banner ;
51
+ }
40
52
}
41
53
42
54
apply ( compiler ) {
@@ -78,13 +90,15 @@ class BannerPlugin {
78
90
basename = filename . substr ( lastSlashIndex + 1 ) ;
79
91
}
80
92
81
- const comment = compilation . getPath ( banner , {
93
+ const data = {
82
94
hash,
83
95
chunk,
84
96
filename,
85
97
basename,
86
98
query
87
- } ) ;
99
+ } ;
100
+
101
+ const comment = compilation . getPath ( banner ( data ) , data ) ;
88
102
89
103
compilation . assets [ file ] = new ConcatSource (
90
104
comment ,
Original file line number Diff line number Diff line change 39
39
],
40
40
"properties" : {
41
41
"banner" : {
42
- "description" : " The banner as string, it will be wrapped in a comment" ,
43
- "type" : " string"
42
+ "description" : " Specifies the banner" ,
43
+ "anyOf" : [
44
+ {
45
+ "instanceof" : " Function"
46
+ },
47
+ {
48
+ "type" : " string"
49
+ }
50
+ ]
44
51
},
45
52
"raw" : {
46
53
"description" : " If true, banner will not be wrapped in a comment" ,
76
83
}
77
84
}
78
85
},
86
+ {
87
+ "description" : " The banner as function, it will be wrapped in a comment" ,
88
+ "instanceof" : " Function"
89
+ },
79
90
{
80
91
"description" : " The banner as string, it will be wrapped in a comment" ,
81
92
"minLength" : 1 ,
Original file line number Diff line number Diff line change 1
- it ( "should contain banner in bundle0 chunk" , function ( ) {
2
- var fs = require ( "fs" ) ;
3
- var source = fs . readFileSync ( __filename , "utf-8" ) ;
1
+ const fs = require ( "fs" ) ;
2
+ const path = require ( "path" ) ;
3
+
4
+ it ( "should contain banner in bundle0 chunk" , ( ) => {
5
+ const source = fs . readFileSync ( __filename , "utf-8" ) ;
4
6
expect ( source ) . toMatch ( "A test value" ) ;
7
+ expect ( source ) . toMatch ( "banner is a string" ) ;
8
+ expect ( source ) . toMatch ( "banner is a function" ) ;
9
+ expect ( source ) . toMatch ( "/*!\n * multiline\n * banner\n * 1\n */" ) ;
5
10
} ) ;
6
11
7
- it ( "should not contain banner in vendors chunk" , function ( ) {
8
- var fs = require ( "fs" ) ,
9
- path = require ( "path" ) ;
10
- var source = fs . readFileSync ( path . join ( __dirname , "vendors.js" ) , "utf-8" ) ;
12
+ it ( "should not contain banner in vendors chunk" , ( ) => {
13
+ const source = fs . readFileSync ( path . join ( __dirname , "vendors.js" ) , "utf-8" ) ;
11
14
expect ( source ) . not . toMatch ( "A test value" ) ;
12
15
} ) ;
13
16
Original file line number Diff line number Diff line change @@ -12,9 +12,14 @@ module.exports = {
12
12
filename : "[name].js"
13
13
} ,
14
14
plugins : [
15
+ new webpack . BannerPlugin ( "banner is a string" ) ,
16
+ new webpack . BannerPlugin ( ( ) => "banner is a function" ) ,
15
17
new webpack . BannerPlugin ( {
16
18
banner : "A test value" ,
17
19
exclude : [ "vendors.js" ]
20
+ } ) ,
21
+ new webpack . BannerPlugin ( {
22
+ banner : ( { chunk } ) => `multiline\nbanner\n${ chunk . id } `
18
23
} )
19
24
]
20
25
} ;
You can’t perform that action at this time.
0 commit comments