42
42
import com .google .gson .reflect .TypeToken ;
43
43
import com .google .gson .stream .JsonReader ;
44
44
import com .google .gson .stream .JsonWriter ;
45
+ import com .google .javascript .jscomp .CodePrinter .LicenseTracker ;
46
+ import com .google .javascript .jscomp .Compiler .ChunkGraphAwareLicenseTracker ;
47
+ import com .google .javascript .jscomp .Compiler .ScriptNodeLicensesOnlyTracker ;
48
+ import com .google .javascript .jscomp .Compiler .SingleBinaryLicenseTracker ;
45
49
import com .google .javascript .jscomp .CompilerOptions .JsonStreamMode ;
46
50
import com .google .javascript .jscomp .CompilerOptions .OutputJs ;
47
51
import com .google .javascript .jscomp .CompilerOptions .TweakProcessing ;
@@ -1036,7 +1040,8 @@ String getModuleOutputFileName(JSChunk m) {
1036
1040
1037
1041
@ VisibleForTesting
1038
1042
@ GwtIncompatible ("Unnecessary" )
1039
- void writeModuleOutput (String fileName , Appendable out , JSChunk m ) throws IOException {
1043
+ void writeModuleOutput (String fileName , Appendable out , LicenseTracker lt , JSChunk m )
1044
+ throws IOException {
1040
1045
if (parsedModuleWrappers == null ) {
1041
1046
parsedModuleWrappers =
1042
1047
parseModuleWrappers (
@@ -1050,6 +1055,7 @@ void writeModuleOutput(String fileName, Appendable out, JSChunk m) throws IOExce
1050
1055
writeOutput (
1051
1056
out ,
1052
1057
compiler ,
1058
+ lt ,
1053
1059
m ,
1054
1060
parsedModuleWrappers .get (m .getName ()).replace ("%basename%" , baseName ),
1055
1061
"%s" ,
@@ -1067,6 +1073,7 @@ void writeModuleOutput(String fileName, Appendable out, JSChunk m) throws IOExce
1067
1073
void writeOutput (
1068
1074
Appendable out ,
1069
1075
Compiler compiler ,
1076
+ LicenseTracker licenseTracker ,
1070
1077
@ Nullable JSChunk module ,
1071
1078
String wrapper ,
1072
1079
String codePlaceholder ,
@@ -1079,7 +1086,7 @@ void writeOutput(
1079
1086
}
1080
1087
checkState (compiler .getOptions ().outputJs == OutputJs .NORMAL );
1081
1088
1082
- String code = module == null ? compiler .toSource () : compiler .toSource (module );
1089
+ String code = module == null ? compiler .toSource () : compiler .toSource (licenseTracker , module );
1083
1090
writeOutput (out , compiler , code , wrapper , codePlaceholder , escaper , filename );
1084
1091
}
1085
1092
@@ -1654,7 +1661,7 @@ int processResults(Result result, List<JSChunk> modules, B options) throws IOExc
1654
1661
outputSourceMap (options , config .jsOutputFile );
1655
1662
}
1656
1663
} else {
1657
- DiagnosticType error = outputModuleBinaryAndSourceMaps (compiler .getModules (), options );
1664
+ DiagnosticType error = outputModuleBinaryAndSourceMaps (compiler .getModuleGraph (), options );
1658
1665
if (error != null ) {
1659
1666
compiler .report (JSError .make (error ));
1660
1667
return 1 ;
@@ -1719,6 +1726,9 @@ void outputSingleBinary(B options) throws IOException {
1719
1726
writeOutput (
1720
1727
jsOutput ,
1721
1728
compiler ,
1729
+ // So long as the JSChunk arg is null the compiler will write all sources to jsOutput
1730
+ // Use single-binary license tracking to dedupe licenses among all the inputs
1731
+ new SingleBinaryLicenseTracker (compiler ),
1722
1732
(JSChunk ) null ,
1723
1733
config .outputWrapper ,
1724
1734
marker ,
@@ -1736,6 +1746,9 @@ JsonFileSpec createJsonFile(B options, String outputMarker, Function<String, Str
1736
1746
writeOutput (
1737
1747
jsOutput ,
1738
1748
compiler ,
1749
+ // So long as the JSChunk arg is null the compiler will write all sources to jsOutput
1750
+ // Use single-binary license tracking to dedupe licenses among all the inputs
1751
+ new SingleBinaryLicenseTracker (compiler ),
1739
1752
(JSChunk ) null ,
1740
1753
config .outputWrapper ,
1741
1754
outputMarker ,
@@ -1768,7 +1781,8 @@ void outputJsonStream() throws IOException {
1768
1781
1769
1782
@ GwtIncompatible ("Unnecessary" )
1770
1783
private @ Nullable DiagnosticType outputModuleBinaryAndSourceMaps (
1771
- Iterable <JSChunk > modules , B options ) throws IOException {
1784
+ JSChunkGraph moduleGraph , B options ) throws IOException {
1785
+ Iterable <JSChunk > modules = moduleGraph .getAllChunks ();
1772
1786
parsedModuleWrappers = parseModuleWrappers (config .moduleWrapper , modules );
1773
1787
if (!isOutputInJson ()) {
1774
1788
// make sure the method generates all dirs up to the latest /
@@ -1790,6 +1804,7 @@ void outputJsonStream() throws IOException {
1790
1804
return INVALID_MODULE_SOURCEMAP_PATTERN ;
1791
1805
}
1792
1806
1807
+ ChunkGraphAwareLicenseTracker mlicenseTracker = new ChunkGraphAwareLicenseTracker (compiler );
1793
1808
for (JSChunk m : modules ) {
1794
1809
if (m .getName ().equals (JSChunk .WEAK_CHUNK_NAME )) {
1795
1810
// Skip the weak module, which is always empty.
@@ -1808,7 +1823,8 @@ void outputJsonStream() throws IOException {
1808
1823
if (options .shouldGatherSourceMapInfo ()) {
1809
1824
compiler .resetAndIntitializeSourceMap ();
1810
1825
}
1811
- writeModuleOutput (moduleFilename , writer , m );
1826
+ mlicenseTracker .setCurrentChunkContext (m );
1827
+ writeModuleOutput (moduleFilename , writer , mlicenseTracker , m );
1812
1828
if (options .shouldGatherSourceMapInfo ()) {
1813
1829
compiler .getSourceMap ().appendTo (mapFileOut , moduleFilename );
1814
1830
}
@@ -1834,7 +1850,7 @@ private JsonFileSpec createJsonFileFromModule(JSChunk module) throws IOException
1834
1850
1835
1851
String filename = getModuleOutputFileName (module );
1836
1852
StringBuilder output = new StringBuilder ();
1837
- writeModuleOutput (filename , output , module );
1853
+ writeModuleOutput (filename , output , new ScriptNodeLicensesOnlyTracker ( compiler ), module );
1838
1854
1839
1855
JsonFileSpec jsonFile = new JsonFileSpec (output .toString (), filename );
1840
1856
0 commit comments