@@ -3,26 +3,21 @@ const fs = require('fs-extra');
3
3
const replace = require ( 'replace-in-file' ) ;
4
4
5
5
const newline = process . platform === 'win32' ? '\r\n' : '\n' ;
6
- const babelReplaceOptions = {
7
- files : '' ,
8
- from : ' \'@vue/app\'' ,
9
- to : ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]' ,
10
- }
11
-
12
-
13
-
14
6
15
7
module . exports = ( api , options , rootOptions ) => {
16
8
17
9
console . log ( 'options.isNativeOnly - ' , options . isNativeOnly )
18
10
console . log ( 'options.isNVW - ' , options . isNVW )
19
11
console . log ( 'options.isNewProject - ' , options . isNewProject )
12
+ console . log ( 'usingTS - ' , api . hasPlugin ( 'typescript' ) )
13
+ console . log ( 'usingBabel - ' , api . hasPlugin ( 'babel' ) )
20
14
21
15
const existingDirPath = './example/' ;
16
+ const jsOrTs = api . hasPlugin ( 'typescript' ) ? 'ts' : 'js'
22
17
23
18
const srcfiles = [
24
- 'router.js' ,
25
- 'main.js' ,
19
+ 'router.' + jsOrTs ,
20
+ 'main.' + jsOrTs ,
26
21
'App.vue' ,
27
22
'views/About.vue' ,
28
23
'views/Home.vue' ,
@@ -32,7 +27,7 @@ module.exports = (api, options, rootOptions) => {
32
27
33
28
const appfiles = [
34
29
'package.json' ,
35
- 'main.js' ,
30
+ 'main.' + jsOrTs ,
36
31
'App.native.vue' ,
37
32
'App.ios.vue' ,
38
33
'App.android.vue' ,
@@ -152,8 +147,8 @@ module.exports = (api, options, rootOptions) => {
152
147
renderFilesIndividually ( api , srcfiles , commonRenderOptions , './templates/simple/without-nvw/src/' , './src/' ) ;
153
148
renderFilesIndividually ( api , appfiles , commonRenderOptions , './templates/simple/without-nvw/app/' , './app/' ) ;
154
149
155
- vueRouterSetup ( api , './' ) ;
156
- vuexSetup ( api , './' ) ;
150
+ vueRouterSetup ( api , './' , jsOrTs ) ;
151
+ vuexSetup ( api , './' , jsOrTs ) ;
157
152
}
158
153
159
154
// New Project and is using Nativescript-Vue-Web
@@ -187,8 +182,8 @@ module.exports = (api, options, rootOptions) => {
187
182
renderFilesIndividually ( api , srcfiles , commonRenderOptions , './templates/simple/without-nvw/src/' , existingDirPath + 'src/' ) ;
188
183
renderFilesIndividually ( api , appfiles , commonRenderOptions , './templates/simple/without-nvw/app/' , existingDirPath + 'app/' ) ;
189
184
190
- vueRouterSetup ( api , existingDirPath ) ;
191
- vuexSetup ( api , existingDirPath ) ;
185
+ vueRouterSetup ( api , existingDirPath , jsOrTs ) ;
186
+ vuexSetup ( api , existingDirPath , jsOrTs ) ;
192
187
}
193
188
194
189
// Existing Project and is using Nativescript-Vue-Web
@@ -224,6 +219,11 @@ module.exports = (api, options, rootOptions) => {
224
219
writeEnvFiles ( './' )
225
220
nsconfigSetup ( api . resolve ( 'nsconfig.json' ) ) ;
226
221
222
+ if ( hasPlugin ( 'typescript' ) ) {
223
+ tsconfigSetup ( api . resolve ( 'tsconfig.json' ) ) ;
224
+ tslintSetup ( api . resolve ( 'tslint.json' ) ) ;
225
+ }
226
+
227
227
// for new projects that are native only, move files/dirs and delete others
228
228
if ( options . isNativeOnly ) {
229
229
// move store.js file from ./src to ./app
@@ -257,6 +257,13 @@ module.exports = (api, options, rootOptions) => {
257
257
writeEnvFiles ( existingDirPath )
258
258
nsconfigSetup ( api . resolve ( existingDirPath + 'nsconfig.json' ) ) ;
259
259
260
+ if ( hasPlugin ( 'typescript' ) ) {
261
+ tsconfigSetup ( api . resolve ( existingDirPath + 'tsconfig.json' ) ) ;
262
+ tslintSetup ( api . resolve ( existingDirPath + 'tslint.json' ) ) ;
263
+
264
+ }
265
+
266
+
260
267
// for existing projects that are native only, try and copy items from src
261
268
// but do not delete anythign in src.
262
269
if ( options . isNativeOnly ) {
@@ -283,11 +290,11 @@ module.exports = (api, options, rootOptions) => {
283
290
284
291
// setup vue-router options
285
292
// will not setup any vue-router options for native app
286
- const vueRouterSetup = module . exports . vueRouterSetup = async ( api , filePathPrepend ) => {
293
+ const vueRouterSetup = module . exports . vueRouterSetup = async ( api , filePathPrepend , jsOrTs ) => {
287
294
try {
288
295
if ( api . hasPlugin ( 'vue-router' ) ) {
289
- api . injectImports ( filePathPrepend + 'src/main.js' , `import router from '~/router'` )
290
- api . injectRootOptions ( filePathPrepend + 'src/main.js' , `router` )
296
+ api . injectImports ( filePathPrepend + 'src/main' + jsOrTs , `import router from '~/router'` )
297
+ api . injectRootOptions ( filePathPrepend + 'src/main' + jsOrTs , `router` )
291
298
}
292
299
} catch ( err ) {
293
300
throw err
@@ -296,13 +303,13 @@ const vueRouterSetup = module.exports.vueRouterSetup = async (api, filePathPrepe
296
303
}
297
304
298
305
// setup Vuex
299
- const vuexSetup = module . exports . vuexSetup = async ( api , filePathPrepend ) => {
306
+ const vuexSetup = module . exports . vuexSetup = async ( api , filePathPrepend , jsOrTs ) => {
300
307
try {
301
308
if ( api . hasPlugin ( 'vuex' ) ) {
302
- api . injectImports ( filePathPrepend + 'src/main.js' , `import store from '~/store'` )
303
- api . injectRootOptions ( filePathPrepend + 'src/main.js' , `store` )
304
- api . injectImports ( filePathPrepend + 'app/main.js' , `import store from 'src/store'` )
305
- api . injectRootOptions ( filePathPrepend + 'app/main.js' , `store` )
309
+ api . injectImports ( filePathPrepend + 'src/main' + jsOrTs , `import store from '~/store'` )
310
+ api . injectRootOptions ( filePathPrepend + 'src/main' + jsOrTs , `store` )
311
+ api . injectImports ( filePathPrepend + 'app/main' + jsOrTs , `import store from 'src/store'` )
312
+ api . injectRootOptions ( filePathPrepend + 'app/main' + jsOrTs , `store` )
306
313
}
307
314
} catch ( err ) {
308
315
throw err
@@ -312,6 +319,13 @@ const vuexSetup = module.exports.vuexSetup = async (api, filePathPrepend) => {
312
319
313
320
// write out babel.config.js options
314
321
const applyBabelConfig = module . exports . applyBabelConfig = async ( api , filePath ) => {
322
+
323
+ const babelReplaceOptions = {
324
+ files : '' ,
325
+ from : ' \'@vue/app\'' ,
326
+ to : ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]' ,
327
+ }
328
+
315
329
try {
316
330
317
331
babelReplaceOptions . files = filePath ;
@@ -411,6 +425,72 @@ const nsconfigSetup = module.exports.nsconfigSetup = async (nsconfigPath) => {
411
425
412
426
}
413
427
428
+ // setup tsconfigSetup
429
+ const tsconfigSetup = module . exports . tsconfigSetup = async ( tsconfigPath ) => {
430
+ let tsconfigContent = '' ;
431
+
432
+ try {
433
+ if ( fs . existsSync ( tsconfigPath ) ) {
434
+ tsconfigContent = JSON . parse ( fs . readFileSync ( tsconfigPath , { encoding : 'utf8' } ) ) ;
435
+ } else {
436
+ tsconfigContent = { } ;
437
+ }
438
+
439
+ delete tsconfigContent . paths [ '@/*' ] ;
440
+ tsconfigContent . paths [ '~/*' ] = "src/*" ;
441
+ tsconfigContent . paths [ 'src/*' ] = "src/*" ;
442
+ tsconfigContent . paths [ 'assets/*' ] = "src/assets/*" ;
443
+ tsconfigContent . paths [ 'fonts/*' ] = "src/fonts/*" ;
444
+ tsconfigContent . paths [ 'root/*' ] = "/*" ;
445
+ tsconfigContent . paths [ 'components/*' ] = "/src/components*" ;
446
+
447
+ tsconfigContent . include . push [ 'app/**/*.ts' ] ;
448
+ tsconfigContent . include . push [ 'app/**/*.tsx' ] ;
449
+ tsconfigContent . include . push [ 'app/**/*.vue' ] ;
450
+
451
+ tsconfigContent . exclude . push [ 'platforms' ] ;
452
+ tsconfigContent . exclude . push [ 'hooks' ] ;
453
+
454
+ fs . writeFileSync ( tsconfigPath , JSON . stringify ( tsconfigContent , null , 2 ) , { encoding : 'utf8' } , ( err ) => {
455
+ if ( err ) console . error ( err )
456
+ } ) ;
457
+
458
+
459
+ } catch ( err ) {
460
+ throw err
461
+ }
462
+
463
+ }
464
+
465
+ // setup tslintSetup
466
+ const tslintSetup = module . exports . tslintSetup = async ( tslintPath ) => {
467
+ let tslintContent = '' ;
468
+
469
+ try {
470
+ if ( fs . existsSync ( tslintPath ) ) {
471
+ tslintContent = JSON . parse ( fs . readFileSync ( tslintPath , { encoding : 'utf8' } ) ) ;
472
+ } else {
473
+ return ;
474
+ }
475
+
476
+ tslintContent . linterOptions . exclude . push [ 'platforms/**' ] ;
477
+ tslintContent . linterOptions . exclude . push [ 'hooks/**' ] ;
478
+
479
+ tslintContent . exclude . push [ 'platforms' ] ;
480
+ tslintContent . exclude . push [ 'hooks' ] ;
481
+
482
+
483
+ fs . writeFileSync ( tslintPath , JSON . stringify ( tslintContent , null , 2 ) , { encoding : 'utf8' } , ( err ) => {
484
+ if ( err ) console . error ( err )
485
+ } ) ;
486
+
487
+
488
+ } catch ( err ) {
489
+ throw err
490
+ }
491
+
492
+ }
493
+
414
494
const copyDirs = module . exports . copyDirs = async ( srcPath , destPath ) => {
415
495
try {
416
496
const baseDir = extractCallDir ( )
0 commit comments