@@ -9,6 +9,9 @@ module.exports = (api, options, rootOptions) => {
9
9
console . log ( 'options.isNVW - ' , options . isNVW )
10
10
console . log ( 'options.isNewProject - ' , options . isNewProject )
11
11
12
+ const newline = process . platform === 'win32' ? '\r\n' : '\n' ;
13
+
14
+
12
15
// New Project & Native Only -- should never be able to use Nativescript-Vue-Web
13
16
if ( options . isNativeOnly && options . isNVW ) {
14
17
throw Error ( 'Invalid options chosen. You cannot have a Native only project and use Nativescript-Vue-Web' )
@@ -46,8 +49,8 @@ module.exports = (api, options, rootOptions) => {
46
49
"setup-webpack-config" : "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance pre" ,
47
50
"remove-webpack-config" : "node ./node_modules/vue-cli-plugin-nativescript-vue/lib/scripts/webpack-maintenance post" ,
48
51
"serve:web" : "vue-cli-service serve --mode development.web" ,
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 " ,
52
+ "serve:android" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=development.android tns run android --bundle" ,
53
+ "serve:ios" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=development.ios tns run ios --bundle" ,
51
54
"build:web" : "vue-cli-service build --mode production.web" ,
52
55
"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
56
"build:ios" : "npm run setup-webpack-config && cross-env-shell VUE_CLI_MODE=production.ios tns run ios --bundle && npm run remove-webpack-config" ,
@@ -140,6 +143,14 @@ module.exports = (api, options, rootOptions) => {
140
143
// should never reach this block of code
141
144
}
142
145
146
+ // create the babel.config.js file
147
+ if ( api . hasPlugin ( 'babel' ) && fs . existsSync ( './babel.config.js' ) ) {
148
+ applyBabelConfig ( api , './babel.config.js' ) ;
149
+ }
150
+
151
+ // copy App_Resources to the ./app folder
152
+ copyDirs ( './templates/App_Resources' , './app/App_Resources' )
153
+
143
154
144
155
} else { // Exising Project
145
156
@@ -162,30 +173,52 @@ module.exports = (api, options, rootOptions) => {
162
173
// should never reach this block of code
163
174
}
164
175
176
+ // create the babel.config.js file
177
+ if ( api . hasPlugin ( 'babel' ) && fs . existsSync ( './example/babel.config.js' ) ) {
178
+ applyBabelConfig ( api , './example/babel.config.js' ) ;
179
+ }
180
+
181
+ // copy App_Resources to the ./app folder
182
+ copyDirs ( './templates/App_Resources' , './example/app/App_Resources' )
183
+
165
184
}
166
185
167
-
168
-
169
186
170
-
187
+
188
+
171
189
api . onCreateComplete ( ( ) => {
172
190
173
- const newline = process . platform === 'win32' ? '\r\n' : '\n' ;
174
191
const gitignorePath = api . resolve ( '.gitignore' ) ;
175
- const gitignoreWebpackConfig = api . resolve ( '.webpack.config.js' ) ;
176
192
177
- // // setup string replacement options for babel.config.js file
178
- // if(api.hasPlugin('babel') && fs.existsSync('./babel.config.js')) {
179
- // const replaceOptions = {
180
- // files: './babel.config.js',
181
- // from: ' \'@vue/app\'',
182
- // to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
183
- // }
184
- // replace(replaceOptions, (err, changes) => {
185
- // if (err) throw err;
186
- // });
187
- // }
193
+ // setup string replacement options for babel.config.js file
194
+ if ( api . hasPlugin ( 'babel' ) && fs . existsSync ( './babel.config.js' ) ) {
195
+
196
+ const replaceOptions = {
197
+ files : '' ,
198
+ from : ' \'@vue/app\'' ,
199
+ to : ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]' ,
200
+ }
201
+
202
+ // edit babel.config.js with options we need
203
+ if ( options . isNewProject ) {
204
+ replaceOptions . files = './babel.config.js' ;
205
+ replace ( replaceOptions , ( err , changes ) => {
206
+ if ( err ) throw err ;
207
+ } ) ;
208
+ } else {
209
+ replaceOptions . files = './example/babel.config.js' ;
210
+ fs . ensureFile ( replaceOptions . files , err => {
211
+ if ( err ) throw err ;
212
+ replace ( replaceOptions , ( err , changes ) => {
213
+ if ( err ) throw err ;
214
+ } ) ;
215
+ } )
216
+
217
+ }
218
+
219
+ }
188
220
221
+
189
222
190
223
// for new projects that are native only, move files/dirs and delete others
191
224
if ( options . isNewProject && options . isNativeOnly ) {
@@ -205,10 +238,16 @@ module.exports = (api, options, rootOptions) => {
205
238
} )
206
239
} )
207
240
241
+ // remove src directory as we don't need it
208
242
fs . remove ( './src' , err => {
209
243
if ( err ) throw err
210
244
} )
211
245
246
+ // remove public directory as we don't need it
247
+ fs . remove ( './public' , err => {
248
+ if ( err ) throw err
249
+ } )
250
+
212
251
}
213
252
214
253
@@ -256,3 +295,36 @@ module.exports = (api, options, rootOptions) => {
256
295
257
296
258
297
}
298
+
299
+
300
+ const applyBabelConfig = module . exports . applyBabelConfig = async ( api , filePath ) => {
301
+
302
+ try {
303
+ api . render ( files => {
304
+ files [ filePath ] = api . genJSConfig ( {
305
+ plugins : [ "@babel/plugin-syntax-dynamic-import" ] ,
306
+ presets : [
307
+ '@vue/app'
308
+ ]
309
+ } )
310
+ } )
311
+
312
+ } catch ( err ) {
313
+ throw err
314
+ }
315
+ }
316
+
317
+ const copyDirs = module . exports . copyDirs = async ( srcPath , destPath ) => {
318
+
319
+ const baseDir = extractCallDir ( )
320
+ const source = path . resolve ( baseDir , srcPath )
321
+ await fs . copy ( source , destPath )
322
+
323
+ }
324
+
325
+ // extract callsite file location using error stack
326
+ const extractCallDir = module . exports . extractCallDir = ( ) => {
327
+ const obj = { }
328
+ Error . captureStackTrace ( obj )
329
+ return path . dirname ( obj . stack . split ( '\n' ) [ 3 ] . match ( / \s \( ( .* ) : \d + : \d + \) $ / ) [ 1 ] )
330
+ }
0 commit comments