Skip to content

Commit

Permalink
feat(logs): support external sourcemaps from .map files (#5434)
Browse files Browse the repository at this point in the history
* support reading sourcemaps from `.map` files

* test: add external source-map tests

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
  • Loading branch information
farfromrefug and rigor789 authored Dec 29, 2020
1 parent 7cdaed7 commit de678cf
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/services/log-source-map-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,17 @@ export class LogSourceMapService implements Mobile.ILogSourceMapService {
public async setSourceMapConsumerForFile(filePath: string): Promise<void> {
try {
if (!this.$fs.getFsStats(filePath).isDirectory()) {
const mapFile = filePath + ".map";
let sourceMapRaw;
const source = this.$fs.readText(filePath);
const sourceMapRaw = sourceMapConverter.fromSource(source);
if (this.$fs.exists(mapFile)) {
sourceMapRaw = sourceMapConverter.fromMapFileSource(
source,
path.dirname(filePath)
);
} else {
sourceMapRaw = sourceMapConverter.fromSource(source);
}
let smc: any = null;
if (sourceMapRaw && sourceMapRaw.sourcemap) {
const sourceMap = sourceMapRaw.sourcemap;
Expand Down
1 change: 1 addition & 0 deletions test/files/sourceMapBundle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!**/*.map
19 changes: 19 additions & 0 deletions test/files/sourceMapBundle/android/app/external.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/files/sourceMapBundle/android/app/external.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions test/files/sourceMapBundle/ios/app/external.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/files/sourceMapBundle/ios/app/external.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 49 additions & 1 deletion test/services/log-source-map-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ const testCases: IDictionary<Array<{
expected:
"System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1203)\n",
},
// External maps
{
caseName: "trace message (external map)",
message:
"JS: at onTap (file:///data/data/org.nativescript.sourceMap/files/app/external.js:12:22)",
expected: `JS: at onTap file: ${toPlatformSep(
"src/external-test.js"
)}:3:4\n`,
},
{
caseName: "error message (external map)",
message:
"System.err: Frame: function:'./external-test.js.onTap', file:'file:///data/data/org.nativescript.sourceMap/files/app/external.js', line: 13, column: 32",
expected: `System.err: Frame: function:'./external-test.js.onTap', file:'file: ${toPlatformSep(
"src/external-test.js"
)}:4:4\n`,
},
],
ios: [
{
Expand All @@ -119,7 +136,7 @@ const testCases: IDictionary<Array<{
)}:31:31 JS ERROR Error: Test\n`,
},
{
caseName: "error stack tracew",
caseName: "error stack trace",
message: "onTap@file:///app/bundle.js:296:32",
expected: `onTap@file: ${toPlatformSep(
"src/main-view-model.ts"
Expand All @@ -138,6 +155,33 @@ const testCases: IDictionary<Array<{
"src/main-view-model.ts"
)}:31:18)\n`,
},
// External maps
{
caseName: "console message (external map)",
message: "CONSOLE LOG file:///app/external.js:11:20: Test.",
expected: `CONSOLE LOG file: ${toPlatformSep(
"src/external-test.js"
)}:2:16 Test.\n`,
},
{
caseName: "trace message (external map)",
message: "CONSOLE TRACE file:///app/external.js:12:22: Test",
expected: `CONSOLE TRACE file: ${toPlatformSep(
"src/external-test.js"
)}:3:4 Test\n`,
},
{
caseName: "error message (external map)",
message: "file:///app/external.js:13:32: JS ERROR Error: Test",
expected: `file: ${toPlatformSep(
"src/external-test.js"
)}:4:4 JS ERROR Error: Test\n`,
},
{
caseName: "error stack trace (external map)",
message: "onTap@file:///app/external.js:13:32",
expected: `onTap@file: ${toPlatformSep("src/external-test.js")}:4:4\n`,
},
],
};

Expand All @@ -158,6 +202,10 @@ describe("log-source-map-service", () => {
const fs = testInjector.resolve<IFileSystem>("fs");
const files = fs.enumerateFilesInDirectorySync(originalFilesLocation);
for (const file of files) {
if (file.endsWith(".map")) {
continue;
}

await logSourceMapService.setSourceMapConsumerForFile(file);
}
});
Expand Down

0 comments on commit de678cf

Please sign in to comment.