@@ -3,26 +3,21 @@ const fs = require('fs-extra');
33const replace = require ( 'replace-in-file' ) ;
44
55const 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-
146
157module . exports = ( api , options , rootOptions ) => {
168
179 console . log ( 'options.isNativeOnly - ' , options . isNativeOnly )
1810 console . log ( 'options.isNVW - ' , options . isNVW )
1911 console . log ( 'options.isNewProject - ' , options . isNewProject )
12+ console . log ( 'usingTS - ' , api . hasPlugin ( 'typescript' ) )
13+ console . log ( 'usingBabel - ' , api . hasPlugin ( 'babel' ) )
2014
2115 const existingDirPath = './example/' ;
16+ const jsOrTs = api . hasPlugin ( 'typescript' ) ? 'ts' : 'js'
2217
2318 const srcfiles = [
24- 'router.js' ,
25- 'main.js' ,
19+ 'router.' + jsOrTs ,
20+ 'main.' + jsOrTs ,
2621 'App.vue' ,
2722 'views/About.vue' ,
2823 'views/Home.vue' ,
@@ -32,7 +27,7 @@ module.exports = (api, options, rootOptions) => {
3227
3328 const appfiles = [
3429 'package.json' ,
35- 'main.js' ,
30+ 'main.' + jsOrTs ,
3631 'App.native.vue' ,
3732 'App.ios.vue' ,
3833 'App.android.vue' ,
@@ -152,8 +147,8 @@ module.exports = (api, options, rootOptions) => {
152147 renderFilesIndividually ( api , srcfiles , commonRenderOptions , './templates/simple/without-nvw/src/' , './src/' ) ;
153148 renderFilesIndividually ( api , appfiles , commonRenderOptions , './templates/simple/without-nvw/app/' , './app/' ) ;
154149
155- vueRouterSetup ( api , './' ) ;
156- vuexSetup ( api , './' ) ;
150+ vueRouterSetup ( api , './' , jsOrTs ) ;
151+ vuexSetup ( api , './' , jsOrTs ) ;
157152 }
158153
159154 // New Project and is using Nativescript-Vue-Web
@@ -187,8 +182,8 @@ module.exports = (api, options, rootOptions) => {
187182 renderFilesIndividually ( api , srcfiles , commonRenderOptions , './templates/simple/without-nvw/src/' , existingDirPath + 'src/' ) ;
188183 renderFilesIndividually ( api , appfiles , commonRenderOptions , './templates/simple/without-nvw/app/' , existingDirPath + 'app/' ) ;
189184
190- vueRouterSetup ( api , existingDirPath ) ;
191- vuexSetup ( api , existingDirPath ) ;
185+ vueRouterSetup ( api , existingDirPath , jsOrTs ) ;
186+ vuexSetup ( api , existingDirPath , jsOrTs ) ;
192187 }
193188
194189 // Existing Project and is using Nativescript-Vue-Web
@@ -224,6 +219,11 @@ module.exports = (api, options, rootOptions) => {
224219 writeEnvFiles ( './' )
225220 nsconfigSetup ( api . resolve ( 'nsconfig.json' ) ) ;
226221
222+ if ( hasPlugin ( 'typescript' ) ) {
223+ tsconfigSetup ( api . resolve ( 'tsconfig.json' ) ) ;
224+ tslintSetup ( api . resolve ( 'tslint.json' ) ) ;
225+ }
226+
227227 // for new projects that are native only, move files/dirs and delete others
228228 if ( options . isNativeOnly ) {
229229 // move store.js file from ./src to ./app
@@ -257,6 +257,13 @@ module.exports = (api, options, rootOptions) => {
257257 writeEnvFiles ( existingDirPath )
258258 nsconfigSetup ( api . resolve ( existingDirPath + 'nsconfig.json' ) ) ;
259259
260+ if ( hasPlugin ( 'typescript' ) ) {
261+ tsconfigSetup ( api . resolve ( existingDirPath + 'tsconfig.json' ) ) ;
262+ tslintSetup ( api . resolve ( existingDirPath + 'tslint.json' ) ) ;
263+
264+ }
265+
266+
260267 // for existing projects that are native only, try and copy items from src
261268 // but do not delete anythign in src.
262269 if ( options . isNativeOnly ) {
@@ -283,11 +290,11 @@ module.exports = (api, options, rootOptions) => {
283290
284291// setup vue-router options
285292// 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 ) => {
287294 try {
288295 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` )
291298 }
292299 } catch ( err ) {
293300 throw err
@@ -296,13 +303,13 @@ const vueRouterSetup = module.exports.vueRouterSetup = async (api, filePathPrepe
296303}
297304
298305// setup Vuex
299- const vuexSetup = module . exports . vuexSetup = async ( api , filePathPrepend ) => {
306+ const vuexSetup = module . exports . vuexSetup = async ( api , filePathPrepend , jsOrTs ) => {
300307 try {
301308 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` )
306313 }
307314 } catch ( err ) {
308315 throw err
@@ -312,6 +319,13 @@ const vuexSetup = module.exports.vuexSetup = async (api, filePathPrepend) => {
312319
313320// write out babel.config.js options
314321const 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+
315329 try {
316330
317331 babelReplaceOptions . files = filePath ;
@@ -411,6 +425,72 @@ const nsconfigSetup = module.exports.nsconfigSetup = async (nsconfigPath) => {
411425
412426}
413427
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+
414494const copyDirs = module . exports . copyDirs = async ( srcPath , destPath ) => {
415495 try {
416496 const baseDir = extractCallDir ( )
0 commit comments