1+ const { fileExists } = require ( '../../util/helpers' )
2+ const { updateFile } = require ( './helpers' )
3+
14function addDependencies ( api ) {
25 api . extendPackage ( {
36 devDependencies : {
@@ -15,15 +18,71 @@ function addDependencies (api) {
1518
1619function renderFiles ( api , opts ) {
1720 const ext = opts . hasTS ? 'ts' : 'js'
21+ const viteConfigFile = `./vite.config.${ ext } `
22+ const viteConfigPath = api . resolve ( viteConfigFile )
23+
1824 const files = {
1925 './index.html' : '../templates/v3/vite/index.vite.html' ,
20- [ `./vite.config.${ ext } ` ] : `../templates/v3/vite/vite.config.${ ext } ` ,
2126 './src/styles/_variables.scss' : '../templates/v3/vite/styles/_variables.scss' ,
2227 }
2328
29+ if ( ! fileExists ( api , viteConfigPath ) ) {
30+ files [ viteConfigFile ] = `../templates/v3/vite/vite.config.${ ext } `
31+ updateJsConfigTarget ( api )
32+ }
33+ else updateViteConfig ( api , viteConfigPath )
34+
2435 api . render ( files , opts )
2536}
2637
38+ function updateJsConfigTarget ( api ) {
39+ const jsConfigPath = api . resolve ( './jsconfig.json' )
40+
41+ if ( fileExists ( api , jsConfigPath ) ) {
42+ updateFile ( api , jsConfigPath , ( lines ) => {
43+ const targetIndex = lines . findIndex ( line => line . match ( / " t a r g e t " : / ) )
44+
45+ if ( targetIndex !== - 1 ) {
46+ lines [ targetIndex ] = lines [ targetIndex ] . replace ( 'es5' , 'esnext' )
47+ }
48+
49+ return lines
50+ } )
51+ }
52+ }
53+
54+ function updateViteConfig ( api , viteConfigPath ) {
55+ updateFile ( api , viteConfigPath , ( lines ) => {
56+ const pluginsIndex = lines . findIndex ( line => line . match ( / p l u g i n s : / ) )
57+ const exportIndex = lines . findIndex ( line => line . includes ( 'export default' ) )
58+
59+ const vuetifyPluginImport = `\n// https://github.com/vuetifyjs/vuetify-loader/tree/next/packages/vite-plugin\nimport vuetify from 'vite-plugin-vuetify'\n`
60+ const vuetifyPlugin = '\n\t\tvuetify({ autoImport: true }),\n'
61+
62+ if ( pluginsIndex !== - 1 ) {
63+ lines [ exportIndex - 2 ] = vuetifyPluginImport
64+ const matchedPlugins = lines [ pluginsIndex ] . match ( / (?< = \[ ) .+ ?(? = \] ) / )
65+
66+ if ( matchedPlugins !== null ) {
67+ const currentPluginsArr = matchedPlugins [ 0 ] . split ( ',' )
68+ const allPlugins = '' . concat (
69+ currentPluginsArr
70+ . map ( plugin => `\n\t\t${ plugin . trim ( ) } ` )
71+ . concat ( vuetifyPlugin )
72+ )
73+
74+ lines [ pluginsIndex ] = lines [ pluginsIndex ] . replace ( / (?< = \[ ) .+ ?(? = \] ) / , allPlugins )
75+
76+ return lines
77+ } else {
78+ lines [ pluginsIndex ] = lines [ pluginsIndex ] . replace ( / \[ .* \] / , vuetifyPlugin )
79+
80+ return lines
81+ }
82+ }
83+ } )
84+ }
85+
2786module . exports = {
2887 addDependencies,
2988 renderFiles,
0 commit comments