@@ -43,19 +43,31 @@ module.exports = SnapshotGenerator;
43
43
44
44
SnapshotGenerator . SNAPSHOT_PACKAGE_NANE = "nativescript-android-snapshot" ;
45
45
46
- SnapshotGenerator . prototype . preprocessInputFiles = function ( inputFiles , outputFile ) {
46
+ SnapshotGenerator . prototype . preprocessInputFiles = function ( inputFiles , outputFile ) {
47
47
// Make some modifcations on the original bundle and save it on the specified path
48
48
const bundlePreambleContent = fs . readFileSync ( BUNDLE_PREAMBLE_PATH , "utf8" ) ;
49
49
const bundleEndingContent = fs . readFileSync ( BUNDLE_ENDING_PATH , "utf8" ) ;
50
50
51
- const inputFilesContent = inputFiles . map ( file => fs . readFileSync ( file , "utf8" ) ) . join ( "\n" ) ;
51
+ // IMPORTANT: join by "\n;" as we are joining IIFE functions and if the snapshot tool is used
52
+ // along with Uglify configuration for replacing `;` with `/n`, we will generate invalid JavaScript
53
+ // Example:
54
+ // (function() {
55
+ // some code here
56
+ // })()
57
+ // // sourceMapUrl......
58
+ // ** when we join without `;` here, the next IIFE is assumed as a function call to the result of the first IIFE
59
+ // (function() {
60
+ // some code here
61
+ // })()
62
+ // // sourceMapUrl......
63
+ const inputFilesContent = inputFiles . map ( file => fs . readFileSync ( file , "utf8" ) ) . join ( "\n;" ) ;
52
64
const snapshotFileContent = bundlePreambleContent + "\n" + inputFilesContent + "\n" + bundleEndingContent ;
53
65
fs . writeFileSync ( outputFile , snapshotFileContent , { encoding : "utf8" } ) ;
54
66
}
55
67
56
68
const snapshotToolsDownloads = { } ;
57
69
58
- SnapshotGenerator . prototype . downloadMksnapshotTool = function ( snapshotToolsPath , v8Version , targetArch ) {
70
+ SnapshotGenerator . prototype . downloadMksnapshotTool = function ( snapshotToolsPath , v8Version , targetArch ) {
59
71
const hostOS = getHostOS ( ) ;
60
72
const mksnapshotToolRelativePath = join ( "mksnapshot-tools" , "v8-v" + v8Version , hostOS + "-" + os . arch ( ) , "mksnapshot-" + targetArch ) ;
61
73
const mksnapshotToolPath = join ( snapshotToolsPath , mksnapshotToolRelativePath ) ;
@@ -84,7 +96,7 @@ SnapshotGenerator.prototype.downloadMksnapshotTool = function(snapshotToolsPath,
84
96
return snapshotToolsDownloads [ mksnapshotToolPath ] ;
85
97
}
86
98
87
- SnapshotGenerator . prototype . convertToAndroidArchName = function ( archName ) {
99
+ SnapshotGenerator . prototype . convertToAndroidArchName = function ( archName ) {
88
100
switch ( archName ) {
89
101
case "arm" : return "armeabi-v7a" ;
90
102
case "arm64" : return "arm64-v8a" ;
@@ -94,7 +106,7 @@ SnapshotGenerator.prototype.convertToAndroidArchName = function(archName) {
94
106
}
95
107
}
96
108
97
- SnapshotGenerator . prototype . runMksnapshotTool = function ( snapshotToolsPath , inputFile , v8Version , targetArchs , buildCSource , mksnapshotParams ) {
109
+ SnapshotGenerator . prototype . runMksnapshotTool = function ( snapshotToolsPath , inputFile , v8Version , targetArchs , buildCSource , mksnapshotParams ) {
98
110
// Cleans the snapshot build folder
99
111
shelljs . rm ( "-rf" , join ( this . buildPath , "snapshots" ) ) ;
100
112
@@ -120,14 +132,22 @@ SnapshotGenerator.prototype.runMksnapshotTool = function(snapshotToolsPath, inpu
120
132
const command = `${ currentArchMksnapshotToolPath } ${ inputFile } --startup_blob ${ join ( currentArchBlobOutputPath , `${ SNAPSHOT_BLOB_NAME } .blob` ) } ${ params } ` ;
121
133
122
134
return new Promise ( ( resolve , reject ) => {
123
- const child = child_process . exec ( command , { encoding : "utf8" } , ( error , stdout , stderr ) => {
135
+ const child = child_process . exec ( command , { encoding : "utf8" } , ( error , stdout , stderr ) => {
124
136
const errorHeader = `Target architecture: ${ androidArch } \n` ;
137
+ let errorFooter = `` ;
138
+ if ( stderr . length || error ) {
139
+ try {
140
+ require ( inputFile ) ;
141
+ } catch ( e ) {
142
+ errorFooter = `\nJavaScript execution error: ${ e . stack } $` ;
143
+ }
144
+ }
125
145
126
146
if ( stderr . length ) {
127
- const message = `${ errorHeader } ${ stderr } ` ;
147
+ const message = `${ errorHeader } ${ stderr } ${ errorFooter } ` ;
128
148
reject ( new Error ( message ) ) ;
129
149
} else if ( error ) {
130
- error . message = `${ errorHeader } ${ error . message } ` ;
150
+ error . message = `${ errorHeader } ${ error . message } ${ errorFooter } ` ;
131
151
reject ( error ) ;
132
152
} else {
133
153
console . log ( stdout ) ;
@@ -139,7 +159,7 @@ SnapshotGenerator.prototype.runMksnapshotTool = function(snapshotToolsPath, inpu
139
159
if ( buildCSource ) {
140
160
const currentArchSrcOutputPath = join ( this . buildPath , "snapshots/src" , androidArch ) ;
141
161
shelljs . mkdir ( "-p" , currentArchSrcOutputPath ) ;
142
- shellJsExecuteInDir ( currentArchBlobOutputPath , function ( ) {
162
+ shellJsExecuteInDir ( currentArchBlobOutputPath , function ( ) {
143
163
shelljs . exec ( `xxd -i ${ SNAPSHOT_BLOB_NAME } .blob > ${ join ( currentArchSrcOutputPath , `${ SNAPSHOT_BLOB_NAME } .c` ) } ` ) ;
144
164
} ) ;
145
165
}
@@ -150,7 +170,7 @@ SnapshotGenerator.prototype.runMksnapshotTool = function(snapshotToolsPath, inpu
150
170
} ) ;
151
171
}
152
172
153
- SnapshotGenerator . prototype . buildSnapshotLibs = function ( androidNdkBuildPath , targetArchs ) {
173
+ SnapshotGenerator . prototype . buildSnapshotLibs = function ( androidNdkBuildPath , targetArchs ) {
154
174
// Compile *.c files to produce *.so libraries with ndk-build tool
155
175
const ndkBuildPath = join ( this . buildPath , "ndk-build" ) ;
156
176
const androidArchs = targetArchs . map ( arch => this . convertToAndroidArchName ( arch ) ) ;
@@ -159,22 +179,22 @@ SnapshotGenerator.prototype.buildSnapshotLibs = function(androidNdkBuildPath, ta
159
179
shelljs . cp ( "-r" , NDK_BUILD_SEED_PATH , ndkBuildPath ) ;
160
180
fs . writeFileSync ( join ( ndkBuildPath , "jni/Application.mk" ) , "APP_ABI := " + androidArchs . join ( " " ) ) ; // create Application.mk file
161
181
shelljs . mv ( join ( this . buildPath , "snapshots/src/*" ) , join ( ndkBuildPath , "jni" ) ) ;
162
- shellJsExecuteInDir ( ndkBuildPath , function ( ) {
182
+ shellJsExecuteInDir ( ndkBuildPath , function ( ) {
163
183
shelljs . exec ( androidNdkBuildPath ) ;
164
184
} ) ;
165
185
return join ( ndkBuildPath , "libs" ) ;
166
186
}
167
187
168
- SnapshotGenerator . prototype . buildIncludeGradle = function ( ) {
188
+ SnapshotGenerator . prototype . buildIncludeGradle = function ( ) {
169
189
shelljs . cp ( INCLUDE_GRADLE_PATH , join ( this . buildPath , "include.gradle" ) ) ;
170
190
}
171
191
172
- SnapshotGenerator . prototype . generate = function ( options ) {
192
+ SnapshotGenerator . prototype . generate = function ( options ) {
173
193
// Arguments validation
174
194
options = options || { } ;
175
195
if ( ! options . v8Version ) { throw new Error ( "No v8 version specified." ) ; }
176
196
if ( ! options . snapshotToolsPath ) { throw new Error ( "snapshotToolsPath option is not specified." ) ; }
177
- const preprocessedInputFile = options . preprocessedInputFile || join ( this . buildPath , "inputFile.preprocessed" ) ;
197
+ const preprocessedInputFile = options . preprocessedInputFile || join ( this . buildPath , "inputFile.preprocessed" ) ;
178
198
179
199
console . log ( "***** Starting snapshot generation using V8 version: " , options . v8Version ) ;
180
200
0 commit comments