Skip to content

Commit c144483

Browse files
12wrigjacopybara-github
authored andcommitted
Track licenses needed as code is being printed in order to avoid printing the license of code that ends up being entirely removed from the final bundle.
PiperOrigin-RevId: 484050870
1 parent aa3f740 commit c144483

File tree

12 files changed

+671
-244
lines changed

12 files changed

+671
-244
lines changed

src/com/google/javascript/jscomp/AbstractCommandLineRunner.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
import com.google.gson.reflect.TypeToken;
4343
import com.google.gson.stream.JsonReader;
4444
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;
4549
import com.google.javascript.jscomp.CompilerOptions.JsonStreamMode;
4650
import com.google.javascript.jscomp.CompilerOptions.OutputJs;
4751
import com.google.javascript.jscomp.CompilerOptions.TweakProcessing;
@@ -1036,7 +1040,8 @@ String getModuleOutputFileName(JSChunk m) {
10361040

10371041
@VisibleForTesting
10381042
@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 {
10401045
if (parsedModuleWrappers == null) {
10411046
parsedModuleWrappers =
10421047
parseModuleWrappers(
@@ -1050,6 +1055,7 @@ void writeModuleOutput(String fileName, Appendable out, JSChunk m) throws IOExce
10501055
writeOutput(
10511056
out,
10521057
compiler,
1058+
lt,
10531059
m,
10541060
parsedModuleWrappers.get(m.getName()).replace("%basename%", baseName),
10551061
"%s",
@@ -1067,6 +1073,7 @@ void writeModuleOutput(String fileName, Appendable out, JSChunk m) throws IOExce
10671073
void writeOutput(
10681074
Appendable out,
10691075
Compiler compiler,
1076+
LicenseTracker licenseTracker,
10701077
@Nullable JSChunk module,
10711078
String wrapper,
10721079
String codePlaceholder,
@@ -1079,7 +1086,7 @@ void writeOutput(
10791086
}
10801087
checkState(compiler.getOptions().outputJs == OutputJs.NORMAL);
10811088

1082-
String code = module == null ? compiler.toSource() : compiler.toSource(module);
1089+
String code = module == null ? compiler.toSource() : compiler.toSource(licenseTracker, module);
10831090
writeOutput(out, compiler, code, wrapper, codePlaceholder, escaper, filename);
10841091
}
10851092

@@ -1654,7 +1661,7 @@ int processResults(Result result, List<JSChunk> modules, B options) throws IOExc
16541661
outputSourceMap(options, config.jsOutputFile);
16551662
}
16561663
} else {
1657-
DiagnosticType error = outputModuleBinaryAndSourceMaps(compiler.getModules(), options);
1664+
DiagnosticType error = outputModuleBinaryAndSourceMaps(compiler.getModuleGraph(), options);
16581665
if (error != null) {
16591666
compiler.report(JSError.make(error));
16601667
return 1;
@@ -1719,6 +1726,9 @@ void outputSingleBinary(B options) throws IOException {
17191726
writeOutput(
17201727
jsOutput,
17211728
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),
17221732
(JSChunk) null,
17231733
config.outputWrapper,
17241734
marker,
@@ -1736,6 +1746,9 @@ JsonFileSpec createJsonFile(B options, String outputMarker, Function<String, Str
17361746
writeOutput(
17371747
jsOutput,
17381748
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),
17391752
(JSChunk) null,
17401753
config.outputWrapper,
17411754
outputMarker,
@@ -1768,7 +1781,8 @@ void outputJsonStream() throws IOException {
17681781

17691782
@GwtIncompatible("Unnecessary")
17701783
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();
17721786
parsedModuleWrappers = parseModuleWrappers(config.moduleWrapper, modules);
17731787
if (!isOutputInJson()) {
17741788
// make sure the method generates all dirs up to the latest /
@@ -1790,6 +1804,7 @@ void outputJsonStream() throws IOException {
17901804
return INVALID_MODULE_SOURCEMAP_PATTERN;
17911805
}
17921806

1807+
ChunkGraphAwareLicenseTracker mlicenseTracker = new ChunkGraphAwareLicenseTracker(compiler);
17931808
for (JSChunk m : modules) {
17941809
if (m.getName().equals(JSChunk.WEAK_CHUNK_NAME)) {
17951810
// Skip the weak module, which is always empty.
@@ -1808,7 +1823,8 @@ void outputJsonStream() throws IOException {
18081823
if (options.shouldGatherSourceMapInfo()) {
18091824
compiler.resetAndIntitializeSourceMap();
18101825
}
1811-
writeModuleOutput(moduleFilename, writer, m);
1826+
mlicenseTracker.setCurrentChunkContext(m);
1827+
writeModuleOutput(moduleFilename, writer, mlicenseTracker, m);
18121828
if (options.shouldGatherSourceMapInfo()) {
18131829
compiler.getSourceMap().appendTo(mapFileOut, moduleFilename);
18141830
}
@@ -1834,7 +1850,7 @@ private JsonFileSpec createJsonFileFromModule(JSChunk module) throws IOException
18341850

18351851
String filename = getModuleOutputFileName(module);
18361852
StringBuilder output = new StringBuilder();
1837-
writeModuleOutput(filename, output, module);
1853+
writeModuleOutput(filename, output, new ScriptNodeLicensesOnlyTracker(compiler), module);
18381854

18391855
JsonFileSpec jsonFile = new JsonFileSpec(output.toString(), filename);
18401856

src/com/google/javascript/jscomp/CodeConsumer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ void endSourceMapping(Node node) {
5555
}
5656

5757
/**
58-
* Provides a means of interrupting the CodeGenerator. Derived classes should
59-
* return false to stop further processing.
58+
* Indicates to the CodeConsumer that this Node might carry licensing information, and allows the
59+
* code consumer to manage licenses as it sees fit.
60+
*/
61+
void trackLicenses(Node node) {}
62+
63+
/**
64+
* Provides a means of interrupting the CodeGenerator. Derived classes should return false to stop
65+
* further processing.
6066
*/
6167
boolean continueProcessing() {
6268
return true;

src/com/google/javascript/jscomp/CodeGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ protected void add(Node node, Context context, boolean printComments) {
204204
if (printComments) {
205205
printLeadingCommentsInOrder(node);
206206
}
207+
cc.trackLicenses(node);
207208

208209
Token type = node.getToken();
209210
String opstr = NodeUtil.opToStr(type);

0 commit comments

Comments
 (0)