|
| 1 | +/* globals describe, it, beforeEach*/ |
1 | 2 | "use strict"; |
2 | | -const should = require("should"); |
| 3 | +require("should"); |
3 | 4 | const sinon = require("sinon"); |
4 | 5 | const UglifyJsPlugin = require("../lib/optimize/UglifyJsPlugin"); |
5 | 6 | const PluginEnvironment = require("./helpers/PluginEnvironment"); |
@@ -409,6 +410,120 @@ describe("UglifyJsPlugin", function() { |
409 | 410 | compilation.assets["test3.js"].should.be.instanceof(SourceMapSource); |
410 | 411 | }); |
411 | 412 | }); |
| 413 | + |
| 414 | + describe("with warningsFilter set", function() { |
| 415 | + let compilationEventBindings, compilation; |
| 416 | + |
| 417 | + describe("and the filter returns true", function() { |
| 418 | + beforeEach(function() { |
| 419 | + const pluginEnvironment = new PluginEnvironment(); |
| 420 | + const compilerEnv = pluginEnvironment.getEnvironmentStub(); |
| 421 | + compilerEnv.context = ""; |
| 422 | + |
| 423 | + const plugin = new UglifyJsPlugin({ |
| 424 | + warningsFilter: function() { |
| 425 | + return true; |
| 426 | + }, |
| 427 | + sourceMap: true, |
| 428 | + compress: { |
| 429 | + warnings: true, |
| 430 | + }, |
| 431 | + mangle: false, |
| 432 | + beautify: true, |
| 433 | + comments: false |
| 434 | + }); |
| 435 | + plugin.apply(compilerEnv); |
| 436 | + const eventBindings = pluginEnvironment.getEventBindings(); |
| 437 | + |
| 438 | + const chunkPluginEnvironment = new PluginEnvironment(); |
| 439 | + compilation = chunkPluginEnvironment.getEnvironmentStub(); |
| 440 | + compilation.assets = { |
| 441 | + "test2.js": { |
| 442 | + source: function() { |
| 443 | + return "function foo(x) { if (x) { return bar(); not_called1(); } }"; |
| 444 | + }, |
| 445 | + map: function() { |
| 446 | + return { |
| 447 | + version: 3, |
| 448 | + sources: ["test1.js"], |
| 449 | + names: ["foo", "x", "bar", "not_called1"], |
| 450 | + mappings: "AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC" |
| 451 | + }; |
| 452 | + } |
| 453 | + }, |
| 454 | + }; |
| 455 | + compilation.errors = []; |
| 456 | + compilation.warnings = []; |
| 457 | + |
| 458 | + eventBindings[0].handler(compilation); |
| 459 | + compilationEventBindings = chunkPluginEnvironment.getEventBindings(); |
| 460 | + }); |
| 461 | + |
| 462 | + it("should get all warnings", function() { |
| 463 | + compilationEventBindings[1].handler([{ |
| 464 | + files: ["test2.js"] |
| 465 | + }], function() { |
| 466 | + compilation.warnings.length.should.be.exactly(1); |
| 467 | + compilation.warnings[0].should.be.an.Error; |
| 468 | + compilation.warnings[0].message.should.containEql("Dropping unreachable code"); |
| 469 | + }); |
| 470 | + }); |
| 471 | + }); |
| 472 | + |
| 473 | + describe("and the filter returns false", function() { |
| 474 | + beforeEach(function() { |
| 475 | + const pluginEnvironment = new PluginEnvironment(); |
| 476 | + const compilerEnv = pluginEnvironment.getEnvironmentStub(); |
| 477 | + compilerEnv.context = ""; |
| 478 | + |
| 479 | + const plugin = new UglifyJsPlugin({ |
| 480 | + warningsFilter: function() { |
| 481 | + return false; |
| 482 | + }, |
| 483 | + sourceMap: true, |
| 484 | + compress: { |
| 485 | + warnings: true, |
| 486 | + }, |
| 487 | + mangle: false, |
| 488 | + beautify: true, |
| 489 | + comments: false |
| 490 | + }); |
| 491 | + plugin.apply(compilerEnv); |
| 492 | + const eventBindings = pluginEnvironment.getEventBindings(); |
| 493 | + |
| 494 | + const chunkPluginEnvironment = new PluginEnvironment(); |
| 495 | + compilation = chunkPluginEnvironment.getEnvironmentStub(); |
| 496 | + compilation.assets = { |
| 497 | + "test2.js": { |
| 498 | + source: function() { |
| 499 | + return "function foo(x) { if (x) { return bar(); not_called1(); } }"; |
| 500 | + }, |
| 501 | + map: function() { |
| 502 | + return { |
| 503 | + version: 3, |
| 504 | + sources: ["test1.js"], |
| 505 | + names: ["foo", "x", "bar", "not_called1"], |
| 506 | + mappings: "AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC" |
| 507 | + }; |
| 508 | + } |
| 509 | + }, |
| 510 | + }; |
| 511 | + compilation.errors = []; |
| 512 | + compilation.warnings = []; |
| 513 | + |
| 514 | + eventBindings[0].handler(compilation); |
| 515 | + compilationEventBindings = chunkPluginEnvironment.getEventBindings(); |
| 516 | + }); |
| 517 | + |
| 518 | + it("should get no warnings", function() { |
| 519 | + compilationEventBindings[1].handler([{ |
| 520 | + files: ["test2.js"] |
| 521 | + }], function() { |
| 522 | + compilation.warnings.length.should.be.exactly(0); |
| 523 | + }); |
| 524 | + }); |
| 525 | + }); |
| 526 | + }); |
412 | 527 | }); |
413 | 528 | }); |
414 | 529 | }); |
|
0 commit comments