@@ -224,7 +224,16 @@ describe("UglifyJsPlugin", function() {
224224 } ,
225225 mangle : false ,
226226 beautify : true ,
227- comments : false
227+ comments : false ,
228+ extractComments : {
229+ condition : 'should be extracted' ,
230+ filename : function ( file ) {
231+ return file . replace ( / ( \. \w + ) $ / , '.license$1' ) ;
232+ } ,
233+ banner : function ( licenseFile ) {
234+ return 'License information can be found in ' + licenseFile ;
235+ }
236+ }
228237 } ) ;
229238 plugin . apply ( compilerEnv ) ;
230239 eventBindings = pluginEnvironment . getEventBindings ( ) ;
@@ -305,6 +314,19 @@ describe("UglifyJsPlugin", function() {
305314 } ;
306315 } ,
307316 } ,
317+ "test4.js" : {
318+ source : function ( ) {
319+ return "/*! this comment should be extracted */ function foo(longVariableName) { /* this will not be extracted */ longVariableName = 1; } // another comment that should be extracted to a separate file\n function foo2(bar) { return bar; }" ;
320+ } ,
321+ map : function ( ) {
322+ return {
323+ version : 3 ,
324+ sources : [ "test.js" ] ,
325+ names : [ "foo" , "longVariableName" ] ,
326+ mappings : "AAAA,QAASA,KAAIC,kBACTA,iBAAmB"
327+ } ;
328+ }
329+ } ,
308330 } ;
309331 compilation . errors = [ ] ;
310332 compilation . warnings = [ ] ;
@@ -524,6 +546,120 @@ describe("UglifyJsPlugin", function() {
524546 } ) ;
525547 } ) ;
526548 } ) ;
549+
550+ it ( "extracts license information to separate file" , function ( ) {
551+ compilationEventBinding . handler ( [ {
552+ files : [ "test4.js" ]
553+ } ] , function ( ) {
554+ compilation . errors . length . should . be . exactly ( 0 ) ;
555+ compilation . assets [ "test4.license.js" ] . _value . should . containEql ( "/*! this comment should be extracted */" ) ;
556+ compilation . assets [ "test4.license.js" ] . _value . should . containEql ( "// another comment that should be extracted to a separate file" ) ;
557+ compilation . assets [ "test4.license.js" ] . _value . should . not . containEql ( "/* this will not be extracted */" ) ;
558+ } ) ;
559+ } ) ;
560+ } ) ;
561+ } ) ;
562+ } ) ;
563+ } ) ;
564+
565+ describe ( "when applied with extract option set to a single file" , function ( ) {
566+ let eventBindings ;
567+ let eventBinding ;
568+
569+ beforeEach ( function ( ) {
570+ const pluginEnvironment = new PluginEnvironment ( ) ;
571+ const compilerEnv = pluginEnvironment . getEnvironmentStub ( ) ;
572+ compilerEnv . context = "" ;
573+
574+ const plugin = new UglifyJsPlugin ( {
575+ comments : "all" ,
576+ extractComments : {
577+ condition : / .* / ,
578+ filename : "extracted-comments.js"
579+ }
580+ } ) ;
581+ plugin . apply ( compilerEnv ) ;
582+ eventBindings = pluginEnvironment . getEventBindings ( ) ;
583+ } ) ;
584+
585+ it ( "binds one event handler" , function ( ) {
586+ eventBindings . length . should . be . exactly ( 1 ) ;
587+ } ) ;
588+
589+ describe ( "compilation handler" , function ( ) {
590+ beforeEach ( function ( ) {
591+ eventBinding = eventBindings [ 0 ] ;
592+ } ) ;
593+
594+ it ( "binds to compilation event" , function ( ) {
595+ eventBinding . name . should . be . exactly ( "compilation" ) ;
596+ } ) ;
597+
598+ describe ( "when called" , function ( ) {
599+ let chunkPluginEnvironment ;
600+ let compilationEventBindings ;
601+ let compilationEventBinding ;
602+ let compilation ;
603+
604+ beforeEach ( function ( ) {
605+ chunkPluginEnvironment = new PluginEnvironment ( ) ;
606+ compilation = chunkPluginEnvironment . getEnvironmentStub ( ) ;
607+ compilation . assets = {
608+ "test.js" : {
609+ source : function ( ) {
610+ return "/* This is a comment from test.js */ function foo(bar) { return bar; }" ;
611+ }
612+ } ,
613+ "test2.js" : {
614+ source : function ( ) {
615+ return "// This is a comment from test2.js\nfunction foo2(bar) { return bar; }" ;
616+ }
617+ } ,
618+ "test3.js" : {
619+ source : function ( ) {
620+ return "/* This is a comment from test3.js */ function foo3(bar) { return bar; }\n// This is another comment from test3.js\nfunction foobar3(baz) { return baz; }" ;
621+ }
622+ } ,
623+ } ;
624+ compilation . errors = [ ] ;
625+ compilation . warnings = [ ] ;
626+
627+ eventBinding . handler ( compilation ) ;
628+ compilationEventBindings = chunkPluginEnvironment . getEventBindings ( ) ;
629+ } ) ;
630+
631+ it ( "binds one event handler" , function ( ) {
632+ compilationEventBindings . length . should . be . exactly ( 1 ) ;
633+ } ) ;
634+
635+ describe ( "optimize-chunk-assets handler" , function ( ) {
636+ beforeEach ( function ( ) {
637+ compilationEventBinding = compilationEventBindings [ 0 ] ;
638+ } ) ;
639+
640+ it ( "preserves comments" , function ( ) {
641+ compilationEventBinding . handler ( [ {
642+ files : [ "test.js" , "test2.js" , "test3.js" ]
643+ } ] , function ( ) {
644+ compilation . assets [ "test.js" ] . source ( ) . should . containEql ( "/*" ) ;
645+ compilation . assets [ "test2.js" ] . source ( ) . should . containEql ( "//" ) ;
646+ compilation . assets [ "test3.js" ] . source ( ) . should . containEql ( "/*" ) ;
647+ compilation . assets [ "test3.js" ] . source ( ) . should . containEql ( "//" ) ;
648+ } ) ;
649+ } ) ;
650+
651+ it ( "extracts comments to specified file" , function ( ) {
652+ compilationEventBinding . handler ( [ {
653+ files : [ "test.js" , "test2.js" , "test3.js" ]
654+ } ] , function ( ) {
655+ compilation . errors . length . should . be . exactly ( 0 ) ;
656+ compilation . assets [ "extracted-comments.js" ] . source ( ) . should . containEql ( "/* This is a comment from test.js */" ) ;
657+ compilation . assets [ "extracted-comments.js" ] . source ( ) . should . containEql ( "// This is a comment from test2.js" ) ;
658+ compilation . assets [ "extracted-comments.js" ] . source ( ) . should . containEql ( "/* This is a comment from test3.js */" ) ;
659+ compilation . assets [ "extracted-comments.js" ] . source ( ) . should . containEql ( "// This is another comment from test3.js" ) ;
660+ compilation . assets [ "extracted-comments.js" ] . source ( ) . should . not . containEql ( "function" ) ;
661+ } ) ;
662+ } ) ;
527663 } ) ;
528664 } ) ;
529665 } ) ;
0 commit comments