@@ -3,16 +3,34 @@ const fs = require("fs");
3
3
4
4
const { isTypeScript, isAngular } = require ( "./projectHelpers" ) ;
5
5
6
- const FRAME_MATCH = / ( \s * ) ( r e q u i r e \( " u i \/ f r a m e " \) ; ) ( \s * ) ( r e q u i r e \( " u i \/ f r a m e \/ a c t i v i t y " \) ; ) / ;
6
+ const FRAME_MATCH = / ( \s * ) ( r e q u i r e \( " u i \/ f r a m e " \) ; ) ( \s * ) ( r e q u i r e \( " u i \/ f r a m e \/ a c t i v i t y " \) ; ) / g ;
7
7
const SCOPED_FRAME = `
8
8
if (!global["__snapshot"]) {
9
9
// In case snapshot generation is enabled these modules will get into the bundle
10
- // but will not be required/evaluated.
10
+ // but will not be required/evaluated.
11
11
// The snapshot webpack plugin will add them to the tns-java-classes.js bundle file.
12
12
// This way, they will be evaluated on app start as early as possible.
13
- $1\t$2$3\t$4
13
+ $1\t$2$3\t$4
14
14
}` ;
15
15
16
+ const CONFIG_MATCH = / ( e x p o r t s = [ ^ ] + ?) \s * r e t u r n ( { [ ^ ] + t a r g e t : \s * n a t i v e s c r i p t T a r g e t [ ^ ] + ?} ; ) / ;
17
+ const CONFIG_REPLACE = `$1
18
+
19
+ const config = $2
20
+
21
+ if (env.snapshot) {
22
+ plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
23
+ chunk: "vendor",
24
+ projectRoot: __dirname,
25
+ webpackConfig: config,
26
+ targetArchs: ["arm", "arm64"],
27
+ tnsJavaClassesOptions: { packages: ["tns-core-modules" ] },
28
+ useLibs: false
29
+ }));
30
+ }
31
+
32
+ return config;` ;
33
+
16
34
function addProjectFiles ( projectDir , appDir ) {
17
35
const projectTemplates = getProjectTemplates ( projectDir ) ;
18
36
Object . keys ( projectTemplates ) . forEach ( function ( templateName ) {
@@ -105,8 +123,13 @@ function editExistingProjectFiles(projectDir) {
105
123
const webpackConfigPath = getFullPath ( projectDir , "webpack.config.js" ) ;
106
124
const webpackCommonPath = getFullPath ( projectDir , "webpack.common.js" ) ;
107
125
108
- editFileContent ( webpackConfigPath , replaceStyleUrlResolvePlugin ) ;
109
- editFileContent ( webpackCommonPath , replaceStyleUrlResolvePlugin ) ;
126
+ const configChangeFunctions = [
127
+ replaceStyleUrlResolvePlugin ,
128
+ addSnapshotPlugin ,
129
+ ] ;
130
+
131
+ editFileContent ( webpackConfigPath , ...configChangeFunctions ) ;
132
+ editFileContent ( webpackCommonPath , ...configChangeFunctions ) ;
110
133
111
134
const extension = isAngular ( { projectDir} ) ? "ts" : "js" ;
112
135
const vendorAndroidPath = getFullPath (
@@ -117,33 +140,31 @@ function editExistingProjectFiles(projectDir) {
117
140
editFileContent ( vendorAndroidPath , addSnapshotToVendor ) ;
118
141
}
119
142
120
- function editFileContent ( path , fn ) {
143
+ function editFileContent ( path , ... funcs ) {
121
144
if ( ! fs . existsSync ( path ) ) {
122
145
return ;
123
146
}
124
147
125
- console . log ( 'editing: ' + path )
126
- const config = fs . readFileSync ( path , "utf8" ) ;
127
- const newConfig = fn ( config ) ;
148
+ let content = fs . readFileSync ( path , "utf8" ) ;
149
+ funcs . forEach ( fn => content = fn ( content ) ) ;
128
150
129
- fs . writeFileSync ( path , newConfig , "utf8" ) ;
151
+ fs . writeFileSync ( path , content , "utf8" ) ;
130
152
}
131
153
132
154
function replaceStyleUrlResolvePlugin ( config ) {
133
155
return config . replace ( / S t y l e U r l R e s o l v e P l u g i n / g, "UrlResolvePlugin" ) ;
134
156
}
135
157
136
158
function addSnapshotPlugin ( config ) {
137
-
159
+ return config . indexOf ( "NativeScriptSnapshotPlugin" ) > - 1 ?
160
+ config :
161
+ config . replace ( CONFIG_MATCH , CONFIG_REPLACE ) ;
138
162
}
139
163
140
164
function addSnapshotToVendor ( content ) {
141
- if ( content . indexOf ( "__snapshot" ) > - 1 ) {
142
- return content ;
143
- }
144
-
145
-
146
- return content . replace ( FRAME_MATCH , SCOPED_FRAME ) ;
165
+ return content . indexOf ( "__snapshot" ) > - 1 ?
166
+ content :
167
+ content . replace ( FRAME_MATCH , SCOPED_FRAME ) ;
147
168
}
148
169
149
170
function getFullPath ( projectDir , filePath ) {
0 commit comments