Skip to content

Commit

Permalink
Build bundle source as string, with relative path from bundle file so…
Browse files Browse the repository at this point in the history
… sourcemaps work. Sort of.
  • Loading branch information
niloc132 committed Oct 12, 2022
1 parent 2ceb662 commit 6aa0173
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
23 changes: 13 additions & 10 deletions src/com/google/javascript/jscomp/AbstractCommandLineRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ protected CommandLineConfig getCommandLineConfig() {
*/
@GwtIncompatible("Unnecessary")
protected abstract void prepForBundleAndAppendTo(
Appendable out, CompilerInput input, String content) throws IOException;
Appendable out, CompilerInput input, String content, String outputPath) throws IOException;

/** Writes whatever runtime libraries are needed to bundle. */
@GwtIncompatible("Unnecessary")
Expand Down Expand Up @@ -1646,6 +1646,7 @@ int processResults(Result result, List<JSChunk> modules, B options) throws IOExc
outputManifest();
outputBundle();
outputModuleGraphJson();
outputSourceMap(options, config.jsOutputFile);
return 0;
} else if (options.outputJs != OutputJs.NONE && result.success) {
outputModuleGraphJson();
Expand Down Expand Up @@ -2274,29 +2275,31 @@ private void outputManifestOrBundle(List<String> outputFiles, boolean isManifest
// Generate per-module manifests or bundles.
Iterable<JSChunk> modules = compiler.getModuleGraph().getAllChunks();
for (JSChunk module : modules) {
try (Writer out = fileNameToOutputWriter2(expandCommandLinePath(output, module))) {
String outputPath = expandCommandLinePath(output, module);
try (Writer out = fileNameToOutputWriter2(outputPath)) {
if (isManifest) {
printManifestTo(module, out);
} else {
printBundleTo(module, out);
printBundleTo(module, out, outputPath);
}
}
}
} else {
// Generate a single file manifest or bundle.
try (Writer out = fileNameToOutputWriter2(expandCommandLinePath(output, null))) {
String outputPath = expandCommandLinePath(output, null);
try (Writer out = fileNameToOutputWriter2(outputPath)) {
if (config.module.isEmpty()) {
// For a single-module compilation, generate a single headerless manifest or bundle
// containing only the strong files.
JSChunk module = compiler.getModuleGraph().getChunkByName(JSChunk.STRONG_CHUNK_NAME);
if (isManifest) {
printManifestTo(module, out);
} else {
printBundleTo(module, out);
printBundleTo(module, out, outputPath);
}
} else {
// For a multi-module compilation, generate a single manifest file with module headers.
printModuleGraphManifestOrBundleTo(compiler.getModuleGraph(), out, isManifest);
printModuleGraphManifestOrBundleTo(compiler.getModuleGraph(), out, isManifest, outputPath);
}
}
}
Expand All @@ -2323,7 +2326,7 @@ void printModuleGraphJsonTo(Appendable out) throws IOException {
/** Prints a set of modules to the manifest or bundle file. */
@VisibleForTesting
@GwtIncompatible("Unnecessary")
void printModuleGraphManifestOrBundleTo(JSChunkGraph graph, Appendable out, boolean isManifest)
void printModuleGraphManifestOrBundleTo(JSChunkGraph graph, Appendable out, boolean isManifest, String outputPath)
throws IOException {
Joiner commas = Joiner.on(",");
boolean requiresNewline = false;
Expand All @@ -2346,7 +2349,7 @@ void printModuleGraphManifestOrBundleTo(JSChunkGraph graph, Appendable out, bool
"{%s%s}\n", module.getName(), dependencies.isEmpty() ? "" : ":" + dependencies));
printManifestTo(module, out);
} else {
printBundleTo(module, out);
printBundleTo(module, out, outputPath);
}
requiresNewline = true;
}
Expand Down Expand Up @@ -2380,7 +2383,7 @@ void printManifestTo(JSChunk module, Appendable out) throws IOException {
*/
@VisibleForTesting
@GwtIncompatible("Unnecessary")
void printBundleTo(JSChunk module, Appendable out) throws IOException {
void printBundleTo(JSChunk module, Appendable out, String outputPath) throws IOException {
ImmutableList<CompilerInput> inputs = module.getInputs();
// Prebuild ASTs before they're needed in getLoadFlags, for performance and because
// StackOverflowErrors can be hit if not prebuilt.
Expand Down Expand Up @@ -2428,7 +2431,7 @@ void printBundleTo(JSChunk module, Appendable out) throws IOException {
out.append(displayName);
out.append("\n");

prepForBundleAndAppendTo(out, input, code);
prepForBundleAndAppendTo(out, input, code, outputPath);

out.append("\n");
}
Expand Down
15 changes: 13 additions & 2 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2092,9 +2092,20 @@ private ClosureBundler getBundler() {
}

@Override
protected void prepForBundleAndAppendTo(Appendable out, CompilerInput input, String content)
protected void prepForBundleAndAppendTo(Appendable out, CompilerInput input, String content, String outputPath)
throws IOException {
getBundler().withPath(input.getName()).appendTo(out, input, content);
String pathToInput = new File(input.getName()).getCanonicalPath();
String pathToOutput = new File(outputPath).getParentFile().getCanonicalPath();
String relativePath = Paths.get(pathToOutput).relativize(Paths.get(pathToInput)).toString();

ClosureBundler bundler;
if (!relativePath.startsWith("..")) {
bundler = getBundler().useEval(true).withSourceUrl(relativePath);
} else {
bundler = getBundler();
}

bundler.withPath(input.getName()).appendTo(out, input, content);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions test/com/google/javascript/jscomp/CommandLineRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ public void testChainModuleManifest() throws Exception {

StringBuilder builder = new StringBuilder();
lastCommandLineRunner.printModuleGraphManifestOrBundleTo(
lastCompiler.getModuleGraph(), builder, true);
lastCompiler.getModuleGraph(), builder, true, null);
assertThat(builder.toString())
.isEqualTo(
Joiner.on('\n')
Expand Down Expand Up @@ -1813,7 +1813,7 @@ public void testStarModuleManifest() throws Exception {

StringBuilder builder = new StringBuilder();
lastCommandLineRunner.printModuleGraphManifestOrBundleTo(
lastCompiler.getModuleGraph(), builder, true);
lastCompiler.getModuleGraph(), builder, true, null);
assertThat(builder.toString())
.isEqualTo(
Joiner.on('\n')
Expand Down

0 comments on commit 6aa0173

Please sign in to comment.