@@ -5,7 +5,7 @@ import path from "path";
5
5
import { SourceMapConsumer } from "source-map" ;
6
6
import CopyWebpackPlugin from "copy-webpack-plugin" ;
7
7
import RequestShortener from "webpack/lib/RequestShortener" ;
8
- import { javascript } from "webpack" ;
8
+ import { javascript , SourceMapDevToolPlugin } from "webpack" ;
9
9
10
10
import TerserPlugin from "../src/index" ;
11
11
@@ -23,6 +23,22 @@ import {
23
23
24
24
jest . setTimeout ( 10000 ) ;
25
25
26
+ expect . addSnapshotSerializer ( {
27
+ test : ( value ) => {
28
+ // For string that are valid JSON
29
+ if ( typeof value !== "string" ) {
30
+ return false ;
31
+ }
32
+
33
+ try {
34
+ return typeof JSON . parse ( value ) === "object" ;
35
+ } catch ( e ) {
36
+ return false ;
37
+ }
38
+ } ,
39
+ print : ( value ) => JSON . stringify ( JSON . parse ( value ) , null , 2 ) ,
40
+ } ) ;
41
+
26
42
describe ( "TerserPlugin" , ( ) => {
27
43
const rawSourceMap = {
28
44
version : 3 ,
@@ -1320,4 +1336,276 @@ describe("TerserPlugin", () => {
1320
1336
resolve ( ) ;
1321
1337
} ) ;
1322
1338
} ) ;
1339
+
1340
+ it ( 'should work with the "devtool" option and the "false" value' , async ( ) => {
1341
+ const compiler = getCompiler ( {
1342
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1343
+ devtool : false ,
1344
+ } ) ;
1345
+
1346
+ new TerserPlugin ( ) . apply ( compiler ) ;
1347
+
1348
+ const stats = await compile ( compiler ) ;
1349
+
1350
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1351
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1352
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1353
+ } ) ;
1354
+
1355
+ it ( 'should work with "devtool" option and the "source-map" value' , async ( ) => {
1356
+ const compiler = getCompiler ( {
1357
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1358
+ devtool : "source-map" ,
1359
+ } ) ;
1360
+
1361
+ new TerserPlugin ( ) . apply ( compiler ) ;
1362
+
1363
+ const stats = await compile ( compiler ) ;
1364
+
1365
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1366
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1367
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1368
+ } ) ;
1369
+
1370
+ it ( 'should work with "devtool" option and the "inline-source-map" value' , async ( ) => {
1371
+ const compiler = getCompiler ( {
1372
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1373
+ devtool : "inline-source-map" ,
1374
+ } ) ;
1375
+
1376
+ new TerserPlugin ( ) . apply ( compiler ) ;
1377
+
1378
+ const stats = await compile ( compiler ) ;
1379
+
1380
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1381
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1382
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1383
+ } ) ;
1384
+
1385
+ it ( 'should work with "devtool" option and the "hidden-source-map" value' , async ( ) => {
1386
+ const compiler = getCompiler ( {
1387
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1388
+ devtool : "hidden-source-map" ,
1389
+ } ) ;
1390
+
1391
+ new TerserPlugin ( ) . apply ( compiler ) ;
1392
+
1393
+ const stats = await compile ( compiler ) ;
1394
+
1395
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1396
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1397
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1398
+ } ) ;
1399
+
1400
+ it ( 'should work with "devtool" option and the "nosources-source-map" value' , async ( ) => {
1401
+ const compiler = getCompiler ( {
1402
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1403
+ devtool : "nosources-source-map" ,
1404
+ } ) ;
1405
+
1406
+ new TerserPlugin ( ) . apply ( compiler ) ;
1407
+
1408
+ const stats = await compile ( compiler ) ;
1409
+
1410
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1411
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1412
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1413
+ } ) ;
1414
+
1415
+ it ( 'should work with "devtool" option and the "eval" value' , async ( ) => {
1416
+ const compiler = getCompiler ( {
1417
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1418
+ devtool : "eval" ,
1419
+ } ) ;
1420
+
1421
+ new TerserPlugin ( ) . apply ( compiler ) ;
1422
+
1423
+ const stats = await compile ( compiler ) ;
1424
+
1425
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1426
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1427
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1428
+ } ) ;
1429
+
1430
+ it ( 'should work with "devtool" option and the "cheap-source-map" value' , async ( ) => {
1431
+ const compiler = getCompiler ( {
1432
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1433
+ devtool : "cheap-source-map" ,
1434
+ } ) ;
1435
+
1436
+ new TerserPlugin ( ) . apply ( compiler ) ;
1437
+
1438
+ const stats = await compile ( compiler ) ;
1439
+
1440
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1441
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1442
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1443
+ } ) ;
1444
+
1445
+ it ( 'should work with the "SourceMapDevToolPlugin" plugin (like "source-map")' , async ( ) => {
1446
+ const compiler = getCompiler ( {
1447
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1448
+ devtool : false ,
1449
+ plugins : [
1450
+ new SourceMapDevToolPlugin ( {
1451
+ filename : "[file].map[query]" ,
1452
+ module : true ,
1453
+ columns : true ,
1454
+ } ) ,
1455
+ ] ,
1456
+ } ) ;
1457
+
1458
+ new TerserPlugin ( ) . apply ( compiler ) ;
1459
+
1460
+ const stats = await compile ( compiler ) ;
1461
+
1462
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1463
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1464
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1465
+ } ) ;
1466
+
1467
+ it ( 'should work with the "SourceMapDevToolPlugin" plugin (like "cheap-source-map")' , async ( ) => {
1468
+ const compiler = getCompiler ( {
1469
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1470
+ devtool : false ,
1471
+ plugins : [
1472
+ new SourceMapDevToolPlugin ( {
1473
+ filename : "[file].map[query]" ,
1474
+ module : false ,
1475
+ columns : false ,
1476
+ } ) ,
1477
+ ] ,
1478
+ } ) ;
1479
+
1480
+ new TerserPlugin ( ) . apply ( compiler ) ;
1481
+
1482
+ const stats = await compile ( compiler ) ;
1483
+
1484
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1485
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1486
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1487
+ } ) ;
1488
+
1489
+ it ( "should work with multi compiler mode with source maps" , async ( ) => {
1490
+ const multiCompiler = getCompiler ( [
1491
+ {
1492
+ mode : "production" ,
1493
+ devtool : "eval" ,
1494
+ bail : true ,
1495
+ cache : { type : "memory" } ,
1496
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1497
+ output : {
1498
+ path : path . resolve ( __dirname , "./dist" ) ,
1499
+ filename : "[name]-1.js" ,
1500
+ chunkFilename : "[id]-1.[name].js" ,
1501
+ } ,
1502
+ optimization : {
1503
+ minimize : false ,
1504
+ } ,
1505
+ plugins : [ new TerserPlugin ( ) ] ,
1506
+ } ,
1507
+ {
1508
+ mode : "production" ,
1509
+ devtool : "source-map" ,
1510
+ bail : true ,
1511
+ cache : { type : "memory" } ,
1512
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1513
+ output : {
1514
+ path : path . resolve ( __dirname , "./dist" ) ,
1515
+ filename : "[name]-2.js" ,
1516
+ chunkFilename : "[id]-2.[name].js" ,
1517
+ } ,
1518
+ optimization : {
1519
+ minimize : false ,
1520
+ } ,
1521
+ plugins : [ new TerserPlugin ( ) ] ,
1522
+ } ,
1523
+ {
1524
+ mode : "production" ,
1525
+ bail : true ,
1526
+ cache : { type : "memory" } ,
1527
+ devtool : false ,
1528
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1529
+ output : {
1530
+ path : path . resolve ( __dirname , "./dist" ) ,
1531
+ filename : "[name]-3.js" ,
1532
+ chunkFilename : "[id]-3.[name].js" ,
1533
+ } ,
1534
+ optimization : {
1535
+ minimize : false ,
1536
+ } ,
1537
+ plugins : [
1538
+ new SourceMapDevToolPlugin ( {
1539
+ filename : "[file].map[query]" ,
1540
+ module : false ,
1541
+ columns : false ,
1542
+ } ) ,
1543
+ new TerserPlugin ( ) ,
1544
+ ] ,
1545
+ } ,
1546
+ {
1547
+ mode : "production" ,
1548
+ bail : true ,
1549
+ cache : { type : "memory" } ,
1550
+ devtool : false ,
1551
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1552
+ output : {
1553
+ path : path . resolve ( __dirname , "./dist" ) ,
1554
+ filename : "[name]-4.js" ,
1555
+ chunkFilename : "[id]-4.[name].js" ,
1556
+ } ,
1557
+ optimization : {
1558
+ minimize : false ,
1559
+ } ,
1560
+ plugins : [
1561
+ new SourceMapDevToolPlugin ( {
1562
+ filename : "[file].map[query]" ,
1563
+ module : true ,
1564
+ columns : true ,
1565
+ } ) ,
1566
+ new TerserPlugin ( ) ,
1567
+ ] ,
1568
+ } ,
1569
+ ] ) ;
1570
+
1571
+ const multiStats = await compile ( multiCompiler ) ;
1572
+
1573
+ multiStats . stats . forEach ( ( stats , index ) => {
1574
+ expect (
1575
+ readsAssets ( multiCompiler . compilers [ index ] , stats )
1576
+ ) . toMatchSnapshot ( "assets" ) ;
1577
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1578
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1579
+ } ) ;
1580
+ } ) ;
1581
+
1582
+ it ( 'should work with "devtool" option and the "source-map" value (the "parallel" option is "false")' , async ( ) => {
1583
+ const compiler = getCompiler ( {
1584
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1585
+ devtool : "source-map" ,
1586
+ } ) ;
1587
+
1588
+ new TerserPlugin ( { parallel : false } ) . apply ( compiler ) ;
1589
+
1590
+ const stats = await compile ( compiler ) ;
1591
+
1592
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1593
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1594
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1595
+ } ) ;
1596
+
1597
+ it ( 'should work with "devtool" option and the "source-map" value (the "parallel" option is "true")' , async ( ) => {
1598
+ const compiler = getCompiler ( {
1599
+ entry : path . resolve ( __dirname , "./fixtures/entry.js" ) ,
1600
+ devtool : "source-map" ,
1601
+ } ) ;
1602
+
1603
+ new TerserPlugin ( { parallel : true } ) . apply ( compiler ) ;
1604
+
1605
+ const stats = await compile ( compiler ) ;
1606
+
1607
+ expect ( readsAssets ( compiler , stats ) ) . toMatchSnapshot ( "assets" ) ;
1608
+ expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
1609
+ expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
1610
+ } ) ;
1323
1611
} ) ;
0 commit comments