@@ -22,12 +22,34 @@ interface IFileLocation {
22
22
export class LogSourceMapService implements Mobile . ILogSourceMapService {
23
23
private static FILE_PREFIX = "file:///" ;
24
24
private getProjectData : Function ;
25
+ private cache : IDictionary < sourcemap . SourceMapConsumer > = { } ;
26
+
25
27
constructor (
26
28
private $fs : IFileSystem ,
27
29
private $projectDataService : IProjectDataService ,
28
30
private $injector : IInjector ,
29
- private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ) {
30
- this . getProjectData = _ . memoize ( this . $projectDataService . getProjectData . bind ( this . $projectDataService ) ) ;
31
+ private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ,
32
+ private $logger : ILogger ) {
33
+ this . getProjectData = _ . memoize ( this . $projectDataService . getProjectData . bind ( this . $projectDataService ) ) ;
34
+ }
35
+
36
+ public async setSourceMapConsumerForFile ( filePath : string ) : Promise < void > {
37
+ try {
38
+ if ( ! this . $fs . getFsStats ( filePath ) . isDirectory ( ) ) {
39
+ const source = this . $fs . readText ( filePath ) ;
40
+ const sourceMapRaw = sourceMapConverter . fromSource ( source ) ;
41
+ let smc : sourcemap . SourceMapConsumer = null ;
42
+ if ( sourceMapRaw && sourceMapRaw . sourcemap ) {
43
+ const sourceMap = sourceMapRaw . sourcemap ;
44
+ smc = new sourcemap . SourceMapConsumer ( sourceMap ) ;
45
+ }
46
+
47
+ this . cache [ filePath ] = smc ;
48
+ }
49
+ } catch ( err ) {
50
+ this . $logger . trace ( `Unable to set sourceMapConsumer for file ${ filePath } . Error is: ${ err } ` ) ;
51
+ }
52
+
31
53
}
32
54
33
55
public replaceWithOriginalFileLocations ( platform : string , messageData : string , loggingOptions : Mobile . IDeviceLogOptions ) : string {
@@ -46,7 +68,7 @@ export class LogSourceMapService implements Mobile.ILogSourceMapService {
46
68
const originalLocation = this . getOriginalFileLocation ( platform , parsedLine , projectData ) ;
47
69
48
70
if ( originalLocation && originalLocation . sourceFile ) {
49
- const { sourceFile, line, column} = originalLocation ;
71
+ const { sourceFile, line, column } = originalLocation ;
50
72
outputData = `${ outputData } ${ parsedLine . messagePrefix } ${ LogSourceMapService . FILE_PREFIX } ${ sourceFile } :${ line } :${ column } ${ parsedLine . messageSuffix } \n` ;
51
73
} else if ( rawLine !== "" ) {
52
74
outputData = `${ outputData } ${ rawLine } \n` ;
@@ -57,25 +79,21 @@ export class LogSourceMapService implements Mobile.ILogSourceMapService {
57
79
}
58
80
59
81
private getOriginalFileLocation ( platform : string , parsedLine : IParsedMessage , projectData : IProjectData ) : IFileLocation {
60
- const fileLoaction = path . join ( this . getFilesLocation ( platform , projectData ) , APP_FOLDER_NAME ) ;
82
+ const fileLocation = path . join ( this . getFilesLocation ( platform , projectData ) , APP_FOLDER_NAME ) ;
61
83
62
84
if ( parsedLine && parsedLine . filePath ) {
63
- const sourceMapFile = path . join ( fileLoaction , parsedLine . filePath ) ;
64
- if ( this . $fs . exists ( sourceMapFile ) ) {
65
- const source = this . $fs . readText ( sourceMapFile ) ;
66
- const sourceMapRaw = sourceMapConverter . fromSource ( source ) ;
67
- if ( sourceMapRaw && sourceMapRaw . sourcemap ) {
68
- const sourceMap = sourceMapRaw . sourcemap ;
69
- const smc = new sourcemap . SourceMapConsumer ( sourceMap ) ;
70
- const originalPosition = smc . originalPositionFor ( { line : parsedLine . line , column : parsedLine . column } ) ;
71
- let sourceFile = originalPosition . source && originalPosition . source . replace ( "webpack:///" , "" ) ;
72
- if ( sourceFile ) {
73
- if ( ! _ . startsWith ( sourceFile , NODE_MODULES_FOLDER_NAME ) ) {
74
- sourceFile = path . join ( projectData . getAppDirectoryRelativePath ( ) , sourceFile ) ;
75
- }
76
- sourceFile = stringReplaceAll ( sourceFile , "/" , path . sep ) ;
77
- return { sourceFile, line : originalPosition . line , column : originalPosition . column } ;
85
+ const sourceMapFile = path . join ( fileLocation , parsedLine . filePath ) ;
86
+ const smc = this . cache [ sourceMapFile ] ;
87
+ if ( smc ) {
88
+ const originalPosition = smc . originalPositionFor ( { line : parsedLine . line , column : parsedLine . column } ) ;
89
+ let sourceFile = originalPosition . source && originalPosition . source . replace ( "webpack:///" , "" ) ;
90
+ if ( sourceFile ) {
91
+ if ( ! _ . startsWith ( sourceFile , NODE_MODULES_FOLDER_NAME ) ) {
92
+ sourceFile = path . join ( projectData . getAppDirectoryRelativePath ( ) , sourceFile ) ;
78
93
}
94
+
95
+ sourceFile = stringReplaceAll ( sourceFile , "/" , path . sep ) ;
96
+ return { sourceFile, line : originalPosition . line , column : originalPosition . column } ;
79
97
}
80
98
}
81
99
}
@@ -111,7 +129,7 @@ export class LogSourceMapService implements Mobile.ILogSourceMapService {
111
129
// "/data/data/org.nativescript.sourceMap/files/app/"
112
130
const devicePath = `${ deviceProjectPath } /${ APP_FOLDER_NAME } /` ;
113
131
// "bundle.js"
114
- filePath = path . relative ( devicePath , `${ "/" } ${ parts [ 0 ] } ` ) ;
132
+ filePath = path . relative ( devicePath , `${ "/" } ${ parts [ 0 ] } ` ) ;
115
133
line = parseInt ( parts [ 1 ] ) ;
116
134
column = parseInt ( parts [ 2 ] ) ;
117
135
messagePrefix = rawMessage . substring ( 0 , fileIndex ) ;
@@ -123,7 +141,7 @@ export class LogSourceMapService implements Mobile.ILogSourceMapService {
123
141
}
124
142
}
125
143
126
- return { filePath, line, column, messagePrefix, messageSuffix} ;
144
+ return { filePath, line, column, messagePrefix, messageSuffix } ;
127
145
}
128
146
129
147
private parseIosLog ( rawMessage : string ) : IParsedMessage {
@@ -138,10 +156,10 @@ export class LogSourceMapService implements Mobile.ILogSourceMapService {
138
156
parts = fileSubstring . split ( ":" ) ;
139
157
140
158
if ( parts && parts . length >= 3 ) {
141
- filePath = parts [ 0 ] ;
159
+ filePath = parts [ 0 ] ;
142
160
// "app/vendor.js"
143
161
if ( _ . startsWith ( filePath , APP_FOLDER_NAME ) ) {
144
- filePath = path . relative ( APP_FOLDER_NAME , parts [ 0 ] ) ;
162
+ filePath = path . relative ( APP_FOLDER_NAME , parts [ 0 ] ) ;
145
163
}
146
164
line = parseInt ( parts [ 1 ] ) ;
147
165
column = parseInt ( parts [ 2 ] ) ;
0 commit comments