1
1
const path = require ( 'path' ) ;
2
- const fs = require ( 'fs' )
3
- const globby = require ( 'globby' ) ;
4
- const rimraf = require ( 'rimraf' )
2
+ const fs = require ( 'fs-extra' ) ;
5
3
const replace = require ( 'replace-in-file' ) ;
6
4
5
+
7
6
module . exports = ( api , options , rootOptions ) => {
7
+
8
+ console . log ( 'options.isNativeOnly - ' , options . isNativeOnly )
9
+ console . log ( 'options.isNVW - ' , options . isNVW )
10
+ console . log ( 'options.isNewProject - ' , options . isNewProject )
11
+
12
+ // New Project & Native Only -- should never be able to use Nativescript-Vue-Web
13
+ if ( options . isNativeOnly && options . isNVW ) {
14
+ throw Error ( 'Invalid options chosen. You cannot have a Native only project and use Nativescript-Vue-Web' )
15
+ }
16
+
17
+ if ( options . isNativeOnly )
18
+ options . isNVW = false ;
8
19
9
- const commonRendorOptions = {
20
+ const commonRenderOptions = {
10
21
applicationName : api . generator . pkg . name ,
11
22
applicationVersion : api . generator . pkg . version ,
12
23
applicationAndroidVersionCode : api . generator . pkg . version . split ( '.' ) . join ( '0' ) ,
13
24
applicationDescription : api . generator . pkg . description || api . generator . pkg . name ,
14
25
applicationLicense : api . generator . pkg . license || 'MIT' ,
15
26
applicationId : options . applicationId ,
16
27
historyMode : options . historyMode || false ,
17
- doesCompile : api . hasPlugin ( 'babel' ) || api . hasPlugin ( 'typescript' )
28
+ doesCompile : api . hasPlugin ( 'babel' ) || api . hasPlugin ( 'typescript' ) ,
29
+ usingBabel : api . hasPlugin ( 'babel' ) ,
30
+ usingTS : api . hasPlugin ( 'typescript' )
18
31
}
19
32
20
33
console . log ( 'adding to package.json' ) ;
@@ -30,107 +43,174 @@ module.exports = (api, options, rootOptions) => {
30
43
}
31
44
} ,
32
45
scripts : {
46
+ "setup-webpack-config" : "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance pre" ,
47
+ "remove-webpack-config" : "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance post" ,
33
48
"serve:web" : "vue-cli-service serve --mode development.web" ,
34
- "serve:android" : "vue-cli-service tns:dev --mode development.android" ,
35
- "serve:ios" : "vue-cli-service tns:dev --mode development.ios" ,
49
+ "serve:android" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE= development.android tns run android --bundle && npm run remove-webpack-config " ,
50
+ "serve:ios" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE= development.ios tns run ios --bundle && npm run remove-webpack-config " ,
36
51
"build:web" : "vue-cli-service build --mode production.web" ,
37
- "build:android" : "vue-cli-service tns:prod --mode production.android" ,
38
- "build:ios" : "vue-cli-service tns:prod --mode production.ios" ,
39
- } ,
52
+ "build:android" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE= production.android tns run android --bundle && npm run remove-webpack-config " ,
53
+ "build:ios" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE= production.ios tns run ios --bundle && npm run remove-webpack-config " ,
54
+ } ,
40
55
dependencies : {
41
56
'nativescript-vue' : '^2.0.2' ,
42
57
'tns-core-modules' : '^4.2.1' ,
43
58
} ,
44
59
devDependencies : {
45
- '@babel/core' : '^7.1.2' ,
46
- '@babel/preset-env' : '^7.1.0' ,
47
- '@babel/types' : '^7.1.3' ,
48
- 'babel-loader' : '^8.0.4' ,
49
- 'babel-traverse' : '^6.26.0' ,
50
60
'clean-webpack-plugin' : '^0.1.19' ,
51
- 'copy-webpack-plugin' : '^4.5.4' ,
61
+ 'copy-webpack-plugin' : '^4.6.0' ,
62
+ 'cross-env' : '^5.2.0' ,
52
63
'nativescript-dev-webpack' : '^0.17.0' ,
53
64
'nativescript-vue-template-compiler' : '^2.0.2' ,
54
65
'nativescript-worker-loader' : '~0.9.1' ,
55
66
'replace-in-file' : '^3.4.2' ,
56
- "string-replace-loader" : "^2.1.1" ,
57
67
}
58
68
} )
59
69
60
- console . log ( 'deleting from package.json' ) ;
61
-
70
+ // if the project is using babel, then load appropriate packages
71
+ if ( api . hasPlugin ( 'babel' ) ) {
72
+ api . extendPackage ( {
73
+ devDependencies : {
74
+ '@babel/core' : '^7.1.2' ,
75
+ '@babel/preset-env' : '^7.1.0' ,
76
+ '@babel/types' : '^7.1.3' ,
77
+ 'babel-loader' : '^8.0.4' ,
78
+ 'babel-traverse' : '^6.26.0' ,
79
+ }
80
+ } )
81
+ }
62
82
83
+ console . log ( 'deleting from package.json' ) ;
63
84
api . extendPackage ( pkg => {
64
- // delete pkg.dependencies['vue']
65
- delete pkg . devDependencies [
66
- // 'vue-template-compiler',
67
- 'babel-core'
68
- ]
69
- // delete pkg.browserslist
85
+ // if the project is using babel, then delete babel-core
86
+ if ( api . hasPlugin ( 'babel' ) ) {
87
+ delete pkg . devDependencies [
88
+ 'babel-core'
89
+ ]
90
+ }
91
+ // we will be replacing these
70
92
delete pkg . scripts [ 'serve' ] ,
71
93
delete pkg . scripts [ 'build' ]
72
94
73
95
} )
74
96
75
97
console . log ( 'doing template rendering' ) ;
76
98
77
- // render the App_Resources files
78
- api . render ( './templates/App_Resources' , commonRendorOptions )
79
-
80
99
// use the answer from the invoke prompt and if it's a new project use the new template
81
100
// and if it is an existing project, use the existing template
82
101
if ( options . isNewProject ) {
83
- api . render ( './templates/simple/new' , commonRendorOptions )
84
- } else {
85
- api . render ( './templates/simple/existing' , commonRendorOptions )
102
+
103
+ // New Project and not using Nativescript-Vue-Web
104
+ if ( ! options . isNVW && ! options . isNativeOnly ) {
105
+ api . render ( './templates/simple/without-nvw/new' , commonRenderOptions )
106
+
107
+ if ( api . hasPlugin ( 'vue-router' ) ) {
108
+ api . injectImports ( 'src/main.js' , `import router from '~/router'` )
109
+ api . injectRootOptions ( 'src/main.js' , `router` )
110
+ }
111
+
112
+ if ( api . hasPlugin ( 'vuex' ) ) {
113
+ api . injectImports ( 'src/main.js' , `import store from '~/store'` )
114
+ api . injectRootOptions ( 'src/main.js' , `store` )
115
+ api . injectImports ( 'app/main.js' , `import store from 'src/store'` )
116
+ api . injectRootOptions ( 'app/main.js' , `store` )
117
+
118
+ }
119
+ }
120
+
121
+ // New Project and is using Nativescript-Vue-Web
122
+ if ( options . isNVW && ! options . isNativeOnly ) {
123
+
124
+ }
125
+
126
+ // New Project & Native Only -- should never be able to use Nativescript-Vue-Web
127
+ if ( ! options . isNVW && options . isNativeOnly ) {
128
+ api . render ( './templates/simple/native-only/new' , commonRenderOptions ) ;
129
+ }
130
+
131
+ if ( options . isNativeOnly && options . isNVW ) {
132
+ // should never reach this block of code
133
+ }
134
+
135
+
136
+ } else { // Exising Project
137
+
138
+ // Existing Project and not using Nativescript-Vue-Web
139
+ if ( ! options . isNVW && ! options . isNativeOnly ) {
140
+ api . render ( './templates/simple/without-nvw/existing' , commonRenderOptions )
141
+ }
142
+
143
+ // Existing Project and is using Nativescript-Vue-Web
144
+ if ( options . isNVW && ! options . isNativeOnly ) {
145
+
146
+ }
147
+
148
+ // Existing Project & Native Only -- should never be able to use Nativescript-Vue-Web
149
+ if ( ! options . isNVW && options . isNativeOnly ) {
150
+ api . render ( './templates/simple/native-only/existing' , commonRenderOptions )
151
+ }
152
+
153
+ if ( options . isNVW && options . isNativeOnly ) {
154
+ // should never reach this block of code
155
+ }
156
+
86
157
}
87
158
159
+
160
+
161
+
162
+
88
163
api . onCreateComplete ( ( ) => {
89
164
90
165
const newline = process . platform === 'win32' ? '\r\n' : '\n' ;
91
- const webpackConfigFile = api . resolve ( './webpack.config.js' ) ;
92
- const src = api . resolve ( 'src' ) ;
93
166
const gitignorePath = api . resolve ( '.gitignore' ) ;
167
+ const gitignoreWebpackConfig = api . resolve ( '.webpack.config.js' ) ;
168
+
169
+ // // setup string replacement options for babel.config.js file
170
+ // if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
171
+ // const replaceOptions = {
172
+ // files: './babel.config.js',
173
+ // from: ' \'@vue/app\'',
174
+ // to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
175
+ // }
176
+ // replace(replaceOptions, (err, changes) => {
177
+ // if (err) throw err;
178
+ // });
179
+ // }
180
+
181
+
182
+ // for new projects that are native only, move files/dirs and delete others
183
+ if ( options . isNewProject && options . isNativeOnly ) {
184
+
185
+ // move store.js file from ./src to ./app
186
+ if ( api . hasPlugin ( 'vuex' ) ) {
187
+ fs . move ( './src/store.js' , './app/store.js' , ( err ) => {
188
+ if ( err ) throw err ;
189
+ } )
190
+ }
94
191
95
- // // // // delete the 'public' directory
96
- // // // const publicPath = api.resolve('public')
97
- // // // if(fs.existsSync(publicPath)) {
98
- // // // rimraf.sync(publicPath)
99
- // // // }
100
-
101
- // rename any webpack.config.js file that might already be there
102
- if ( fs . existsSync ( webpackConfigFile ) ) {
103
- fs . rename ( webpackConfigFile , './webpack.config.old' , ( err ) => {
104
- if ( err ) throw err ;
105
- } ) ;
106
- }
192
+ // move assets directory from ./src/assets to ./app/assets
193
+ fs . ensureDir ( './src/assets' , err => {
194
+ if ( err ) throw err ;
195
+ fs . move ( './src/assets' , './app/assets' , err => {
196
+ if ( err ) throw err ;
197
+ } )
198
+ } )
107
199
108
- // rename main.js to main.web.js
109
- if ( fs . existsSync ( path . resolve ( src , 'main.js' ) ) ) {
110
- fs . rename ( path . resolve ( src , 'main.js' ) , path . resolve ( src , 'main.web.js' ) , ( err ) => {
111
- if ( err ) throw err ;
112
- } ) ;
113
- }
200
+ fs . remove ( './src' , err => {
201
+ if ( err ) throw err
202
+ } )
114
203
115
- // setup string replacement options for babel.config.js file
116
- if ( api . hasPlugin ( 'babel' ) && fs . existsSync ( './babel.config.js' ) ) {
117
- const replaceOptions = {
118
- files : './babel.config.js' ,
119
- from : ' \'@vue/app\'' ,
120
- to : ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]' ,
121
- }
122
- replace ( replaceOptions , ( err , changes ) => {
123
- if ( err ) throw err ;
124
- } ) ;
125
204
}
126
205
206
+
127
207
// write out environmental files
128
- const developmentAndroid = 'NODE_ENV=development' + newline + 'VUE_PLATFORM =android' + newline + 'VUE_APP_MODE=native' ;
129
- const developmentIOS = 'NODE_ENV=development' + newline + 'VUE_PLATFORM =ios' + newline + 'VUE_APP_MODE=native' ;
130
- const developmentWeb = 'NODE_ENV=development' + newline + 'VUE_PLATFORM =web' + newline + 'VUE_APP_MODE=web' ;
131
- const productionAndroid = 'NODE_ENV=production' + newline + 'VUE_PLATFORM =android' + newline + 'VUE_APP_MODE=native' ;
132
- const productionIOS = 'NODE_ENV=production' + newline + 'VUE_PLATFORM =ios' + newline + 'VUE_APP_MODE=native' ;
133
- const productionWeb = 'NODE_ENV=production' + newline + 'VUE_PLATFORM =web' + newline + 'VUE_APP_MODE=web' ;
208
+ const developmentAndroid = 'NODE_ENV=development' + newline + 'VUE_APP_PLATFORM =android' + newline + 'VUE_APP_MODE=native' ;
209
+ const developmentIOS = 'NODE_ENV=development' + newline + 'VUE_APP_PLATFORM =ios' + newline + 'VUE_APP_MODE=native' ;
210
+ const developmentWeb = 'NODE_ENV=development' + newline + 'VUE_APP_PLATFORM =web' + newline + 'VUE_APP_MODE=web' ;
211
+ const productionAndroid = 'NODE_ENV=production' + newline + 'VUE_APP_PLATFORM =android' + newline + 'VUE_APP_MODE=native' ;
212
+ const productionIOS = 'NODE_ENV=production' + newline + 'VUE_APP_PLATFORM =ios' + newline + 'VUE_APP_MODE=native' ;
213
+ const productionWeb = 'NODE_ENV=production' + newline + 'VUE_APP_PLATFORM =web' + newline + 'VUE_APP_MODE=web' ;
134
214
135
215
fs . writeFileSync ( './.env.development.android' , developmentAndroid , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
136
216
fs . writeFileSync ( './.env.development.ios' , developmentIOS , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
@@ -142,8 +222,8 @@ module.exports = (api, options, rootOptions) => {
142
222
143
223
// write nsconfig.json
144
224
const nsconfig = {
145
- 'appPath' : 'src ' ,
146
- 'appResourcesPath' : 'src /App_Resources'
225
+ 'appPath' : 'app ' ,
226
+ 'appResourcesPath' : 'app /App_Resources'
147
227
}
148
228
fs . writeFileSync ( './nsconfig.json' , JSON . stringify ( nsconfig , null , 2 ) , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
149
229
@@ -156,7 +236,7 @@ module.exports = (api, options, rootOptions) => {
156
236
gitignoreContent = '' ;
157
237
}
158
238
159
- const gitignoreAdditions = newline + '# NativeScript application' + newline + 'hooks' + newline + 'platforms'
239
+ const gitignoreAdditions = newline + '# NativeScript application' + newline + 'hooks' + newline + 'platforms' + newline + './webpack.config.js'
160
240
if ( gitignoreContent . indexOf ( gitignoreAdditions ) === - 1 ) {
161
241
gitignoreContent += gitignoreAdditions
162
242
@@ -165,6 +245,6 @@ module.exports = (api, options, rootOptions) => {
165
245
166
246
} )
167
247
168
-
248
+
169
249
170
250
}
0 commit comments