1
1
const path = require ( "path" ) ;
2
2
const fs = require ( "fs" ) ;
3
3
4
- const helpers = require ( "./projectHelpers" ) ;
4
+ const { isTypeScript, isAngular } = require ( "./projectHelpers" ) ;
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 " \) ; ) / g;
7
+ const SCOPED_FRAME = `
8
+ if (!global["__snapshot"]) {
9
+ // In case snapshot generation is enabled these modules will get into the bundle
10
+ // but will not be required/evaluated.
11
+ // The snapshot webpack plugin will add them to the tns-java-classes.js bundle file.
12
+ // This way, they will be evaluated on app start as early as possible.
13
+ $1\t$2$3\t$4
14
+ }` ;
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;` ;
5
33
6
34
function addProjectFiles ( projectDir , appDir ) {
7
35
const projectTemplates = getProjectTemplates ( projectDir ) ;
@@ -51,10 +79,10 @@ function copyTemplate(templateName, destinationPath) {
51
79
function getProjectTemplates ( projectDir ) {
52
80
let templates = { }
53
81
54
- if ( helpers . isAngular ( { projectDir} ) ) {
82
+ if ( isAngular ( { projectDir} ) ) {
55
83
templates [ "webpack.angular.js" ] = "webpack.config.js" ;
56
84
templates [ "tsconfig.aot.json" ] = "tsconfig.aot.json" ;
57
- } else if ( helpers . isTypeScript ( { projectDir} ) ) {
85
+ } else if ( isTypeScript ( { projectDir} ) ) {
58
86
templates [ "webpack.typescript.js" ] = "webpack.config.js" ;
59
87
} else {
60
88
templates [ "webpack.javascript.js" ] = "webpack.config.js" ;
@@ -69,7 +97,7 @@ function getAppTemplates(projectDir, appDir) {
69
97
"vendor-platform.ios.ts" : tsOrJs ( projectDir , "vendor-platform.ios" ) ,
70
98
} ;
71
99
72
- if ( helpers . isAngular ( { projectDir} ) ) {
100
+ if ( isAngular ( { projectDir} ) ) {
73
101
templates [ "vendor.angular.ts" ] = tsOrJs ( projectDir , "vendor" ) ;
74
102
} else {
75
103
templates [ "vendor.nativescript.ts" ] = tsOrJs ( projectDir , "vendor" ) ;
@@ -95,31 +123,67 @@ function editExistingProjectFiles(projectDir) {
95
123
const webpackConfigPath = getFullPath ( projectDir , "webpack.config.js" ) ;
96
124
const webpackCommonPath = getFullPath ( projectDir , "webpack.common.js" ) ;
97
125
98
- editWebpackConfig ( webpackConfigPath , replaceStyleUrlResolvePlugin ) ;
99
- editWebpackConfig ( webpackCommonPath , replaceStyleUrlResolvePlugin ) ;
126
+ const configChangeFunctions = [
127
+ replaceStyleUrlResolvePlugin ,
128
+ addSnapshotPlugin ,
129
+ ] ;
130
+
131
+ editFileContent ( webpackConfigPath , ...configChangeFunctions ) ;
132
+ editFileContent ( webpackCommonPath , ...configChangeFunctions ) ;
133
+
134
+ const extension = isAngular ( { projectDir} ) ? "ts" : "js" ;
135
+ const vendorAndroidPath = getFullPath (
136
+ projectDir ,
137
+ `app/vendor-platform.android.${ extension } `
138
+ ) ;
139
+
140
+ editFileContent ( vendorAndroidPath , addSnapshotToVendor ) ;
100
141
}
101
142
102
- function editWebpackConfig ( path , fn ) {
143
+ function editFileContent ( path , ... funcs ) {
103
144
if ( ! fs . existsSync ( path ) ) {
104
145
return ;
105
146
}
106
147
107
- const config = fs . readFileSync ( path , "utf8" ) ;
108
- const newConfig = fn ( config ) ;
148
+ let content = fs . readFileSync ( path , "utf8" ) ;
149
+ funcs . forEach ( fn => content = fn ( content ) ) ;
109
150
110
- fs . writeFileSync ( path , newConfig , "utf8" ) ;
151
+ fs . writeFileSync ( path , content , "utf8" ) ;
111
152
}
112
153
113
154
function replaceStyleUrlResolvePlugin ( config ) {
155
+ if ( config . indexOf ( "StyleUrlResolvePlugin" ) === - 1 ) {
156
+ return config ;
157
+ }
158
+
159
+ console . info ( "Replacing deprecated StyleUrlsResolvePlugin with UrlResolvePlugin..." ) ;
114
160
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" ) ;
115
161
}
116
162
163
+ function addSnapshotPlugin ( config ) {
164
+ if ( config . indexOf ( "NativeScriptSnapshotPlugin" ) > - 1 ) {
165
+ return config ;
166
+ }
167
+
168
+ console . info ( "Adding NativeScriptSnapshotPlugin configuration..." ) ;
169
+ return config . replace ( CONFIG_MATCH , CONFIG_REPLACE ) ;
170
+ }
171
+
172
+ function addSnapshotToVendor ( content ) {
173
+ if ( content . indexOf ( "__snapshot" ) > - 1 ) {
174
+ return content ;
175
+ }
176
+
177
+ console . info ( "Adding __snapshot configuration to app/vendor-platform.android ..." ) ;
178
+ return content . replace ( FRAME_MATCH , SCOPED_FRAME ) ;
179
+ }
180
+
117
181
function getFullPath ( projectDir , filePath ) {
118
182
return path . resolve ( projectDir , filePath ) ;
119
183
}
120
184
121
185
function tsOrJs ( projectDir , name ) {
122
- const extension = helpers . isTypeScript ( { projectDir} ) ? "ts" : "js" ;
186
+ const extension = isTypeScript ( { projectDir} ) ? "ts" : "js" ;
123
187
return `${ name } .${ extension } ` ;
124
188
}
125
189
0 commit comments