Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit 5bd45d9

Browse files
refactor(vite): handle config for create-vue projects (#325)
1 parent fabe7df commit 5bd45d9

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

packages/vue-cli-plugin-vuetify/generator/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = (api, opts) => {
2424

2525
// Add dependencies
2626
vuetify.addDependencies(api, opts)
27-
if (opts.useAlaCarte) alaCarte.addDependencies(api, opts.useV3)
27+
if (opts.useAlaCarte && !opts.useVite) alaCarte.addDependencies(api, opts.useV3)
2828
else if (opts.usePolyfill) polyfill.addDependencies(api)
2929

3030
// Vite
@@ -51,14 +51,18 @@ module.exports = (api, opts) => {
5151
if (!opts.installFonts) fonts.addLinks(api, opts.iconFont)
5252
vuetify.setHtmlLang(api, opts.locale)
5353

54-
if (fileExists(api, 'src/public/index.html')) {
55-
fs.unlinkSync(api.resolve('src/public/index.html'))
54+
if (fileExists(api, './src/public/index.html')) {
55+
fs.unlinkSync(api.resolve('./src/public/index.html'))
5656
}
5757

5858
const configFile = api.resolve('./vue.config.js')
5959

6060
if (fileExists(api, configFile)) {
61-
vuetify.addVuetifyLoaderDocsLink(configFile)
61+
if (opts.useVite) {
62+
fs.unlinkSync(configFile)
63+
} else {
64+
vuetify.addVuetifyLoaderDocsLink(configFile)
65+
}
6266
}
6367

6468
api.exitLog('Discord community: https://community.vuetifyjs.com')

packages/vue-cli-plugin-vuetify/generator/templates/v3/vite/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from 'vite'
22
import vue from '@vitejs/plugin-vue'
3-
import vuetify from '@vuetify/vite-plugin'
3+
import vuetify from 'vite-plugin-vuetify'
44

55
const path = require('path')
66

packages/vue-cli-plugin-vuetify/generator/tools/vite.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const { fileExists } = require('../../util/helpers')
2+
const { updateFile } = require('./helpers')
3+
14
function addDependencies (api) {
25
api.extendPackage({
36
devDependencies: {
@@ -15,15 +18,71 @@ function addDependencies (api) {
1518

1619
function 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(/"target":/))
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(/plugins:/))
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+
2786
module.exports = {
2887
addDependencies,
2988
renderFiles,

0 commit comments

Comments
 (0)