create-vite-vue 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +10 -10
- package/lib/plugins/autoRoute.js +79 -0
- package/lib/plugins/https.js +38 -0
- package/lib/plugins/index.js +23 -0
- package/lib/plugins/tailwind.js +14 -0
- package/lib/template.js +0 -9
- package/package.json +1 -1
- package/lib/router.js +0 -21
- package/lib/viteConfig.js +0 -24
package/bin/index.js
CHANGED
|
@@ -7,11 +7,10 @@ import { fileURLToPath } from 'url'
|
|
|
7
7
|
import { parseExtraPlugins, parseFeatures } from '../lib/features.js'
|
|
8
8
|
import { generateMainFile } from '../lib/mainFile.js'
|
|
9
9
|
import { generatePackageJson } from '../lib/package.js'
|
|
10
|
+
import { setupPlugins } from '../lib/plugins/index.js'
|
|
10
11
|
import { askAutoRoute, askRunDev, chooseFeatures, chooseLanguage, getProjectName } from '../lib/prompts.js'
|
|
11
|
-
import {
|
|
12
|
-
import { appendTailwind, copyBaseTemplate, copyOptionalTemplates, updateIndexHtml } from '../lib/template.js'
|
|
12
|
+
import { copyBaseTemplate, copyOptionalTemplates, updateIndexHtml } from '../lib/template.js'
|
|
13
13
|
import { checkNodeVersion, detectPackageManager, runCmd } from '../lib/utils.js'
|
|
14
|
-
import { configureVite } from '../lib/viteConfig.js'
|
|
15
14
|
|
|
16
15
|
// ===================== 常量 =====================
|
|
17
16
|
const requiredVersion = '22.19.0'
|
|
@@ -43,21 +42,22 @@ const pkgCommands = {
|
|
|
43
42
|
// 模板文件处理
|
|
44
43
|
copyBaseTemplate(language, targetDir, __dirname)
|
|
45
44
|
updateIndexHtml(projectName, targetDir)
|
|
46
|
-
appendTailwind(extraPlugins, targetDir)
|
|
47
45
|
copyOptionalTemplates(features, extraPlugins, language, targetDir, __dirname)
|
|
48
46
|
|
|
47
|
+
// 配置插件
|
|
48
|
+
setupPlugins(features, extraPlugins, {
|
|
49
|
+
language,
|
|
50
|
+
targetDir,
|
|
51
|
+
autoRoute,
|
|
52
|
+
enableHttps
|
|
53
|
+
})
|
|
54
|
+
|
|
49
55
|
// 生成 main 文件
|
|
50
56
|
await generateMainFile(features, extraPlugins, language, targetDir)
|
|
51
57
|
|
|
52
58
|
// package.json
|
|
53
59
|
generatePackageJson(projectName, features, extraPlugins, autoRoute, enableHttps, language, targetDir, pkgManager)
|
|
54
60
|
|
|
55
|
-
// 配置 vite
|
|
56
|
-
configureVite(language, autoRoute, enableHttps, targetDir)
|
|
57
|
-
|
|
58
|
-
// 配置 router
|
|
59
|
-
configureRouter(features.router, autoRoute, language, targetDir)
|
|
60
|
-
|
|
61
61
|
// 安装依赖
|
|
62
62
|
runCmd(pkgCommands[pkgManager].install, targetDir)
|
|
63
63
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// lib/plugins/autoRoute.js
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
|
|
5
|
+
export function setupAutoRoute (language, targetDir) {
|
|
6
|
+
setupRouter(language, targetDir)
|
|
7
|
+
setupVite(language, targetDir)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// ================= router =================
|
|
11
|
+
function setupRouter (language, targetDir) {
|
|
12
|
+
const routerIndexPath = path.join(
|
|
13
|
+
targetDir,
|
|
14
|
+
`src/router/index.${language === 'ts' ? 'ts' : 'js'}`
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
const content = `import { createRouter, createWebHistory } from 'vue-router'
|
|
18
|
+
import routes from '~pages'
|
|
19
|
+
|
|
20
|
+
routes.unshift({ path: '/', redirect: '/home' })
|
|
21
|
+
|
|
22
|
+
export default createRouter({ history: createWebHistory(), routes })`
|
|
23
|
+
|
|
24
|
+
fs.writeFileSync(routerIndexPath, content)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ================= vite =================
|
|
28
|
+
function setupVite (language, targetDir) {
|
|
29
|
+
const viteConfigPath = path.join(
|
|
30
|
+
targetDir,
|
|
31
|
+
`vite.config.${language === 'ts' ? 'ts' : 'js'}`
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
if(!fs.existsSync(viteConfigPath)) return
|
|
35
|
+
|
|
36
|
+
let viteConfig = fs.readFileSync(viteConfigPath, 'utf-8')
|
|
37
|
+
|
|
38
|
+
// import fs
|
|
39
|
+
if(!viteConfig.includes("import fs from 'fs'")) {
|
|
40
|
+
viteConfig = `import fs from 'fs'\n${viteConfig}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// import Pages
|
|
44
|
+
if(!viteConfig.includes("vite-plugin-pages")) {
|
|
45
|
+
viteConfig = viteConfig.replace(
|
|
46
|
+
/(import .*?from .*?\n)/,
|
|
47
|
+
`$1import Pages from 'vite-plugin-pages'\n`
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 插件注入
|
|
52
|
+
if(!viteConfig.includes("Pages({")) {
|
|
53
|
+
viteConfig = viteConfig.replace(
|
|
54
|
+
/plugins:\s*\[/,
|
|
55
|
+
`plugins: [\n Pages({
|
|
56
|
+
dirs: 'src/views',
|
|
57
|
+
extensions: ['vue'],
|
|
58
|
+
exclude: ['**/_*.vue'],
|
|
59
|
+
async extendRoute(route) {
|
|
60
|
+
const componentPath = path.resolve(process.cwd(), route.component.slice(1))
|
|
61
|
+
const dirPath = path.dirname(componentPath)
|
|
62
|
+
const metaFile = path.resolve(dirPath, 'meta.json')
|
|
63
|
+
if(fs.existsSync(metaFile)) {
|
|
64
|
+
try {
|
|
65
|
+
const metaContent = fs.readFileSync(metaFile, 'utf-8')
|
|
66
|
+
const meta = JSON.parse(metaContent)
|
|
67
|
+
route.meta = { ...(route.meta || {}), ...meta }
|
|
68
|
+
} catch(err) {
|
|
69
|
+
console.warn(\`加载 \${metaFile} 失败:\`, err)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { ...route }
|
|
73
|
+
}
|
|
74
|
+
}),`
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fs.writeFileSync(viteConfigPath, viteConfig)
|
|
79
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// lib/plugins/https.js
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
|
|
5
|
+
export function setupHttps (targetDir) {
|
|
6
|
+
const viteConfigPathTs = path.join(targetDir, 'vite.config.ts')
|
|
7
|
+
const viteConfigPathJs = path.join(targetDir, 'vite.config.js')
|
|
8
|
+
|
|
9
|
+
const viteConfigPath = fs.existsSync(viteConfigPathTs)
|
|
10
|
+
? viteConfigPathTs
|
|
11
|
+
: viteConfigPathJs
|
|
12
|
+
|
|
13
|
+
if(!viteConfigPath || !fs.existsSync(viteConfigPath)) return
|
|
14
|
+
|
|
15
|
+
let viteConfig = fs.readFileSync(viteConfigPath, 'utf-8')
|
|
16
|
+
|
|
17
|
+
// ================== 1. 注入 import ==================
|
|
18
|
+
if(!viteConfig.includes("vite-plugin-mkcert")) {
|
|
19
|
+
viteConfig = viteConfig.replace(
|
|
20
|
+
/(import .*?from .*?\n)/,
|
|
21
|
+
`$1import mkcert from 'vite-plugin-mkcert'\n`
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ================== 2. 注入插件 ==================
|
|
26
|
+
if(!viteConfig.includes('mkcert()')) {
|
|
27
|
+
viteConfig = viteConfig.replace(
|
|
28
|
+
/plugins:\s*\[/,
|
|
29
|
+
`plugins: [\n mkcert(),`
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fs.writeFileSync(viteConfigPath, viteConfig)
|
|
34
|
+
|
|
35
|
+
// ================== 3. 提示 ==================
|
|
36
|
+
console.log('🔐 已启用 HTTPS(mkcert)')
|
|
37
|
+
console.log('👉 首次运行会自动生成本地证书,请稍等...')
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// lib/plugins/index.js
|
|
2
|
+
import { setupAutoRoute } from './autoRoute.js'
|
|
3
|
+
import { setupHttps } from './https.js'
|
|
4
|
+
import { setupTailwind } from './tailwind.js'
|
|
5
|
+
|
|
6
|
+
export function setupPlugins (features, extraPlugins, context) {
|
|
7
|
+
const { language, targetDir, autoRoute, enableHttps } = context
|
|
8
|
+
|
|
9
|
+
// Tailwind
|
|
10
|
+
if(extraPlugins.includes('tailwind')) {
|
|
11
|
+
setupTailwind(targetDir)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// HTTPS
|
|
15
|
+
if(enableHttps) {
|
|
16
|
+
setupHttps(targetDir)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 自动路由
|
|
20
|
+
if(features.router && autoRoute) {
|
|
21
|
+
setupAutoRoute(language, targetDir)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// lib/plugins/tailwind.js
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
|
|
5
|
+
export function setupTailwind (targetDir) {
|
|
6
|
+
const stylePath = path.join(targetDir, 'src/style.css')
|
|
7
|
+
if(!fs.existsSync(stylePath)) return
|
|
8
|
+
|
|
9
|
+
const original = fs.readFileSync(stylePath, 'utf-8')
|
|
10
|
+
|
|
11
|
+
if(!original.includes('@import "tailwindcss";')) {
|
|
12
|
+
fs.writeFileSync(stylePath, `@import "tailwindcss";\n${original}`)
|
|
13
|
+
}
|
|
14
|
+
}
|
package/lib/template.js
CHANGED
|
@@ -14,15 +14,6 @@ export function updateIndexHtml (projectName, targetDir) {
|
|
|
14
14
|
fs.writeFileSync(indexPath, indexContent.replace(/<title>.*<\/title>/, `<title>${projectName}</title>`))
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export function appendTailwind (extraPlugins, targetDir) {
|
|
18
|
-
if(!extraPlugins.includes('tailwind')) return
|
|
19
|
-
const stylePath = path.join(targetDir, 'src/style.css')
|
|
20
|
-
const original = fs.readFileSync(stylePath, 'utf-8')
|
|
21
|
-
if(!original.startsWith('@import "tailwindcss";')) {
|
|
22
|
-
fs.writeFileSync(stylePath, `@import "tailwindcss";\n${original}`)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
17
|
export function copyOptionalTemplates (features, extraPlugins, language, targetDir, __dirname) {
|
|
27
18
|
const copy = name => {
|
|
28
19
|
fs.cpSync(path.resolve(__dirname, `../template/${name}`), targetDir, { recursive: true })
|
package/package.json
CHANGED
package/lib/router.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// lib/router.js
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
|
|
5
|
-
export function configureRouter (routerEnabled, autoRoute, language, targetDir) {
|
|
6
|
-
if(!routerEnabled) return
|
|
7
|
-
const routerIndexPath = path.join(targetDir, `src/router/index.${language === 'ts' ? 'ts' : 'js'}`)
|
|
8
|
-
const content = autoRoute
|
|
9
|
-
? `import { createRouter, createWebHistory } from 'vue-router'
|
|
10
|
-
import routes from '~pages'
|
|
11
|
-
|
|
12
|
-
routes.unshift({ path: '/', redirect: '/home' })
|
|
13
|
-
|
|
14
|
-
export default createRouter({ history: createWebHistory(), routes })`
|
|
15
|
-
: `import { createRouter, createWebHistory } from 'vue-router'
|
|
16
|
-
|
|
17
|
-
const routes = [ { path: '/', component: () => import('@/views/home/index.vue') } ]
|
|
18
|
-
|
|
19
|
-
export default createRouter({ history: createWebHistory(), routes })`
|
|
20
|
-
fs.writeFileSync(routerIndexPath, content)
|
|
21
|
-
}
|
package/lib/viteConfig.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// lib/viteConfig.js
|
|
2
|
-
import fs from 'fs'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
|
|
5
|
-
export function configureVite (language, autoRoute, enableHttps, targetDir) {
|
|
6
|
-
const viteConfigPath = path.join(targetDir, `vite.config.${language === 'ts' ? 'ts' : 'js'}`)
|
|
7
|
-
if(!fs.existsSync(viteConfigPath)) return
|
|
8
|
-
let viteConfig = fs.readFileSync(viteConfigPath, 'utf-8')
|
|
9
|
-
|
|
10
|
-
// mkcert
|
|
11
|
-
if(enableHttps && !viteConfig.includes("vite-plugin-mkcert")) {
|
|
12
|
-
viteConfig = viteConfig.replace(/(import .*?from .*?\n)/, `$1import mkcert from 'vite-plugin-mkcert'\n`)
|
|
13
|
-
viteConfig = viteConfig.replace(/plugins:\s*\[/, `plugins: [\n mkcert(),`)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// 自动路由
|
|
17
|
-
if(autoRoute) {
|
|
18
|
-
if(!viteConfig.includes("import fs from 'fs'")) viteConfig = `import fs from 'fs'\n${viteConfig}`
|
|
19
|
-
if(!viteConfig.includes("import Pages from 'vite-plugin-pages'")) viteConfig = viteConfig.replace(/(import .*?from .*?\n)/, `$1import Pages from 'vite-plugin-pages'\n`)
|
|
20
|
-
if(!viteConfig.includes("Pages({")) viteConfig = viteConfig.replace(/plugins:\s*\[/, `plugins: [\n Pages({\n dirs: 'src/views',\n extensions: ['vue'],\n exclude: ['**/_*.vue'],\n async extendRoute(route) {\n const componentPath = path.resolve(process.cwd(), route.component.slice(1))\n const dirPath = path.dirname(componentPath)\n const metaFile = path.resolve(dirPath, 'meta.json')\n if(fs.existsSync(metaFile)) {\n try {\n const metaContent = fs.readFileSync(metaFile, 'utf-8')\n const meta = JSON.parse(metaContent)\n route.meta = { ...(route.meta || {}), ...meta }\n } catch(err) {\n console.warn(\`加载 \${metaFile} 失败:\`, err)\n }\n }\n return { ...route }\n }\n }),`)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
fs.writeFileSync(viteConfigPath, viteConfig)
|
|
24
|
-
}
|