Skip to content

Commit 9a58a7f

Browse files
rahul-kamatcopybara-github
authored andcommitted
Do not support sourcemappingURLs with a configured directory for TypedAST builds.
Previously, we were serializing the **path** of the sourcemap file on disk as the `sourceMappingURL` in lazyAst. With this change, we will serialize the **name** of the sourcemap file. This only affects TypedAST builds. E.g: We will now serialize `a.js.map` instead of `foo/bar/a.js.map`. PiperOrigin-RevId: 504935372
1 parent 2ab6df6 commit 9a58a7f

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,12 @@ public void addInputSourceMap(String sourceFileName, SourceMapInput inputSourceM
32823282
SourceMapInput sourceMapInput = inputSourceMaps.getOrDefault(sourceFileName, null);
32833283
String sourceMappingURL = sourceMapInput != null ? sourceMapInput.getOriginalPath() : null;
32843284
if (sourceMappingURL != null && !sourceMappingURL.endsWith(".inline.map")) {
3285+
// Set sourceMappingURL as the name of the sourcemap file, not the original location/path of
3286+
// the sourcemap file on disk. E.g. turn "blaze-out/.../a.js.map" into "a.js.map"
3287+
sourceMappingURL =
3288+
sourceMappingURL.contains("/")
3289+
? sourceMappingURL.substring(sourceMappingURL.lastIndexOf("/") + 1)
3290+
: sourceMappingURL;
32853291
return sourceMappingURL;
32863292
}
32873293
return null;

test/com/google/javascript/jscomp/serialization/SerializeAndDeserializeAstTest.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,12 @@ public void testSourceMaps() {
414414
" }",
415415
" return X;",
416416
"}());");
417-
String sourceMapPath = "relativedir/foo.js.map";
418-
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMapPath;
417+
String sourceMappingURL = "foo.js.map";
418+
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMappingURL;
419419
String code = sourceMapTestCode + "\n" + sourceMappingURLComment;
420420

421421
Result result = testAndReturnResult(srcs(code), expected(code));
422-
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo(sourceMapPath);
422+
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo(sourceMappingURL);
423423
}
424424

425425
@Test
@@ -433,8 +433,8 @@ public void testSourceMapsWithoutResolvingSourceMapAnnotations() {
433433
" }",
434434
" return X;",
435435
"}());");
436-
String sourceMapPath = "relativedir/foo.js.map";
437-
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMapPath;
436+
String sourceMappingURL = "foo.js.map";
437+
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMappingURL;
438438
String code = sourceMapTestCode + "\n" + sourceMappingURLComment;
439439

440440
Result result = testAndReturnResult(srcs(code), expected(code));
@@ -453,14 +453,37 @@ public void testSourceMapsWithoutParsingInlineSourceMaps() {
453453
" }",
454454
" return X;",
455455
"}());");
456-
String sourceMapPath = "relativedir/foo.js.map";
457-
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMapPath;
456+
String sourceMappingURL = "foo.js.map";
457+
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMappingURL;
458458
String code = sourceMapTestCode + "\n" + sourceMappingURLComment;
459459

460460
Result result = testAndReturnResult(srcs(code), expected(code));
461461
// Input source map is registered when `parseInlineSourceMaps = false`, but we won't try to
462462
// parse it as a Base64 encoded source map.
463-
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo(sourceMapPath);
463+
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo(sourceMappingURL);
464+
}
465+
466+
@Test
467+
public void testConfiguredDirectorySourceMaps() {
468+
// We do not allow the TypeScript compiler to set "compilerOptions.sourceRoot" (option to
469+
// configure a directory to store
470+
// sourcemaps). Sourcemaps (.js.map) files are placed next to the .js files.
471+
// This means sourcemap URLs should be the name of the sourcemap file, not a path to the
472+
// sourcemap file. If we see a path, we will serialize only the name of the sourcemap file.
473+
String sourceMapTestCode =
474+
lines(
475+
"var X = (function () {",
476+
" function X(input) {",
477+
" this.y = input;",
478+
" }",
479+
" return X;",
480+
"}());");
481+
String sourceMappingURLPath = "directory/foo.js.map";
482+
String sourceMappingURLComment = "//# sourceMappingURL=" + sourceMappingURLPath;
483+
String code = sourceMapTestCode + "\n" + sourceMappingURLComment;
484+
485+
Result result = testAndReturnResult(srcs(code), expected(code));
486+
assertThat(result.compiler.getInputSourceMappingURL("testcode")).isEqualTo("foo.js.map");
464487
}
465488

466489
@Test

test/com/google/javascript/jscomp/serialization/SerializeTypedAstPassTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,14 @@ public void serializesSourceMappingURL() throws InvalidProtocolBufferException {
435435
" return X;",
436436
"}());");
437437

438-
String code = sourceMapTestCode + "\n//# sourceMappingURL=relativedir/foo.js.map";
438+
String code = sourceMapTestCode + "\n//# sourceMappingURL=foo.js.map";
439439

440440
SerializationResult result = compile(code);
441441

442442
LazyAst lazyAst = result.ast.getCodeAstList().get(0);
443443
String sourceMappingURL = lazyAst.getSourceMappingUrl();
444444

445-
assertThat(sourceMappingURL).isEqualTo("relativedir/foo.js.map");
445+
assertThat(sourceMappingURL).isEqualTo("foo.js.map");
446446
}
447447

448448
@Test

0 commit comments

Comments
 (0)