1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
|
package api
// This file implements most of the API. This includes the "Build", "Transform",
// "FormatMessages", and "AnalyzeMetafile" functions.
import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/evanw/esbuild/internal/api_helpers"
"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/bundler"
"github.com/evanw/esbuild/internal/cache"
"github.com/evanw/esbuild/internal/compat"
"github.com/evanw/esbuild/internal/config"
"github.com/evanw/esbuild/internal/css_ast"
"github.com/evanw/esbuild/internal/fs"
"github.com/evanw/esbuild/internal/graph"
"github.com/evanw/esbuild/internal/helpers"
"github.com/evanw/esbuild/internal/js_ast"
"github.com/evanw/esbuild/internal/js_parser"
"github.com/evanw/esbuild/internal/linker"
"github.com/evanw/esbuild/internal/logger"
"github.com/evanw/esbuild/internal/resolver"
"github.com/evanw/esbuild/internal/xxhash"
)
func validatePathTemplate(template string) []config.PathTemplate {
if template == "" {
return nil
}
template = "./" + strings.ReplaceAll(template, "\\", "/")
parts := make([]config.PathTemplate, 0, 4)
search := 0
// Split by placeholders
for search < len(template) {
// Jump to the next "["
if found := strings.IndexByte(template[search:], '['); found == -1 {
break
} else {
search += found
}
head, tail := template[:search], template[search:]
placeholder := config.NoPlaceholder
// Check for a placeholder
switch {
case strings.HasPrefix(tail, "[dir]"):
placeholder = config.DirPlaceholder
search += len("[dir]")
case strings.HasPrefix(tail, "[name]"):
placeholder = config.NamePlaceholder
search += len("[name]")
case strings.HasPrefix(tail, "[hash]"):
placeholder = config.HashPlaceholder
search += len("[hash]")
case strings.HasPrefix(tail, "[ext]"):
placeholder = config.ExtPlaceholder
search += len("[ext]")
default:
// Skip past the "[" so we don't find it again
search++
continue
}
// Add a part for everything up to and including this placeholder
parts = append(parts, config.PathTemplate{
Data: head,
Placeholder: placeholder,
})
// Reset the search after this placeholder
template = template[search:]
search = 0
}
// Append any remaining data as a part without a placeholder
if search < len(template) {
parts = append(parts, config.PathTemplate{
Data: template,
Placeholder: config.NoPlaceholder,
})
}
return parts
}
func validatePlatform(value Platform) config.Platform {
switch value {
case PlatformDefault, PlatformBrowser:
return config.PlatformBrowser
case PlatformNode:
return config.PlatformNode
case PlatformNeutral:
return config.PlatformNeutral
default:
panic("Invalid platform")
}
}
func validateFormat(value Format) config.Format {
switch value {
case FormatDefault:
return config.FormatPreserve
case FormatIIFE:
return config.FormatIIFE
case FormatCommonJS:
return config.FormatCommonJS
case FormatESModule:
return config.FormatESModule
default:
panic("Invalid format")
}
}
func validateSourceMap(value SourceMap) config.SourceMap {
switch value {
case SourceMapNone:
return config.SourceMapNone
case SourceMapLinked:
return config.SourceMapLinkedWithComment
case SourceMapInline:
return config.SourceMapInline
case SourceMapExternal:
return config.SourceMapExternalWithoutComment
case SourceMapInlineAndExternal:
return config.SourceMapInlineAndExternal
default:
panic("Invalid source map")
}
}
func validateLegalComments(value LegalComments, bundle bool) config.LegalComments {
switch value {
case LegalCommentsDefault:
if bundle {
return config.LegalCommentsEndOfFile
} else {
return config.LegalCommentsInline
}
case LegalCommentsNone:
return config.LegalCommentsNone
case LegalCommentsInline:
return config.LegalCommentsInline
case LegalCommentsEndOfFile:
return config.LegalCommentsEndOfFile
case LegalCommentsLinked:
return config.LegalCommentsLinkedWithComment
case LegalCommentsExternal:
return config.LegalCommentsExternalWithoutComment
default:
panic("Invalid source map")
}
}
func validateColor(value StderrColor) logger.UseColor {
switch value {
case ColorIfTerminal:
return logger.ColorIfTerminal
case ColorNever:
return logger.ColorNever
case ColorAlways:
return logger.ColorAlways
default:
panic("Invalid color")
}
}
func validateLogLevel(value LogLevel) logger.LogLevel {
switch value {
case LogLevelVerbose:
return logger.LevelVerbose
case LogLevelDebug:
return logger.LevelDebug
case LogLevelInfo:
return logger.LevelInfo
case LogLevelWarning:
return logger.LevelWarning
case LogLevelError:
return logger.LevelError
case LogLevelSilent:
return logger.LevelSilent
default:
panic("Invalid log level")
}
}
func validateASCIIOnly(value Charset) bool {
switch value {
case CharsetDefault, CharsetASCII:
return true
case CharsetUTF8:
return false
default:
panic("Invalid charset")
}
}
func validateExternalPackages(value Packages) bool {
switch value {
case PackagesDefault, PackagesBundle:
return false
case PackagesExternal:
return true
default:
panic("Invalid packages")
}
}
func validateTreeShaking(value TreeShaking, bundle bool, format Format) bool {
switch value {
case TreeShakingDefault:
// If we're in an IIFE then there's no way to concatenate additional code
// to the end of our output so we assume tree shaking is safe. And when
// bundling we assume that tree shaking is safe because if you want to add
// code to the bundle, you should be doing that by including it in the
// bundle instead of concatenating it afterward, so we also assume tree
// shaking is safe then. Otherwise we assume tree shaking is not safe.
return bundle || format == FormatIIFE
case TreeShakingFalse:
return false
case TreeShakingTrue:
return true
default:
panic("Invalid tree shaking")
}
}
func validateLoader(value Loader) config.Loader {
switch value {
case LoaderBase64:
return config.LoaderBase64
case LoaderBinary:
return config.LoaderBinary
case LoaderCopy:
return config.LoaderCopy
case LoaderCSS:
return config.LoaderCSS
case LoaderDataURL:
return config.LoaderDataURL
case LoaderDefault:
return config.LoaderDefault
case LoaderEmpty:
return config.LoaderEmpty
case LoaderFile:
return config.LoaderFile
case LoaderGlobalCSS:
return config.LoaderGlobalCSS
case LoaderJS:
return config.LoaderJS
case LoaderJSON:
return config.LoaderJSON
case LoaderJSX:
return config.LoaderJSX
case LoaderLocalCSS:
return config.LoaderLocalCSS
case LoaderNone:
return config.LoaderNone
case LoaderText:
return config.LoaderText
case LoaderTS:
return config.LoaderTS
case LoaderTSX:
return config.LoaderTSX
default:
panic("Invalid loader")
}
}
func extractPathStyle(absPaths AbsPaths, flag AbsPaths) logger.PathStyle {
if (absPaths & flag) != 0 {
return logger.AbsPath
} else {
return logger.RelPath
}
}
var versionRegex = regexp.MustCompile(`^([0-9]+)(?:\.([0-9]+))?(?:\.([0-9]+))?(-[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*)?$`)
func validateFeatures(log logger.Log, target Target, engines []Engine) (compat.JSFeature, compat.CSSFeature, map[css_ast.D]compat.CSSPrefix, string) {
if target == DefaultTarget && len(engines) == 0 {
return 0, 0, nil, ""
}
constraints := make(map[compat.Engine]compat.Semver)
targets := make([]string, 0, 1+len(engines))
switch target {
case ES5:
constraints[compat.ES] = compat.Semver{Parts: []int{5}}
case ES2015:
constraints[compat.ES] = compat.Semver{Parts: []int{2015}}
case ES2016:
constraints[compat.ES] = compat.Semver{Parts: []int{2016}}
case ES2017:
constraints[compat.ES] = compat.Semver{Parts: []int{2017}}
case ES2018:
constraints[compat.ES] = compat.Semver{Parts: []int{2018}}
case ES2019:
constraints[compat.ES] = compat.Semver{Parts: []int{2019}}
case ES2020:
constraints[compat.ES] = compat.Semver{Parts: []int{2020}}
case ES2021:
constraints[compat.ES] = compat.Semver{Parts: []int{2021}}
case ES2022:
constraints[compat.ES] = compat.Semver{Parts: []int{2022}}
case ES2023:
constraints[compat.ES] = compat.Semver{Parts: []int{2023}}
case ES2024:
constraints[compat.ES] = compat.Semver{Parts: []int{2024}}
case ESNext, DefaultTarget:
default:
panic("Invalid target")
}
for _, engine := range engines {
if match := versionRegex.FindStringSubmatch(engine.Version); match != nil {
if major, err := strconv.Atoi(match[1]); err == nil {
parts := []int{major}
if minor, err := strconv.Atoi(match[2]); err == nil {
parts = append(parts, minor)
if patch, err := strconv.Atoi(match[3]); err == nil {
parts = append(parts, patch)
}
}
constraints[convertEngineName(engine.Name)] = compat.Semver{
Parts: parts,
PreRelease: match[4],
}
continue
}
}
text := "All version numbers passed to esbuild must be in the format \"X\", \"X.Y\", or \"X.Y.Z\" where X, Y, and Z are non-negative integers."
log.AddErrorWithNotes(nil, logger.Range{}, fmt.Sprintf("Invalid version: %q", engine.Version),
[]logger.MsgData{{Text: text}})
}
for engine, version := range constraints {
targets = append(targets, engine.String()+version.String())
}
if target == ESNext {
targets = append(targets, "esnext")
}
sort.Strings(targets)
targetEnv := helpers.StringArrayToQuotedCommaSeparatedString(targets)
return compat.UnsupportedJSFeatures(constraints), compat.UnsupportedCSSFeatures(constraints), compat.CSSPrefixData(constraints), targetEnv
}
func validateSupported(log logger.Log, supported map[string]bool) (
jsFeature compat.JSFeature,
jsMask compat.JSFeature,
cssFeature compat.CSSFeature,
cssMask compat.CSSFeature,
) {
for k, v := range supported {
if js, ok := compat.StringToJSFeature[k]; ok {
jsMask |= js
if !v {
jsFeature |= js
}
} else if css, ok := compat.StringToCSSFeature[k]; ok {
cssMask |= css
if !v {
cssFeature |= css
}
} else {
log.AddError(nil, logger.Range{}, fmt.Sprintf("%q is not a valid feature name for the \"supported\" setting", k))
}
}
return
}
func validateGlobalName(log logger.Log, text string, path string) []string {
if text != "" {
source := logger.Source{
KeyPath: logger.Path{Text: path},
PrettyPaths: logger.PrettyPaths{Abs: path, Rel: path},
Contents: text,
}
if result, ok := js_parser.ParseGlobalName(log, source); ok {
return result
}
}
return nil
}
func validateRegex(log logger.Log, what string, value string) *regexp.Regexp {
if value == "" {
return nil
}
regex, err := regexp.Compile(value)
if err != nil {
log.AddError(nil, logger.Range{},
fmt.Sprintf("The %q setting is not a valid Go regular expression: %s", what, value))
return nil
}
return regex
}
func validateExternals(log logger.Log, fs fs.FS, paths []string) config.ExternalSettings {
result := config.ExternalSettings{
PreResolve: config.ExternalMatchers{Exact: make(map[string]bool)},
PostResolve: config.ExternalMatchers{Exact: make(map[string]bool)},
}
for _, path := range paths {
if index := strings.IndexByte(path, '*'); index != -1 {
// Wildcard behavior
if strings.ContainsRune(path[index+1:], '*') {
log.AddError(nil, logger.Range{}, fmt.Sprintf("External path %q cannot have more than one \"*\" wildcard", path))
} else {
result.PreResolve.Patterns = append(result.PreResolve.Patterns, config.WildcardPattern{Prefix: path[:index], Suffix: path[index+1:]})
if !resolver.IsPackagePath(path) {
if absPath := validatePath(log, fs, path, "external path"); absPath != "" {
if absIndex := strings.IndexByte(absPath, '*'); absIndex != -1 && !strings.ContainsRune(absPath[absIndex+1:], '*') {
result.PostResolve.Patterns = append(result.PostResolve.Patterns, config.WildcardPattern{Prefix: absPath[:absIndex], Suffix: absPath[absIndex+1:]})
}
}
}
}
} else {
// Non-wildcard behavior
result.PreResolve.Exact[path] = true
if resolver.IsPackagePath(path) {
result.PreResolve.Patterns = append(result.PreResolve.Patterns, config.WildcardPattern{Prefix: path + "/"})
} else if absPath := validatePath(log, fs, path, "external path"); absPath != "" {
result.PostResolve.Exact[absPath] = true
}
}
}
return result
}
func validateAlias(log logger.Log, fs fs.FS, alias map[string]string) map[string]string {
valid := make(map[string]string, len(alias))
for old, new := range alias {
if new == "" {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid alias substitution: %q", new))
continue
}
// Valid alias names:
// "foo"
// "foo/bar"
// "@foo"
// "@foo/bar"
// "@foo/bar/baz"
//
// Invalid alias names:
// "./foo"
// "../foo"
// "/foo"
// "C:\\foo"
// ".foo"
// "foo/"
// "@foo/"
// "foo/../bar"
//
if !strings.HasPrefix(old, ".") && !strings.HasPrefix(old, "/") && !fs.IsAbs(old) && path.Clean(strings.ReplaceAll(old, "\\", "/")) == old {
valid[old] = new
continue
}
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid alias name: %q", old))
}
return valid
}
func isValidExtension(ext string) bool {
return len(ext) >= 2 && ext[0] == '.' && ext[len(ext)-1] != '.'
}
func validateResolveExtensions(log logger.Log, order []string) []string {
if order == nil {
return []string{".tsx", ".ts", ".jsx", ".js", ".css", ".json"}
}
for _, ext := range order {
if !isValidExtension(ext) {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid file extension: %q", ext))
}
}
return order
}
func validateLoaders(log logger.Log, loaders map[string]Loader) map[string]config.Loader {
result := bundler.DefaultExtensionToLoaderMap()
for ext, loader := range loaders {
if ext != "" && !isValidExtension(ext) {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid file extension: %q", ext))
}
result[ext] = validateLoader(loader)
}
return result
}
func validateJSXExpr(log logger.Log, text string, name string) config.DefineExpr {
if text != "" {
if expr, _ := js_parser.ParseDefineExpr(text); len(expr.Parts) > 0 || (name == "fragment" && expr.Constant != nil) {
return expr
}
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid JSX %s: %q", name, text))
}
return config.DefineExpr{}
}
// This returns an arbitrary but unique key for each unique array of strings
func mapKeyForDefine(parts []string) string {
var sb strings.Builder
var n [4]byte
for _, part := range parts {
binary.LittleEndian.PutUint32(n[:], uint32(len(part)))
sb.Write(n[:])
sb.WriteString(part)
}
return sb.String()
}
func validateDefines(
log logger.Log,
defines map[string]string,
pureFns []string,
platform config.Platform,
isBuildAPI bool,
minify bool,
drop Drop,
) (*config.ProcessedDefines, []config.InjectedDefine) {
// Sort injected defines for determinism, since the imports will be injected
// into every file in the order that we return them from this function
sortedKeys := make([]string, 0, len(defines))
for key := range defines {
sortedKeys = append(sortedKeys, key)
}
sort.Strings(sortedKeys)
rawDefines := make(map[string]config.DefineData)
nodeEnvParts := []string{"process", "env", "NODE_ENV"}
nodeEnvMapKey := mapKeyForDefine(nodeEnvParts)
var injectedDefines []config.InjectedDefine
for _, key := range sortedKeys {
value := defines[key]
keyParts := validateGlobalName(log, key, "(define name)")
if keyParts == nil {
continue
}
mapKey := mapKeyForDefine(keyParts)
// Parse the value
defineExpr, injectExpr := js_parser.ParseDefineExpr(value)
// Define simple expressions
if defineExpr.Constant != nil || len(defineExpr.Parts) > 0 {
rawDefines[mapKey] = config.DefineData{KeyParts: keyParts, DefineExpr: &defineExpr}
// Try to be helpful for common mistakes
if len(defineExpr.Parts) == 1 && mapKey == nodeEnvMapKey {
data := logger.MsgData{
Text: fmt.Sprintf("%q is defined as an identifier instead of a string (surround %q with quotes to get a string)", key, value),
}
part := defineExpr.Parts[0]
switch logger.API {
case logger.CLIAPI:
data.Location = &logger.MsgLocation{
File: logger.PrettyPaths{Abs: "<cli>", Rel: "<cli>"},
Line: 1,
Column: 30,
Length: len(part),
LineText: fmt.Sprintf("--define:process.env.NODE_ENV=%s", part),
Suggestion: fmt.Sprintf("\\\"%s\\\"", part),
}
case logger.JSAPI:
data.Location = &logger.MsgLocation{
File: logger.PrettyPaths{Abs: "<js>", Rel: "<js>"},
Line: 1,
Column: 34,
Length: len(part) + 2,
LineText: fmt.Sprintf("define: { 'process.env.NODE_ENV': '%s' }", part),
Suggestion: fmt.Sprintf("'\"%s\"'", part),
}
case logger.GoAPI:
data.Location = &logger.MsgLocation{
File: logger.PrettyPaths{Abs: "<go>", Rel: "<go>"},
Line: 1,
Column: 50,
Length: len(part) + 2,
LineText: fmt.Sprintf("Define: map[string]string{\"process.env.NODE_ENV\": \"%s\"}", part),
Suggestion: fmt.Sprintf("\"\\\"%s\\\"\"", part),
}
}
log.AddMsgID(logger.MsgID_JS_SuspiciousDefine, logger.Msg{
Kind: logger.Warning,
Data: data,
})
}
continue
}
// Inject complex expressions
if injectExpr != nil {
index := ast.MakeIndex32(uint32(len(injectedDefines)))
injectedDefines = append(injectedDefines, config.InjectedDefine{
Source: logger.Source{Contents: value},
Data: injectExpr,
Name: key,
})
rawDefines[mapKey] = config.DefineData{KeyParts: keyParts, DefineExpr: &config.DefineExpr{InjectedDefineIndex: index}}
continue
}
// Anything else is unsupported
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid define value (must be an entity name or JS literal): %s", value))
}
// If we're bundling for the browser, add a special-cased define for
// "process.env.NODE_ENV" that is "development" when not minifying and
// "production" when minifying. This is a convention from the React world
// that must be handled to avoid all React code crashing instantly. This
// is only done if it's not already defined so that you can override it if
// necessary.
if isBuildAPI && platform == config.PlatformBrowser {
if _, process := rawDefines[mapKeyForDefine([]string{"process"})]; !process {
if _, processEnv := rawDefines[mapKeyForDefine([]string{"process.env"})]; !processEnv {
if _, processEnvNodeEnv := rawDefines[nodeEnvMapKey]; !processEnvNodeEnv {
var value []uint16
if minify {
value = helpers.StringToUTF16("production")
} else {
value = helpers.StringToUTF16("development")
}
rawDefines[nodeEnvMapKey] = config.DefineData{KeyParts: nodeEnvParts, DefineExpr: &config.DefineExpr{Constant: &js_ast.EString{Value: value}}}
}
}
}
}
// If we're dropping all console API calls, replace each one with undefined
if (drop & DropConsole) != 0 {
consoleParts := []string{"console"}
consoleMapKey := mapKeyForDefine(consoleParts)
define := rawDefines[consoleMapKey]
define.KeyParts = consoleParts
define.Flags |= config.MethodCallsMustBeReplacedWithUndefined
rawDefines[consoleMapKey] = define
}
for _, key := range pureFns {
keyParts := validateGlobalName(log, key, "(pure name)")
if keyParts == nil {
continue
}
mapKey := mapKeyForDefine(keyParts)
// Merge with any previously-specified defines
define := rawDefines[mapKey]
define.KeyParts = keyParts
define.Flags |= config.CallCanBeUnwrappedIfUnused
rawDefines[mapKey] = define
}
// Processing defines is expensive. Process them once here so the same object
// can be shared between all parsers we create using these arguments.
definesArray := make([]config.DefineData, 0, len(rawDefines))
for _, define := range rawDefines {
definesArray = append(definesArray, define)
}
processed := config.ProcessDefines(definesArray)
return &processed, injectedDefines
}
func validateLogOverrides(input map[string]LogLevel) (output map[logger.MsgID]logger.LogLevel) {
output = make(map[uint8]logger.LogLevel)
for k, v := range input {
logger.StringToMsgIDs(k, validateLogLevel(v), output)
}
return
}
func validatePath(log logger.Log, fs fs.FS, relPath string, pathKind string) string {
if relPath == "" {
return ""
}
absPath, ok := fs.Abs(relPath)
if !ok {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid %s: %s", pathKind, relPath))
}
return absPath
}
func validateOutputExtensions(log logger.Log, outExtensions map[string]string) (js string, css string) {
for key, value := range outExtensions {
if !isValidExtension(value) {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid output extension: %q", value))
}
switch key {
case ".js":
js = value
case ".css":
css = value
default:
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid output extension: %q (valid: .css, .js)", key))
}
}
return
}
func validateBannerOrFooter(log logger.Log, name string, values map[string]string) (js string, css string) {
for key, value := range values {
switch key {
case "js":
js = value
case "css":
css = value
default:
log.AddError(nil, logger.Range{}, fmt.Sprintf("Invalid %s file type: %q (valid: css, js)", name, key))
}
}
return
}
func validateKeepNames(log logger.Log, options *config.Options) {
if options.KeepNames && options.UnsupportedJSFeatures.Has(compat.FunctionNameConfigurable) {
where := config.PrettyPrintTargetEnvironment(options.OriginalTargetEnv, options.UnsupportedJSFeatureOverridesMask)
log.AddErrorWithNotes(nil, logger.Range{}, fmt.Sprintf("The \"keep names\" setting cannot be used with %s", where), []logger.MsgData{{
Text: "In this environment, the \"Function.prototype.name\" property is not configurable and assigning to it will throw an error. " +
"Either use a newer target environment or disable the \"keep names\" setting."}})
}
}
func convertLocationToPublic(loc *logger.MsgLocation, pathStyle logger.PathStyle) *Location {
if loc != nil {
return &Location{
File: loc.File.Select(pathStyle),
Namespace: loc.Namespace,
Line: loc.Line,
Column: loc.Column,
Length: loc.Length,
LineText: loc.LineText,
Suggestion: loc.Suggestion,
}
}
return nil
}
func convertMessagesToPublic(kind logger.MsgKind, msgs []logger.Msg, pathStyle logger.PathStyle) []Message {
var filtered []Message
for _, msg := range msgs {
if msg.Kind == kind {
var notes []Note
for _, note := range msg.Notes {
notes = append(notes, Note{
Text: note.Text,
Location: convertLocationToPublic(note.Location, pathStyle),
})
}
filtered = append(filtered, Message{
ID: logger.MsgIDToString(msg.ID),
PluginName: msg.PluginName,
Text: msg.Data.Text,
Location: convertLocationToPublic(msg.Data.Location, pathStyle),
Notes: notes,
Detail: msg.Data.UserDetail,
})
}
}
return filtered
}
func convertLocationToInternal(loc *Location) *logger.MsgLocation {
if loc != nil {
namespace := loc.Namespace
if namespace == "" {
namespace = "file"
}
return &logger.MsgLocation{
File: logger.PrettyPaths{Abs: loc.File, Rel: loc.File},
Namespace: namespace,
Line: loc.Line,
Column: loc.Column,
Length: loc.Length,
LineText: loc.LineText,
Suggestion: loc.Suggestion,
}
}
return nil
}
func convertMessagesToInternal(msgs []logger.Msg, kind logger.MsgKind, messages []Message) []logger.Msg {
for _, message := range messages {
var notes []logger.MsgData
for _, note := range message.Notes {
notes = append(notes, logger.MsgData{
Text: note.Text,
Location: convertLocationToInternal(note.Location),
})
}
msgs = append(msgs, logger.Msg{
ID: logger.StringToMaximumMsgID(message.ID),
PluginName: message.PluginName,
Kind: kind,
Data: logger.MsgData{
Text: message.Text,
Location: convertLocationToInternal(message.Location),
UserDetail: message.Detail,
},
Notes: notes,
})
}
return msgs
}
func convertErrorsAndWarningsToInternal(errors []Message, warnings []Message) []logger.Msg {
if len(errors)+len(warnings) > 0 {
msgs := make(logger.SortableMsgs, 0, len(errors)+len(warnings))
msgs = convertMessagesToInternal(msgs, logger.Error, errors)
msgs = convertMessagesToInternal(msgs, logger.Warning, warnings)
sort.Stable(msgs)
return msgs
}
return nil
}
func cloneMangleCache(log logger.Log, mangleCache map[string]interface{}) map[string]interface{} {
if mangleCache == nil {
return nil
}
clone := make(map[string]interface{}, len(mangleCache))
for k, v := range mangleCache {
if v == "__proto__" {
// This could cause problems for our binary serialization protocol. It's
// also unnecessary because we already avoid mangling this property name.
log.AddError(nil, logger.Range{},
fmt.Sprintf("Invalid identifier name %q in mangle cache", k))
} else if _, ok := v.(string); ok || v == false {
clone[k] = v
} else {
log.AddError(nil, logger.Range{},
fmt.Sprintf("Expected %q in mangle cache to map to either a string or false", k))
}
}
return clone
}
////////////////////////////////////////////////////////////////////////////////
// Build API
func contextImpl(buildOpts BuildOptions) (*internalContext, []Message) {
logOptions := logger.OutputOptions{
IncludeSource: true,
MessageLimit: buildOpts.LogLimit,
Color: validateColor(buildOpts.Color),
LogLevel: validateLogLevel(buildOpts.LogLevel),
PathStyle: extractPathStyle(buildOpts.AbsPaths, LogAbsPath),
Overrides: validateLogOverrides(buildOpts.LogOverride),
}
// Validate that the current working directory is an absolute path
absWorkingDir := buildOpts.AbsWorkingDir
realFS, err := fs.RealFS(fs.RealFSOptions{
AbsWorkingDir: absWorkingDir,
// This is a long-lived file system object so do not cache calls to
// ReadDirectory() (they are normally cached for the duration of a build
// for performance).
DoNotCache: true,
})
if err != nil {
log := logger.NewStderrLog(logOptions)
log.AddError(nil, logger.Range{}, err.Error())
return nil, convertMessagesToPublic(logger.Error, log.Done(), logOptions.PathStyle)
}
// Do not re-evaluate plugins when rebuilding. Also make sure the working
// directory doesn't change, since breaking that invariant would break the
// validation that we just did above.
caches := cache.MakeCacheSet()
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, logOptions.Overrides)
onEndCallbacks, onDisposeCallbacks, finalizeBuildOptions := loadPlugins(&buildOpts, realFS, log, caches)
options, entryPoints := validateBuildOptions(buildOpts, log, realFS)
finalizeBuildOptions(&options)
if buildOpts.AbsWorkingDir != absWorkingDir {
panic("Mutating \"AbsWorkingDir\" is not allowed")
}
// If we have errors already, then refuse to build any further. This only
// happens when the build options themselves contain validation errors.
msgs := log.Done()
if log.HasErrors() {
if logOptions.LogLevel < logger.LevelSilent {
// Print all deferred validation log messages to stderr. We defer all log
// messages that are generated above because warnings are re-printed for
// every rebuild and we don't want to double-print these warnings for the
// first build.
stderr := logger.NewStderrLog(logOptions)
for _, msg := range msgs {
stderr.AddMsg(msg)
}
stderr.Done()
}
return nil, convertMessagesToPublic(logger.Error, msgs, options.LogPathStyle)
}
args := rebuildArgs{
caches: caches,
onEndCallbacks: onEndCallbacks,
onDisposeCallbacks: onDisposeCallbacks,
logOptions: logOptions,
logWarnings: msgs,
entryPoints: entryPoints,
options: options,
mangleCache: buildOpts.MangleCache,
absWorkingDir: absWorkingDir,
write: buildOpts.Write,
}
return &internalContext{
args: args,
realFS: realFS,
absWorkingDir: absWorkingDir,
}, nil
}
type buildInProgress struct {
state rebuildState
waitGroup sync.WaitGroup
cancel config.CancelFlag
}
type internalContext struct {
mutex sync.Mutex
args rebuildArgs
activeBuild *buildInProgress
recentBuild *BuildResult
realFS fs.FS
absWorkingDir string
watcher *watcher
handler *apiHandler
didDispose bool
// This saves just enough information to be able to compute a useful diff
// between two sets of output files. That way we don't need to hold both
// sets of output files in memory at once to compute a diff.
latestHashes map[string]string
}
func (ctx *internalContext) rebuild() rebuildState {
ctx.mutex.Lock()
// Ignore disposed contexts
if ctx.didDispose {
ctx.mutex.Unlock()
return rebuildState{}
}
// If there's already an active build, just return that build's result
if build := ctx.activeBuild; build != nil {
ctx.mutex.Unlock()
build.waitGroup.Wait()
return build.state
}
// Otherwise, start a new build
build := &buildInProgress{}
build.waitGroup.Add(1)
ctx.activeBuild = build
args := ctx.args
watcher := ctx.watcher
handler := ctx.handler
oldHashes := ctx.latestHashes
args.options.CancelFlag = &build.cancel
ctx.mutex.Unlock()
// Do the build without holding the mutex
var newHashes map[string]string
build.state, newHashes = rebuildImpl(args, oldHashes)
if handler != nil {
handler.broadcastBuildResult(build.state.result, newHashes)
}
if watcher != nil {
watcher.setWatchData(build.state.watchData)
}
// Store the recent build for the dev server
recentBuild := &build.state.result
ctx.mutex.Lock()
ctx.activeBuild = nil
ctx.recentBuild = recentBuild
ctx.latestHashes = newHashes
ctx.mutex.Unlock()
// Clear the recent build after it goes stale
go func() {
time.Sleep(250 * time.Millisecond)
ctx.mutex.Lock()
if ctx.recentBuild == recentBuild {
ctx.recentBuild = nil
}
ctx.mutex.Unlock()
}()
build.waitGroup.Done()
return build.state
}
// This is used by the dev server. The dev server does a rebuild on each
// incoming request since a) we want incoming requests to always be up to
// date and b) we don't necessarily know what output paths to even serve
// without running another build (e.g. the hashes may have changed).
//
// However, there is a small period of time where we reuse old build results
// instead of generating new ones. This is because page loads likely involve
// multiple requests, and don't want to rebuild separately for each of those
// requests.
func (ctx *internalContext) activeBuildOrRecentBuildOrRebuild() BuildResult {
ctx.mutex.Lock()
// If there's already an active build, wait for it and return that
if build := ctx.activeBuild; build != nil {
ctx.mutex.Unlock()
build.waitGroup.Wait()
return build.state.result
}
// Then try to return a recentl already-completed build
if build := ctx.recentBuild; build != nil {
ctx.mutex.Unlock()
return *build
}
// Otherwise, fall back to rebuilding
ctx.mutex.Unlock()
return ctx.Rebuild()
}
func (ctx *internalContext) Rebuild() BuildResult {
return ctx.rebuild().result
}
func (ctx *internalContext) Watch(options WatchOptions) error {
ctx.mutex.Lock()
defer ctx.mutex.Unlock()
// Ignore disposed contexts
if ctx.didDispose {
return errors.New("Cannot watch a disposed context")
}
// Don't allow starting watch mode multiple times
if ctx.watcher != nil {
return errors.New("Watch mode has already been enabled")
}
logLevel := ctx.args.logOptions.LogLevel
ctx.watcher = &watcher{
fs: ctx.realFS,
shouldLog: logLevel == logger.LevelInfo || logLevel == logger.LevelDebug || logLevel == logger.LevelVerbose,
useColor: ctx.args.logOptions.Color,
pathStyle: ctx.args.logOptions.PathStyle,
rebuild: func() fs.WatchData {
return ctx.rebuild().watchData
},
delayInMS: time.Duration(options.Delay),
}
// All subsequent builds will be watch mode builds
ctx.args.options.WatchMode = true
// Start the file watcher goroutine
ctx.watcher.start()
// Do the first watch mode build on another goroutine
go func() {
ctx.mutex.Lock()
build := ctx.activeBuild
ctx.mutex.Unlock()
// If there's an active build, then it's not a watch build. Wait for it to
// finish first so we don't just get this build when we call "Rebuild()".
if build != nil {
build.waitGroup.Wait()
}
// Trigger a rebuild now that we know all future builds will pick up on
// our watcher. This build will populate the initial watch data, which is
// necessary to be able to know what file system changes are relevant.
ctx.Rebuild()
}()
return nil
}
func (ctx *internalContext) Cancel() {
ctx.mutex.Lock()
// Ignore disposed contexts
if ctx.didDispose {
ctx.mutex.Unlock()
return
}
build := ctx.activeBuild
ctx.mutex.Unlock()
if build != nil {
// Tell observers to cut this build short
build.cancel.Cancel()
// Wait for the build to finish before returning
build.waitGroup.Wait()
}
}
func (ctx *internalContext) Dispose() {
// Only dispose once
ctx.mutex.Lock()
if ctx.didDispose {
ctx.mutex.Unlock()
return
}
ctx.didDispose = true
ctx.recentBuild = nil
build := ctx.activeBuild
ctx.mutex.Unlock()
if ctx.watcher != nil {
ctx.watcher.stop()
}
if ctx.handler != nil {
ctx.handler.stop()
}
// It's important to wait for the build to finish before returning. The JS
// API will unregister its callbacks when it returns. If that happens while
// the build is still in progress, that might cause the JS API to generate
// errors when we send it events (e.g. when it runs "onEnd" callbacks) that
// we then print to the terminal, which would be confusing.
if build != nil {
build.waitGroup.Wait()
}
// Run each "OnDispose" callback on its own goroutine
for _, fn := range ctx.args.onDisposeCallbacks {
go fn()
}
}
func prettyPrintByteCount(n int) string {
var size string
if n < 1024 {
size = fmt.Sprintf("%db ", n)
} else if n < 1024*1024 {
size = fmt.Sprintf("%.1fkb", float64(n)/(1024))
} else if n < 1024*1024*1024 {
size = fmt.Sprintf("%.1fmb", float64(n)/(1024*1024))
} else {
size = fmt.Sprintf("%.1fgb", float64(n)/(1024*1024*1024))
}
return size
}
func printSummary(color logger.UseColor, outputFiles []OutputFile, start time.Time) {
if len(outputFiles) == 0 {
return
}
var table logger.SummaryTable = make([]logger.SummaryTableEntry, len(outputFiles))
if cwd, err := os.Getwd(); err == nil {
if realFS, err := fs.RealFS(fs.RealFSOptions{AbsWorkingDir: cwd}); err == nil {
for i, file := range outputFiles {
path, ok := realFS.Rel(realFS.Cwd(), file.Path)
if !ok {
path = file.Path
}
base := realFS.Base(path)
n := len(file.Contents)
table[i] = logger.SummaryTableEntry{
Dir: path[:len(path)-len(base)],
Base: base,
Size: prettyPrintByteCount(n),
Bytes: n,
IsSourceMap: strings.HasSuffix(base, ".map"),
}
}
}
}
// Don't print the time taken by the build if we're running under Yarn 1
// since Yarn 1 always prints its own copy of the time taken by each command
if userAgent, ok := os.LookupEnv("npm_config_user_agent"); ok {
if strings.Contains(userAgent, "yarn/1.") {
logger.PrintSummary(color, table, nil)
return
}
}
logger.PrintSummary(color, table, &start)
}
func validateBuildOptions(
buildOpts BuildOptions,
log logger.Log,
realFS fs.FS,
) (
options config.Options,
entryPoints []bundler.EntryPoint,
) {
jsFeatures, cssFeatures, cssPrefixData, targetEnv := validateFeatures(log, buildOpts.Target, buildOpts.Engines)
jsOverrides, jsMask, cssOverrides, cssMask := validateSupported(log, buildOpts.Supported)
outJS, outCSS := validateOutputExtensions(log, buildOpts.OutExtension)
bannerJS, bannerCSS := validateBannerOrFooter(log, "banner", buildOpts.Banner)
footerJS, footerCSS := validateBannerOrFooter(log, "footer", buildOpts.Footer)
minify := buildOpts.MinifyWhitespace && buildOpts.MinifyIdentifiers && buildOpts.MinifySyntax
platform := validatePlatform(buildOpts.Platform)
defines, injectedDefines := validateDefines(log, buildOpts.Define, buildOpts.Pure, platform, true /* isBuildAPI */, minify, buildOpts.Drop)
options = config.Options{
CSSPrefixData: cssPrefixData,
UnsupportedJSFeatures: jsFeatures.ApplyOverrides(jsOverrides, jsMask),
UnsupportedCSSFeatures: cssFeatures.ApplyOverrides(cssOverrides, cssMask),
UnsupportedJSFeatureOverrides: jsOverrides,
UnsupportedJSFeatureOverridesMask: jsMask,
UnsupportedCSSFeatureOverrides: cssOverrides,
UnsupportedCSSFeatureOverridesMask: cssMask,
OriginalTargetEnv: targetEnv,
JSX: config.JSXOptions{
Preserve: buildOpts.JSX == JSXPreserve,
AutomaticRuntime: buildOpts.JSX == JSXAutomatic,
Factory: validateJSXExpr(log, buildOpts.JSXFactory, "factory"),
Fragment: validateJSXExpr(log, buildOpts.JSXFragment, "fragment"),
Development: buildOpts.JSXDev,
ImportSource: buildOpts.JSXImportSource,
SideEffects: buildOpts.JSXSideEffects,
},
Defines: defines,
InjectedDefines: injectedDefines,
Platform: platform,
SourceMap: validateSourceMap(buildOpts.Sourcemap),
LegalComments: validateLegalComments(buildOpts.LegalComments, buildOpts.Bundle),
SourceRoot: buildOpts.SourceRoot,
ExcludeSourcesContent: buildOpts.SourcesContent == SourcesContentExclude,
MinifySyntax: buildOpts.MinifySyntax,
MinifyWhitespace: buildOpts.MinifyWhitespace,
MinifyIdentifiers: buildOpts.MinifyIdentifiers,
LineLimit: buildOpts.LineLimit,
MangleProps: validateRegex(log, "mangle props", buildOpts.MangleProps),
ReserveProps: validateRegex(log, "reserve props", buildOpts.ReserveProps),
MangleQuoted: buildOpts.MangleQuoted == MangleQuotedTrue,
DropLabels: append([]string{}, buildOpts.DropLabels...),
DropDebugger: (buildOpts.Drop & DropDebugger) != 0,
AllowOverwrite: buildOpts.AllowOverwrite,
ASCIIOnly: validateASCIIOnly(buildOpts.Charset),
IgnoreDCEAnnotations: buildOpts.IgnoreAnnotations,
TreeShaking: validateTreeShaking(buildOpts.TreeShaking, buildOpts.Bundle, buildOpts.Format),
GlobalName: validateGlobalName(log, buildOpts.GlobalName, "(global name)"),
CodeSplitting: buildOpts.Splitting,
OutputFormat: validateFormat(buildOpts.Format),
AbsOutputFile: validatePath(log, realFS, buildOpts.Outfile, "outfile path"),
AbsOutputDir: validatePath(log, realFS, buildOpts.Outdir, "outdir path"),
AbsOutputBase: validatePath(log, realFS, buildOpts.Outbase, "outbase path"),
NeedsMetafile: buildOpts.Metafile,
EntryPathTemplate: validatePathTemplate(buildOpts.EntryNames),
ChunkPathTemplate: validatePathTemplate(buildOpts.ChunkNames),
AssetPathTemplate: validatePathTemplate(buildOpts.AssetNames),
OutputExtensionJS: outJS,
OutputExtensionCSS: outCSS,
ExtensionToLoader: validateLoaders(log, buildOpts.Loader),
ExtensionOrder: validateResolveExtensions(log, buildOpts.ResolveExtensions),
ExternalSettings: validateExternals(log, realFS, buildOpts.External),
ExternalPackages: validateExternalPackages(buildOpts.Packages),
PackageAliases: validateAlias(log, realFS, buildOpts.Alias),
TSConfigPath: validatePath(log, realFS, buildOpts.Tsconfig, "tsconfig path"),
TSConfigRaw: buildOpts.TsconfigRaw,
MainFields: buildOpts.MainFields,
PublicPath: buildOpts.PublicPath,
KeepNames: buildOpts.KeepNames,
CodePathStyle: extractPathStyle(buildOpts.AbsPaths, CodeAbsPath),
LogPathStyle: extractPathStyle(buildOpts.AbsPaths, LogAbsPath),
MetafilePathStyle: extractPathStyle(buildOpts.AbsPaths, MetafileAbsPath),
InjectPaths: append([]string{}, buildOpts.Inject...),
AbsNodePaths: make([]string, len(buildOpts.NodePaths)),
JSBanner: bannerJS,
JSFooter: footerJS,
CSSBanner: bannerCSS,
CSSFooter: footerCSS,
PreserveSymlinks: buildOpts.PreserveSymlinks,
}
validateKeepNames(log, &options)
if buildOpts.Conditions != nil {
options.Conditions = append([]string{}, buildOpts.Conditions...)
}
if options.MainFields != nil {
options.MainFields = append([]string{}, options.MainFields...)
}
for i, path := range buildOpts.NodePaths {
options.AbsNodePaths[i] = validatePath(log, realFS, path, "node path")
}
entryPoints = make([]bundler.EntryPoint, 0, len(buildOpts.EntryPoints)+len(buildOpts.EntryPointsAdvanced))
hasEntryPointWithWildcard := false
for _, ep := range buildOpts.EntryPoints {
entryPoints = append(entryPoints, bundler.EntryPoint{InputPath: ep})
if strings.ContainsRune(ep, '*') {
hasEntryPointWithWildcard = true
}
}
for _, ep := range buildOpts.EntryPointsAdvanced {
entryPoints = append(entryPoints, bundler.EntryPoint{InputPath: ep.InputPath, OutputPath: ep.OutputPath})
if strings.ContainsRune(ep.InputPath, '*') {
hasEntryPointWithWildcard = true
}
}
entryPointCount := len(entryPoints)
if buildOpts.Stdin != nil {
entryPointCount++
options.Stdin = &config.StdinInfo{
Loader: validateLoader(buildOpts.Stdin.Loader),
Contents: buildOpts.Stdin.Contents,
SourceFile: buildOpts.Stdin.Sourcefile,
AbsResolveDir: validatePath(log, realFS, buildOpts.Stdin.ResolveDir, "resolve directory path"),
}
}
if options.AbsOutputDir == "" && (entryPointCount > 1 || hasEntryPointWithWildcard) {
log.AddError(nil, logger.Range{},
"Must use \"outdir\" when there are multiple input files")
} else if options.AbsOutputDir == "" && options.CodeSplitting {
log.AddError(nil, logger.Range{},
"Must use \"outdir\" when code splitting is enabled")
} else if options.AbsOutputFile != "" && options.AbsOutputDir != "" {
log.AddError(nil, logger.Range{}, "Cannot use both \"outfile\" and \"outdir\"")
} else if options.AbsOutputFile != "" {
// If the output file is specified, use it to derive the output directory
options.AbsOutputDir = realFS.Dir(options.AbsOutputFile)
} else if options.AbsOutputDir == "" {
options.WriteToStdout = true
// Forbid certain features when writing to stdout
if options.SourceMap != config.SourceMapNone && options.SourceMap != config.SourceMapInline {
log.AddError(nil, logger.Range{}, "Cannot use an external source map without an output path")
}
if options.LegalComments.HasExternalFile() {
log.AddError(nil, logger.Range{}, "Cannot use linked or external legal comments without an output path")
}
for _, loader := range options.ExtensionToLoader {
if loader == config.LoaderFile {
log.AddError(nil, logger.Range{}, "Cannot use the \"file\" loader without an output path")
break
}
if loader == config.LoaderCopy {
log.AddError(nil, logger.Range{}, "Cannot use the \"copy\" loader without an output path")
break
}
}
// Use the current directory as the output directory instead of an empty
// string because external modules with relative paths need a base directory.
options.AbsOutputDir = realFS.Cwd()
}
if !buildOpts.Bundle {
// Disallow bundle-only options when not bundling
if options.ExternalSettings.PreResolve.HasMatchers() || options.ExternalSettings.PostResolve.HasMatchers() {
log.AddError(nil, logger.Range{}, "Cannot use \"external\" without \"bundle\"")
}
if len(options.PackageAliases) > 0 {
log.AddError(nil, logger.Range{}, "Cannot use \"alias\" without \"bundle\"")
}
} else if options.OutputFormat == config.FormatPreserve {
// If the format isn't specified, set the default format using the platform
switch options.Platform {
case config.PlatformBrowser:
options.OutputFormat = config.FormatIIFE
case config.PlatformNode:
options.OutputFormat = config.FormatCommonJS
case config.PlatformNeutral:
options.OutputFormat = config.FormatESModule
}
}
// Set the output mode using other settings
if buildOpts.Bundle {
options.Mode = config.ModeBundle
} else if options.OutputFormat != config.FormatPreserve {
options.Mode = config.ModeConvertFormat
}
// Automatically enable the "module" condition for better tree shaking
if options.Conditions == nil && options.Platform != config.PlatformNeutral {
options.Conditions = []string{"module"}
}
// Code splitting is experimental and currently only enabled for ES6 modules
if options.CodeSplitting && options.OutputFormat != config.FormatESModule {
log.AddError(nil, logger.Range{}, "Splitting currently only works with the \"esm\" format")
}
// Code splitting is experimental and currently only enabled for ES6 modules
if options.TSConfigPath != "" && options.TSConfigRaw != "" {
log.AddError(nil, logger.Range{}, "Cannot provide \"tsconfig\" as both a raw string and a path")
}
// If we aren't writing the output to the file system, then we can allow the
// output paths to be the same as the input paths. This helps when serving.
if !buildOpts.Write {
options.AllowOverwrite = true
}
return
}
type onEndCallback struct {
pluginName string
fn func(*BuildResult) (OnEndResult, error)
}
type rebuildArgs struct {
caches *cache.CacheSet
onEndCallbacks []onEndCallback
onDisposeCallbacks []func()
logOptions logger.OutputOptions
logWarnings []logger.Msg
entryPoints []bundler.EntryPoint
options config.Options
mangleCache map[string]interface{}
absWorkingDir string
write bool
}
type rebuildState struct {
result BuildResult
watchData fs.WatchData
options config.Options
}
func rebuildImpl(args rebuildArgs, oldHashes map[string]string) (rebuildState, map[string]string) {
log := logger.NewStderrLog(args.logOptions)
// All validation warnings are repeated for every rebuild
for _, msg := range args.logWarnings {
log.AddMsg(msg)
}
// Convert and validate the buildOpts
realFS, err := fs.RealFS(fs.RealFSOptions{
AbsWorkingDir: args.absWorkingDir,
WantWatchData: args.options.WatchMode,
})
if err != nil {
// This should already have been checked by the caller
panic(err.Error())
}
var result BuildResult
var watchData fs.WatchData
var toWriteToStdout []byte
var timer *helpers.Timer
if api_helpers.UseTimer {
timer = &helpers.Timer{}
}
// Scan over the bundle
bundle := bundler.ScanBundle(config.BuildCall, log, realFS, args.caches, args.entryPoints, args.options, timer)
watchData = realFS.WatchData()
// The new build summary remains the same as the old one when there are
// errors. A failed build shouldn't erase the previous successful build.
newHashes := oldHashes
// Stop now if there were errors
var results []graph.OutputFile
var metafile string
if !log.HasErrors() {
// Compile the bundle
result.MangleCache = cloneMangleCache(log, args.mangleCache)
results, metafile = bundle.Compile(log, timer, result.MangleCache, linker.Link)
// Canceling a build generates a single error at the end of the build
if args.options.CancelFlag.DidCancel() {
log.AddError(nil, logger.Range{}, "The build was canceled")
}
// Stop now if there were errors
if !log.HasErrors() {
result.Metafile = metafile
}
}
// Populate the results to return
var hashBytes [8]byte
result.OutputFiles = make([]OutputFile, len(results))
newHashes = make(map[string]string)
for i, item := range results {
if args.options.WriteToStdout {
item.AbsPath = "<stdout>"
}
hasher := xxhash.New()
hasher.Write(item.Contents)
binary.LittleEndian.PutUint64(hashBytes[:], hasher.Sum64())
hash := base64.RawStdEncoding.EncodeToString(hashBytes[:])
result.OutputFiles[i] = OutputFile{
Path: item.AbsPath,
Contents: item.Contents,
Hash: hash,
}
newHashes[item.AbsPath] = hash
}
// Write output files before "OnEnd" callbacks run so they can expect
// output files to exist on the file system. "OnEnd" callbacks can be
// used to move output files to a different location after the build.
if args.write {
timer.Begin("Write output files")
if args.options.WriteToStdout {
// Special-case writing to stdout
if log.HasErrors() {
// No output is printed if there were any build errors
} else if len(results) != 1 {
log.AddError(nil, logger.Range{}, fmt.Sprintf(
"Internal error: did not expect to generate %d files when writing to stdout", len(results)))
} else {
// Print this later on, at the end of the current function
toWriteToStdout = results[0].Contents
}
} else {
// Delete old files that are no longer relevant
var toDelete []string
for absPath := range oldHashes {
if _, ok := newHashes[absPath]; !ok {
toDelete = append(toDelete, absPath)
}
}
// Process all file operations in parallel
waitGroup := sync.WaitGroup{}
waitGroup.Add(len(results) + len(toDelete))
for _, result := range results {
go func(result graph.OutputFile) {
defer waitGroup.Done()
fs.BeforeFileOpen()
defer fs.AfterFileClose()
if oldHash, ok := oldHashes[result.AbsPath]; ok && oldHash == newHashes[result.AbsPath] {
if contents, err := ioutil.ReadFile(result.AbsPath); err == nil && bytes.Equal(contents, result.Contents) {
// Skip writing out files that haven't changed since last time
return
}
}
if err := fs.MkdirAll(realFS, realFS.Dir(result.AbsPath), 0755); err != nil {
log.AddError(nil, logger.Range{}, fmt.Sprintf(
"Failed to create output directory: %s", err.Error()))
} else {
var mode os.FileMode = 0666
if result.IsExecutable {
mode = 0777
}
if err := ioutil.WriteFile(result.AbsPath, result.Contents, mode); err != nil {
log.AddError(nil, logger.Range{}, fmt.Sprintf(
"Failed to write to output file: %s", err.Error()))
}
}
}(result)
}
for _, absPath := range toDelete {
go func(absPath string) {
defer waitGroup.Done()
fs.BeforeFileOpen()
defer fs.AfterFileClose()
os.Remove(absPath)
}(absPath)
}
waitGroup.Wait()
}
timer.End("Write output files")
}
// Only return the mangle cache for a successful build
if log.HasErrors() {
result.MangleCache = nil
}
// Populate the result object with the messages so far
msgs := log.Peek()
result.Errors = convertMessagesToPublic(logger.Error, msgs, args.options.LogPathStyle)
result.Warnings = convertMessagesToPublic(logger.Warning, msgs, args.options.LogPathStyle)
// Run any registered "OnEnd" callbacks now. These always run regardless of
// whether the current build has bee canceled or not. They can check for
// errors by checking the error array in the build result, and canceled
// builds should always have at least one error.
timer.Begin("On-end callbacks")
for _, onEnd := range args.onEndCallbacks {
fromPlugin, thrown := onEnd.fn(&result)
// Report errors and warnings generated by the plugin
for i := range fromPlugin.Errors {
if fromPlugin.Errors[i].PluginName == "" {
fromPlugin.Errors[i].PluginName = onEnd.pluginName
}
}
for i := range fromPlugin.Warnings {
if fromPlugin.Warnings[i].PluginName == "" {
fromPlugin.Warnings[i].PluginName = onEnd.pluginName
}
}
// Report errors thrown by the plugin itself
if thrown != nil {
fromPlugin.Errors = append(fromPlugin.Errors, Message{
PluginName: onEnd.pluginName,
Text: thrown.Error(),
})
}
// Log any errors and warnings generated above
for _, msg := range convertErrorsAndWarningsToInternal(fromPlugin.Errors, fromPlugin.Warnings) {
log.AddMsg(msg)
}
// Add the errors and warnings to the result object
result.Errors = append(result.Errors, fromPlugin.Errors...)
result.Warnings = append(result.Warnings, fromPlugin.Warnings...)
// Stop if an "onEnd" callback failed. This counts as a build failure.
if len(fromPlugin.Errors) > 0 {
break
}
}
timer.End("On-end callbacks")
// Log timing information now that we're all done
timer.Log(log)
// End the log after "OnEnd" callbacks have added any additional errors and/or
// warnings. This may may print any warnings that were deferred up until this
// point, as well as a message with the number of errors and/or warnings
// omitted due to the configured log limit.
log.Done()
// Only write to stdout after the log has been finalized. We want this output
// to show up in the terminal after the message that was printed above.
if toWriteToStdout != nil {
os.Stdout.Write(toWriteToStdout)
}
return rebuildState{
result: result,
options: args.options,
watchData: watchData,
}, newHashes
}
////////////////////////////////////////////////////////////////////////////////
// Transform API
func transformImpl(input string, transformOpts TransformOptions) TransformResult {
log := logger.NewStderrLog(logger.OutputOptions{
IncludeSource: true,
MessageLimit: transformOpts.LogLimit,
Color: validateColor(transformOpts.Color),
LogLevel: validateLogLevel(transformOpts.LogLevel),
PathStyle: extractPathStyle(transformOpts.AbsPaths, LogAbsPath),
Overrides: validateLogOverrides(transformOpts.LogOverride),
})
caches := cache.MakeCacheSet()
// Apply default values
if transformOpts.Sourcefile == "" {
transformOpts.Sourcefile = "<stdin>"
}
if transformOpts.Loader == LoaderNone {
transformOpts.Loader = LoaderJS
}
// Convert and validate the transformOpts
jsFeatures, cssFeatures, cssPrefixData, targetEnv := validateFeatures(log, transformOpts.Target, transformOpts.Engines)
jsOverrides, jsMask, cssOverrides, cssMask := validateSupported(log, transformOpts.Supported)
platform := validatePlatform(transformOpts.Platform)
defines, injectedDefines := validateDefines(log, transformOpts.Define, transformOpts.Pure, platform, false /* isBuildAPI */, false /* minify */, transformOpts.Drop)
mangleCache := cloneMangleCache(log, transformOpts.MangleCache)
options := config.Options{
CSSPrefixData: cssPrefixData,
UnsupportedJSFeatures: jsFeatures.ApplyOverrides(jsOverrides, jsMask),
UnsupportedCSSFeatures: cssFeatures.ApplyOverrides(cssOverrides, cssMask),
UnsupportedJSFeatureOverrides: jsOverrides,
UnsupportedJSFeatureOverridesMask: jsMask,
UnsupportedCSSFeatureOverrides: cssOverrides,
UnsupportedCSSFeatureOverridesMask: cssMask,
OriginalTargetEnv: targetEnv,
TSConfigRaw: transformOpts.TsconfigRaw,
JSX: config.JSXOptions{
Preserve: transformOpts.JSX == JSXPreserve,
AutomaticRuntime: transformOpts.JSX == JSXAutomatic,
Factory: validateJSXExpr(log, transformOpts.JSXFactory, "factory"),
Fragment: validateJSXExpr(log, transformOpts.JSXFragment, "fragment"),
Development: transformOpts.JSXDev,
ImportSource: transformOpts.JSXImportSource,
SideEffects: transformOpts.JSXSideEffects,
},
Defines: defines,
InjectedDefines: injectedDefines,
Platform: platform,
SourceMap: validateSourceMap(transformOpts.Sourcemap),
LegalComments: validateLegalComments(transformOpts.LegalComments, false /* bundle */),
SourceRoot: transformOpts.SourceRoot,
ExcludeSourcesContent: transformOpts.SourcesContent == SourcesContentExclude,
OutputFormat: validateFormat(transformOpts.Format),
GlobalName: validateGlobalName(log, transformOpts.GlobalName, "(global name)"),
MinifySyntax: transformOpts.MinifySyntax,
MinifyWhitespace: transformOpts.MinifyWhitespace,
MinifyIdentifiers: transformOpts.MinifyIdentifiers,
LineLimit: transformOpts.LineLimit,
MangleProps: validateRegex(log, "mangle props", transformOpts.MangleProps),
ReserveProps: validateRegex(log, "reserve props", transformOpts.ReserveProps),
MangleQuoted: transformOpts.MangleQuoted == MangleQuotedTrue,
DropLabels: append([]string{}, transformOpts.DropLabels...),
DropDebugger: (transformOpts.Drop & DropDebugger) != 0,
ASCIIOnly: validateASCIIOnly(transformOpts.Charset),
IgnoreDCEAnnotations: transformOpts.IgnoreAnnotations,
TreeShaking: validateTreeShaking(transformOpts.TreeShaking, false /* bundle */, transformOpts.Format),
AbsOutputFile: transformOpts.Sourcefile + "-out",
KeepNames: transformOpts.KeepNames,
CodePathStyle: extractPathStyle(transformOpts.AbsPaths, CodeAbsPath),
LogPathStyle: extractPathStyle(transformOpts.AbsPaths, LogAbsPath),
MetafilePathStyle: extractPathStyle(transformOpts.AbsPaths, MetafileAbsPath),
Stdin: &config.StdinInfo{
Loader: validateLoader(transformOpts.Loader),
Contents: input,
SourceFile: transformOpts.Sourcefile,
},
}
validateKeepNames(log, &options)
if options.Stdin.Loader.IsCSS() {
options.CSSBanner = transformOpts.Banner
options.CSSFooter = transformOpts.Footer
} else {
options.JSBanner = transformOpts.Banner
options.JSFooter = transformOpts.Footer
}
if options.SourceMap == config.SourceMapLinkedWithComment {
// Linked source maps don't make sense because there's no output file name
log.AddError(nil, logger.Range{}, "Cannot transform with linked source maps")
}
if options.SourceMap != config.SourceMapNone && options.Stdin.SourceFile == "" {
log.AddError(nil, logger.Range{},
"Must use \"sourcefile\" with \"sourcemap\" to set the original file name")
}
if logger.API == logger.CLIAPI {
if options.LegalComments.HasExternalFile() {
log.AddError(nil, logger.Range{}, "Cannot transform with linked or external legal comments")
}
} else if options.LegalComments == config.LegalCommentsLinkedWithComment {
log.AddError(nil, logger.Range{}, "Cannot transform with linked legal comments")
}
// Set the output mode using other settings
if options.OutputFormat != config.FormatPreserve {
options.Mode = config.ModeConvertFormat
}
var results []graph.OutputFile
// Stop now if there were errors
if !log.HasErrors() {
var timer *helpers.Timer
if api_helpers.UseTimer {
timer = &helpers.Timer{}
}
// Scan over the bundle
mockFS := fs.MockFS(make(map[string]string), fs.MockUnix, "/")
bundle := bundler.ScanBundle(config.TransformCall, log, mockFS, caches, nil, options, timer)
// Stop now if there were errors
if !log.HasErrors() {
// Compile the bundle
results, _ = bundle.Compile(log, timer, mangleCache, linker.Link)
}
timer.Log(log)
}
// Return the results
var code []byte
var sourceMap []byte
var legalComments []byte
var shortestAbsPath string
for _, result := range results {
if shortestAbsPath == "" || len(result.AbsPath) < len(shortestAbsPath) {
shortestAbsPath = result.AbsPath
}
}
// Unpack the JavaScript file, the source map file, and the legal comments file
for _, result := range results {
switch result.AbsPath {
case shortestAbsPath:
code = result.Contents
case shortestAbsPath + ".map":
sourceMap = result.Contents
case shortestAbsPath + ".LEGAL.txt":
legalComments = result.Contents
}
}
// Only return the mangle cache for a successful build
if log.HasErrors() {
mangleCache = nil
}
msgs := log.Done()
return TransformResult{
Errors: convertMessagesToPublic(logger.Error, msgs, options.LogPathStyle),
Warnings: convertMessagesToPublic(logger.Warning, msgs, options.LogPathStyle),
Code: code,
Map: sourceMap,
LegalComments: legalComments,
MangleCache: mangleCache,
}
}
////////////////////////////////////////////////////////////////////////////////
// Plugin API
type pluginImpl struct {
log logger.Log
fs fs.FS
plugin config.Plugin
}
func (impl *pluginImpl) onStart(callback func() (OnStartResult, error)) {
impl.plugin.OnStart = append(impl.plugin.OnStart, config.OnStart{
Name: impl.plugin.Name,
Callback: func() (result config.OnStartResult) {
response, err := callback()
if err != nil {
result.ThrownError = err
return
}
// Convert log messages
result.Msgs = convertErrorsAndWarningsToInternal(response.Errors, response.Warnings)
return
},
})
}
func importKindToResolveKind(kind ast.ImportKind) ResolveKind {
switch kind {
case ast.ImportEntryPoint:
return ResolveEntryPoint
case ast.ImportStmt:
return ResolveJSImportStatement
case ast.ImportRequire:
return ResolveJSRequireCall
case ast.ImportDynamic:
return ResolveJSDynamicImport
case ast.ImportRequireResolve:
return ResolveJSRequireResolve
case ast.ImportAt:
return ResolveCSSImportRule
case ast.ImportComposesFrom:
return ResolveCSSComposesFrom
case ast.ImportURL:
return ResolveCSSURLToken
default:
panic("Internal error")
}
}
func resolveKindToImportKind(kind ResolveKind) ast.ImportKind {
switch kind {
case ResolveEntryPoint:
return ast.ImportEntryPoint
case ResolveJSImportStatement:
return ast.ImportStmt
case ResolveJSRequireCall:
return ast.ImportRequire
case ResolveJSDynamicImport:
return ast.ImportDynamic
case ResolveJSRequireResolve:
return ast.ImportRequireResolve
case ResolveCSSImportRule:
return ast.ImportAt
case ResolveCSSComposesFrom:
return ast.ImportComposesFrom
case ResolveCSSURLToken:
return ast.ImportURL
default:
panic("Internal error")
}
}
func (impl *pluginImpl) onResolve(options OnResolveOptions, callback func(OnResolveArgs) (OnResolveResult, error)) {
filter, err := config.CompileFilterForPlugin(impl.plugin.Name, "OnResolve", options.Filter)
if filter == nil {
impl.log.AddError(nil, logger.Range{}, err.Error())
return
}
impl.plugin.OnResolve = append(impl.plugin.OnResolve, config.OnResolve{
Name: impl.plugin.Name,
Filter: filter,
Namespace: options.Namespace,
Callback: func(args config.OnResolveArgs) (result config.OnResolveResult) {
response, err := callback(OnResolveArgs{
Path: args.Path,
Importer: args.Importer.Text,
Namespace: args.Importer.Namespace,
ResolveDir: args.ResolveDir,
Kind: importKindToResolveKind(args.Kind),
PluginData: args.PluginData,
With: args.With.DecodeIntoMap(),
})
result.PluginName = response.PluginName
result.AbsWatchFiles = impl.validatePathsArray(response.WatchFiles, "watch file")
result.AbsWatchDirs = impl.validatePathsArray(response.WatchDirs, "watch directory")
// Restrict the suffix to start with "?" or "#" for now to match esbuild's behavior
if err == nil && response.Suffix != "" && response.Suffix[0] != '?' && response.Suffix[0] != '#' {
err = fmt.Errorf("Invalid path suffix %q returned from plugin (must start with \"?\" or \"#\")", response.Suffix)
}
if err != nil {
result.ThrownError = err
return
}
result.Path = logger.Path{
Text: response.Path,
Namespace: response.Namespace,
IgnoredSuffix: response.Suffix,
}
result.External = response.External
result.IsSideEffectFree = response.SideEffects == SideEffectsFalse
result.PluginData = response.PluginData
// Convert log messages
result.Msgs = convertErrorsAndWarningsToInternal(response.Errors, response.Warnings)
// Warn if the plugin returned things without resolving the path
if response.Path == "" && !response.External {
var what string
if response.Namespace != "" {
what = "namespace"
} else if response.Suffix != "" {
what = "suffix"
} else if response.PluginData != nil {
what = "pluginData"
} else if response.WatchFiles != nil {
what = "watchFiles"
} else if response.WatchDirs != nil {
what = "watchDirs"
}
if what != "" {
path := "path"
if logger.API == logger.GoAPI {
what = strings.Title(what)
path = strings.Title(path)
}
result.Msgs = append(result.Msgs, logger.Msg{
Kind: logger.Warning,
Data: logger.MsgData{Text: fmt.Sprintf("Returning %q doesn't do anything when %q is empty", what, path)},
})
}
}
return
},
})
}
func (impl *pluginImpl) onLoad(options OnLoadOptions, callback func(OnLoadArgs) (OnLoadResult, error)) {
filter, err := config.CompileFilterForPlugin(impl.plugin.Name, "OnLoad", options.Filter)
if filter == nil {
impl.log.AddError(nil, logger.Range{}, err.Error())
return
}
impl.plugin.OnLoad = append(impl.plugin.OnLoad, config.OnLoad{
Filter: filter,
Namespace: options.Namespace,
Callback: func(args config.OnLoadArgs) (result config.OnLoadResult) {
response, err := callback(OnLoadArgs{
Path: args.Path.Text,
Namespace: args.Path.Namespace,
PluginData: args.PluginData,
Suffix: args.Path.IgnoredSuffix,
With: args.Path.ImportAttributes.DecodeIntoMap(),
})
result.PluginName = response.PluginName
result.AbsWatchFiles = impl.validatePathsArray(response.WatchFiles, "watch file")
result.AbsWatchDirs = impl.validatePathsArray(response.WatchDirs, "watch directory")
if err != nil {
result.ThrownError = err
return
}
result.Contents = response.Contents
result.Loader = validateLoader(response.Loader)
result.PluginData = response.PluginData
pathKind := fmt.Sprintf("resolve directory path for plugin %q", impl.plugin.Name)
if absPath := validatePath(impl.log, impl.fs, response.ResolveDir, pathKind); absPath != "" {
result.AbsResolveDir = absPath
}
// Convert log messages
result.Msgs = convertErrorsAndWarningsToInternal(response.Errors, response.Warnings)
return
},
})
}
func (impl *pluginImpl) validatePathsArray(pathsIn []string, name string) (pathsOut []string) {
if len(pathsIn) > 0 {
pathKind := fmt.Sprintf("%s path for plugin %q", name, impl.plugin.Name)
for _, relPath := range pathsIn {
if absPath := validatePath(impl.log, impl.fs, relPath, pathKind); absPath != "" {
pathsOut = append(pathsOut, absPath)
}
}
}
return
}
func loadPlugins(initialOptions *BuildOptions, fs fs.FS, log logger.Log, caches *cache.CacheSet) (
onEndCallbacks []onEndCallback,
onDisposeCallbacks []func(),
finalizeBuildOptions func(*config.Options),
) {
// Clone the plugin array to guard against mutation during iteration
clone := append(make([]Plugin, 0, len(initialOptions.Plugins)), initialOptions.Plugins...)
var optionsForResolve *config.Options
var plugins []config.Plugin
// This is called after the build options have been validated
finalizeBuildOptions = func(options *config.Options) {
options.Plugins = plugins
optionsForResolve = options
}
for i, item := range clone {
if item.Name == "" {
log.AddError(nil, logger.Range{}, fmt.Sprintf("Plugin at index %d is missing a name", i))
continue
}
impl := &pluginImpl{
fs: fs,
log: log,
plugin: config.Plugin{Name: item.Name},
}
resolve := func(path string, options ResolveOptions) (result ResolveResult) {
// If options are missing, then this is being called before plugin setup
// has finished. That isn't allowed because plugin setup is allowed to
// change the initial options object, which can affect path resolution.
if optionsForResolve == nil {
return ResolveResult{Errors: []Message{{Text: "Cannot call \"resolve\" before plugin setup has completed"}}}
}
if options.Kind == ResolveNone {
return ResolveResult{Errors: []Message{{Text: "Must specify \"kind\" when calling \"resolve\""}}}
}
// Make a new resolver so it has its own log
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, validateLogOverrides(initialOptions.LogOverride))
optionsClone := *optionsForResolve
resolver := resolver.NewResolver(config.BuildCall, fs, log, caches, &optionsClone)
// Make sure the resolve directory is an absolute path, which can fail
absResolveDir := validatePath(log, fs, options.ResolveDir, "resolve directory")
if log.HasErrors() {
msgs := log.Done()
result.Errors = convertMessagesToPublic(logger.Error, msgs, optionsClone.LogPathStyle)
result.Warnings = convertMessagesToPublic(logger.Warning, msgs, optionsClone.LogPathStyle)
return
}
// Run path resolution
kind := resolveKindToImportKind(options.Kind)
resolveResult, _, _ := bundler.RunOnResolvePlugins(
plugins,
resolver,
log,
fs,
&caches.FSCache,
nil, // importSource
logger.Range{}, // importPathRange
logger.Path{Text: options.Importer, Namespace: options.Namespace},
path,
logger.EncodeImportAttributes(options.With),
kind,
absResolveDir,
options.PluginData,
optionsClone.LogPathStyle,
)
msgs := log.Done()
// Populate the result
result.Errors = convertMessagesToPublic(logger.Error, msgs, optionsClone.LogPathStyle)
result.Warnings = convertMessagesToPublic(logger.Warning, msgs, optionsClone.LogPathStyle)
if resolveResult != nil {
result.Path = resolveResult.PathPair.Primary.Text
result.External = resolveResult.PathPair.IsExternal
result.SideEffects = resolveResult.PrimarySideEffectsData == nil
result.Namespace = resolveResult.PathPair.Primary.Namespace
result.Suffix = resolveResult.PathPair.Primary.IgnoredSuffix
result.PluginData = resolveResult.PluginData
} else if len(result.Errors) == 0 {
// Always fail with at least one error
pluginName := item.Name
if options.PluginName != "" {
pluginName = options.PluginName
}
text, _, notes := bundler.ResolveFailureErrorTextSuggestionNotes(
resolver, path, kind, pluginName, fs, absResolveDir, optionsForResolve.Platform,
logger.PrettyPaths{}, "", optionsClone.LogPathStyle)
result.Errors = append(result.Errors, convertMessagesToPublic(logger.Error, []logger.Msg{{
Data: logger.MsgData{Text: text},
Notes: notes,
}}, optionsClone.LogPathStyle)...)
}
return
}
onEnd := func(fn func(*BuildResult) (OnEndResult, error)) {
onEndCallbacks = append(onEndCallbacks, onEndCallback{
pluginName: item.Name,
fn: fn,
})
}
onDispose := func(fn func()) {
onDisposeCallbacks = append(onDisposeCallbacks, fn)
}
item.Setup(PluginBuild{
InitialOptions: initialOptions,
Resolve: resolve,
OnStart: impl.onStart,
OnEnd: onEnd,
OnDispose: onDispose,
OnResolve: impl.onResolve,
OnLoad: impl.onLoad,
})
plugins = append(plugins, impl.plugin)
}
return
}
////////////////////////////////////////////////////////////////////////////////
// FormatMessages API
func formatMsgsImpl(msgs []Message, opts FormatMessagesOptions) []string {
kind := logger.Error
if opts.Kind == WarningMessage {
kind = logger.Warning
}
logMsgs := convertMessagesToInternal(nil, kind, msgs)
strings := make([]string, len(logMsgs))
for i, msg := range logMsgs {
strings[i] = msg.String(
logger.OutputOptions{
IncludeSource: true,
},
logger.TerminalInfo{
UseColorEscapes: opts.Color,
Width: opts.TerminalWidth,
},
)
}
return strings
}
////////////////////////////////////////////////////////////////////////////////
// AnalyzeMetafile API
type metafileEntry struct {
name string
entryPoint string
entries []metafileEntry
size int
}
// This type is just so we can use Go's native sort function
type metafileArray []metafileEntry
func (a metafileArray) Len() int { return len(a) }
func (a metafileArray) Swap(i int, j int) { a[i], a[j] = a[j], a[i] }
func (a metafileArray) Less(i int, j int) bool {
ai := a[i]
aj := a[j]
return ai.size > aj.size || (ai.size == aj.size && ai.name < aj.name)
}
func getObjectProperty(expr js_ast.Expr, key string) js_ast.Expr {
if obj, ok := expr.Data.(*js_ast.EObject); ok {
for _, prop := range obj.Properties {
if helpers.UTF16EqualsString(prop.Key.Data.(*js_ast.EString).Value, key) {
return prop.ValueOrNil
}
}
}
return js_ast.Expr{}
}
func getObjectPropertyNumber(expr js_ast.Expr, key string) *js_ast.ENumber {
value, _ := getObjectProperty(expr, key).Data.(*js_ast.ENumber)
return value
}
func getObjectPropertyString(expr js_ast.Expr, key string) *js_ast.EString {
value, _ := getObjectProperty(expr, key).Data.(*js_ast.EString)
return value
}
func getObjectPropertyObject(expr js_ast.Expr, key string) *js_ast.EObject {
value, _ := getObjectProperty(expr, key).Data.(*js_ast.EObject)
return value
}
func getObjectPropertyArray(expr js_ast.Expr, key string) *js_ast.EArray {
value, _ := getObjectProperty(expr, key).Data.(*js_ast.EArray)
return value
}
func analyzeMetafileImpl(metafile string, opts AnalyzeMetafileOptions) string {
log := logger.NewDeferLog(logger.DeferLogNoVerboseOrDebug, nil)
source := logger.Source{Contents: metafile}
if result, ok := js_parser.ParseJSON(log, source, js_parser.JSONOptions{}); ok {
if outputs := getObjectPropertyObject(result, "outputs"); outputs != nil {
var entries metafileArray
var entryPoints []string
// Scan over the "outputs" object
for _, output := range outputs.Properties {
if key := helpers.UTF16ToString(output.Key.Data.(*js_ast.EString).Value); !strings.HasSuffix(key, ".map") {
entryPointPath := ""
if entryPoint := getObjectPropertyString(output.ValueOrNil, "entryPoint"); entryPoint != nil {
entryPointPath = helpers.UTF16ToString(entryPoint.Value)
entryPoints = append(entryPoints, entryPointPath)
}
if bytes := getObjectPropertyNumber(output.ValueOrNil, "bytes"); bytes != nil {
if inputs := getObjectPropertyObject(output.ValueOrNil, "inputs"); inputs != nil {
var children metafileArray
for _, input := range inputs.Properties {
if bytesInOutput := getObjectPropertyNumber(input.ValueOrNil, "bytesInOutput"); bytesInOutput != nil && bytesInOutput.Value > 0 {
children = append(children, metafileEntry{
name: helpers.UTF16ToString(input.Key.Data.(*js_ast.EString).Value),
size: int(bytesInOutput.Value),
})
}
}
sort.Sort(children)
entries = append(entries, metafileEntry{
name: key,
size: int(bytes.Value),
entries: children,
entryPoint: entryPointPath,
})
}
}
}
}
sort.Sort(entries)
type importData struct {
imports []string
}
type graphData struct {
parent string
depth uint32
}
importsForPath := make(map[string]importData)
// Scan over the "inputs" object
if inputs := getObjectPropertyObject(result, "inputs"); inputs != nil {
for _, prop := range inputs.Properties {
if imports := getObjectPropertyArray(prop.ValueOrNil, "imports"); imports != nil {
var data importData
for _, item := range imports.Items {
if path := getObjectPropertyString(item, "path"); path != nil {
data.imports = append(data.imports, helpers.UTF16ToString(path.Value))
}
}
importsForPath[helpers.UTF16ToString(prop.Key.Data.(*js_ast.EString).Value)] = data
}
}
}
// Returns a graph with links pointing from imports to importers
graphForEntryPoints := func(worklist []string) map[string]graphData {
if !opts.Verbose {
return nil
}
graph := make(map[string]graphData)
for _, entryPoint := range worklist {
graph[entryPoint] = graphData{}
}
for len(worklist) > 0 {
top := worklist[len(worklist)-1]
worklist = worklist[:len(worklist)-1]
childDepth := graph[top].depth + 1
for _, importPath := range importsForPath[top].imports {
imported, ok := graph[importPath]
if !ok {
imported.depth = math.MaxUint32
}
if imported.depth > childDepth {
imported.depth = childDepth
imported.parent = top
graph[importPath] = imported
worklist = append(worklist, importPath)
}
}
}
return graph
}
graphForAllEntryPoints := graphForEntryPoints(entryPoints)
type tableEntry struct {
first string
second string
third string
firstLen int
secondLen int
thirdLen int
isTopLevel bool
}
var table []tableEntry
var colors logger.Colors
if opts.Color {
colors = logger.TerminalColors
}
// Build up the table with an entry for each output file (other than ".map" files)
for _, entry := range entries {
second := prettyPrintByteCount(entry.size)
third := "100.0%"
table = append(table, tableEntry{
first: entry.name,
firstLen: utf8.RuneCountInString(entry.name),
second: second,
secondLen: len(second),
third: third,
thirdLen: len(third),
isTopLevel: true,
})
graph := graphForAllEntryPoints
if entry.entryPoint != "" {
// If there are multiple entry points and this output file is from an
// entry point, prefer import paths for this entry point. This is less
// confusing than showing import paths for another entry point.
graph = graphForEntryPoints([]string{entry.entryPoint})
}
// Add a sub-entry for each input file in this output file
for j, child := range entry.entries {
indent := " ├ "
if j+1 == len(entry.entries) {
indent = " └ "
}
percent := 100.0 * float64(child.size) / float64(entry.size)
first := indent + child.name
second := prettyPrintByteCount(child.size)
third := fmt.Sprintf("%.1f%%", percent)
table = append(table, tableEntry{
first: first,
firstLen: utf8.RuneCountInString(first),
second: second,
secondLen: len(second),
third: third,
thirdLen: len(third),
})
// If we're in verbose mode, also print the import chain from this file
// up toward an entry point to show why this file is in the bundle
if opts.Verbose {
indent = " │ "
if j+1 == len(entry.entries) {
indent = " "
}
data := graph[child.name]
depth := 0
for data.depth != 0 {
table = append(table, tableEntry{
first: fmt.Sprintf("%s%s%s └ %s%s", indent, colors.Dim, strings.Repeat(" ", depth), data.parent, colors.Reset),
})
data = graph[data.parent]
depth += 3
}
}
}
}
maxFirstLen := 0
maxSecondLen := 0
maxThirdLen := 0
// Calculate column widths
for _, entry := range table {
if maxFirstLen < entry.firstLen {
maxFirstLen = entry.firstLen
}
if maxSecondLen < entry.secondLen {
maxSecondLen = entry.secondLen
}
if maxThirdLen < entry.thirdLen {
maxThirdLen = entry.thirdLen
}
}
sb := strings.Builder{}
// Render the columns now that we know the widths
for _, entry := range table {
prefix := "\n"
color := colors.Bold
if !entry.isTopLevel {
prefix = ""
color = ""
}
// Import paths don't have second and third columns
if entry.second == "" && entry.third == "" {
sb.WriteString(fmt.Sprintf("%s %s\n",
prefix,
entry.first,
))
continue
}
second := entry.second
secondTrimmed := strings.TrimRight(second, " ")
lineChar := " "
extraSpace := 0
if opts.Verbose {
lineChar = "─"
extraSpace = 1
}
sb.WriteString(fmt.Sprintf("%s %s%s%s %s%s%s %s%s%s %s%s%s %s%s%s\n",
prefix,
color,
entry.first,
colors.Reset,
colors.Dim,
strings.Repeat(lineChar, extraSpace+maxFirstLen-entry.firstLen+maxSecondLen-entry.secondLen),
colors.Reset,
color,
secondTrimmed,
colors.Reset,
colors.Dim,
strings.Repeat(lineChar, extraSpace+maxThirdLen-entry.thirdLen+len(second)-len(secondTrimmed)),
colors.Reset,
color,
entry.third,
colors.Reset,
))
}
return sb.String()
}
}
return ""
}
func stripDirPrefix(path string, prefix string, allowedSlashes string) (string, bool) {
if strings.HasPrefix(path, prefix) {
pathLen := len(path)
prefixLen := len(prefix)
// Just return the path if there is no prefix
if prefixLen == 0 {
return path, true
}
// Return the empty string if the path equals the prefix
if pathLen == prefixLen {
return "", true
}
if strings.IndexByte(allowedSlashes, prefix[prefixLen-1]) >= 0 {
// Return the suffix if the prefix ends in a slash. Examples:
//
// stripDirPrefix(`/foo`, `/`, `/`) => `foo`
// stripDirPrefix(`C:\foo`, `C:\`, `\/`) => `foo`
//
return path[prefixLen:], true
} else if strings.IndexByte(allowedSlashes, path[prefixLen]) >= 0 {
// Return the suffix if there's a slash after the prefix. Examples:
//
// stripDirPrefix(`/foo/bar`, `/foo`, `/`) => `bar`
// stripDirPrefix(`C:\foo\bar`, `C:\foo`, `\/`) => `bar`
//
return path[prefixLen+1:], true
}
}
return "", false
}
|