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 " \) ; ) / ;
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
+ }` ;
5
15
6
16
function addProjectFiles ( projectDir , appDir ) {
7
17
const projectTemplates = getProjectTemplates ( projectDir ) ;
@@ -51,10 +61,10 @@ function copyTemplate(templateName, destinationPath) {
51
61
function getProjectTemplates ( projectDir ) {
52
62
let templates = { }
53
63
54
- if ( helpers . isAngular ( { projectDir} ) ) {
64
+ if ( isAngular ( { projectDir} ) ) {
55
65
templates [ "webpack.angular.js" ] = "webpack.config.js" ;
56
66
templates [ "tsconfig.aot.json" ] = "tsconfig.aot.json" ;
57
- } else if ( helpers . isTypeScript ( { projectDir} ) ) {
67
+ } else if ( isTypeScript ( { projectDir} ) ) {
58
68
templates [ "webpack.typescript.js" ] = "webpack.config.js" ;
59
69
} else {
60
70
templates [ "webpack.javascript.js" ] = "webpack.config.js" ;
@@ -69,7 +79,7 @@ function getAppTemplates(projectDir, appDir) {
69
79
"vendor-platform.ios.ts" : tsOrJs ( projectDir , "vendor-platform.ios" ) ,
70
80
} ;
71
81
72
- if ( helpers . isAngular ( { projectDir} ) ) {
82
+ if ( isAngular ( { projectDir} ) ) {
73
83
templates [ "vendor.angular.ts" ] = tsOrJs ( projectDir , "vendor" ) ;
74
84
} else {
75
85
templates [ "vendor.nativescript.ts" ] = tsOrJs ( projectDir , "vendor" ) ;
@@ -95,15 +105,24 @@ function editExistingProjectFiles(projectDir) {
95
105
const webpackConfigPath = getFullPath ( projectDir , "webpack.config.js" ) ;
96
106
const webpackCommonPath = getFullPath ( projectDir , "webpack.common.js" ) ;
97
107
98
- editWebpackConfig ( webpackConfigPath , replaceStyleUrlResolvePlugin ) ;
99
- editWebpackConfig ( webpackCommonPath , replaceStyleUrlResolvePlugin ) ;
108
+ editFileContent ( webpackConfigPath , replaceStyleUrlResolvePlugin ) ;
109
+ editFileContent ( webpackCommonPath , replaceStyleUrlResolvePlugin ) ;
110
+
111
+ const extension = isAngular ( { projectDir} ) ? "ts" : "js" ;
112
+ const vendorAndroidPath = getFullPath (
113
+ projectDir ,
114
+ `app/vendor-platform.android.${ extension } `
115
+ ) ;
116
+
117
+ editFileContent ( vendorAndroidPath , addSnapshotToVendor ) ;
100
118
}
101
119
102
- function editWebpackConfig ( path , fn ) {
120
+ function editFileContent ( path , fn ) {
103
121
if ( ! fs . existsSync ( path ) ) {
104
122
return ;
105
123
}
106
124
125
+ console . log ( 'editing: ' + path )
107
126
const config = fs . readFileSync ( path , "utf8" ) ;
108
127
const newConfig = fn ( config ) ;
109
128
@@ -114,12 +133,25 @@ function replaceStyleUrlResolvePlugin(config) {
114
133
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
134
}
116
135
136
+ function addSnapshotPlugin ( config ) {
137
+
138
+ }
139
+
140
+ function addSnapshotToVendor ( content ) {
141
+ if ( content . indexOf ( "__snapshot" ) > - 1 ) {
142
+ return content ;
143
+ }
144
+
145
+
146
+ return content . replace ( FRAME_MATCH , SCOPED_FRAME ) ;
147
+ }
148
+
117
149
function getFullPath ( projectDir , filePath ) {
118
150
return path . resolve ( projectDir , filePath ) ;
119
151
}
120
152
121
153
function tsOrJs ( projectDir , name ) {
122
- const extension = helpers . isTypeScript ( { projectDir} ) ? "ts" : "js" ;
154
+ const extension = isTypeScript ( { projectDir} ) ? "ts" : "js" ;
123
155
return `${ name } .${ extension } ` ;
124
156
}
125
157
0 commit comments