Skip to content

Commit d43db8f

Browse files
committed
workflow(dev): support 'unplugin-vue2-script-setup' for vue 2.6
1 parent 82bc32a commit d43db8f

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

scripts/dev.mts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
// pnpm add prompts cross-spawn kolorist magicast -D -w
2+
13
import fs from 'node:fs'
24
import { execSync } from 'node:child_process'
35
import prompts from 'prompts'
46
import spawn from 'cross-spawn'
57
import { loadFile, writeFile } from 'magicast'
6-
import type { ASTNode, ProxifiedImportItem } from 'magicast'
8+
import type { ASTNode } from 'magicast'
79
import { cyan } from 'kolorist'
10+
import { addVitePlugin } from 'magicast/helpers'
811

912
type VueVersion = '3' | '2.7' | '2.6'
1013

@@ -33,6 +36,7 @@ const vueVersionToDeps: Record<VueVersion, Record<string, string>> = {
3336
'@vue/composition-api': 'latest',
3437
'@vue/test-utils': 'legacy',
3538
'vite-plugin-vue2': 'latest',
39+
'unplugin-vue2-script-setup': 'latest',
3640
'vue': '~2.6.14',
3741
'vue-template-compiler': '~2.6.14',
3842
},
@@ -50,45 +54,62 @@ async function dev() {
5054
return
5155
}
5256

53-
const { shouldUpgradeDependencies } = await prompts({
57+
/* const { shouldUpgradeDependencies } = await prompts({
5458
type: 'confirm',
5559
name: 'shouldUpgradeDependencies',
5660
message: 'Upgrade dependencies',
57-
})
61+
}) */
5862

5963
console.log(cyan('Fetching origin...'))
6064
spawn.sync('git', ['pull'], { stdio: 'inherit' })
6165

6266
console.log(cyan(`Switching to Vue ${targetVersion}...`))
63-
6467
const mod = await loadFile('./vite.config.ts')
6568

66-
let isViteConfigChanged = false
69+
// imported 表示命名导入的值,默认导入是 default
70+
// k 和 mod.imports[k].local 和 constructor 三者一致,表示导入取的别名
6771

72+
// 删掉 vue 相关引入
73+
const existedVuePlugins: Record<string, boolean> = {}
6874
for (const k in mod.imports) {
6975
for (const vueVersion in vueVersionToVitePlugin) {
70-
if (mod.imports[k].from === vueVersionToVitePlugin[vueVersion as VueVersion] && targetVersion !== vueVersion) {
76+
if (mod.imports[k] && [vueVersionToVitePlugin[vueVersion as VueVersion], 'unplugin-vue2-script-setup/vite'].includes(mod.imports[k].from)) {
7177
delete mod.imports[k]
72-
isViteConfigChanged = true
73-
break
78+
existedVuePlugins[k] = true
7479
}
7580
}
7681
}
7782

78-
if (!mod.imports.vue) {
79-
mod.imports.vue = {
80-
imported: targetVersion === '2.6' ? 'createVuePlugin' : 'default',
81-
local: 'vue',
82-
from: vueVersionToVitePlugin[targetVersion],
83-
} as ProxifiedImportItem
84-
isViteConfigChanged = true
83+
// 删掉 vue 相关插件
84+
const options = mod.exports.default.$type === 'function-call'
85+
? mod.exports.default.$args[0]
86+
: mod.exports.default
87+
if (Object.keys(existedVuePlugins).length && options.plugins?.length) {
88+
for (let i = options.plugins.length - 1; i > 0; i--) {
89+
const p = options.plugins[i]
90+
if (p?.$type === 'function-call' && existedVuePlugins[p.$callee]) {
91+
options.plugins.splice(i, 1)
92+
}
93+
}
8594
}
8695

87-
if (isViteConfigChanged) {
88-
await writeFile(mod as unknown as ASTNode, './vite.config.ts')
89-
spawn.sync('npx', ['eslint', './vite.config.ts', '--fix'], { stdio: 'inherit' })
96+
// 添加 vue 相关插件
97+
addVitePlugin(mod, {
98+
from: vueVersionToVitePlugin[targetVersion],
99+
imported: targetVersion === '2.6' ? 'createVuePlugin' : 'default',
100+
constructor: 'vue',
101+
})
102+
if (targetVersion === '2.6') {
103+
addVitePlugin(mod, {
104+
from: 'unplugin-vue2-script-setup/vite',
105+
imported: 'default',
106+
constructor: 'ScriptSetup',
107+
})
90108
}
91109

110+
await writeFile(mod as unknown as ASTNode, './vite.config.ts')
111+
spawn.sync('npx', ['eslint', './vite.config.ts', '--fix'], { stdio: 'inherit' })
112+
92113
let isDepsChanged = false
93114

94115
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
@@ -118,18 +139,18 @@ async function dev() {
118139
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))
119140
console.log(cyan('Linting package.json...'))
120141
spawn.sync('npx', ['eslint', './package.json', '--fix'], { stdio: 'inherit' })
121-
if (!shouldUpgradeDependencies) {
122-
installDependencies()
123-
}
142+
// if (!shouldUpgradeDependencies) {
143+
await installDependencies()
144+
// }
124145
}
125146

126-
if (shouldUpgradeDependencies) {
147+
/* if (shouldUpgradeDependencies) {
127148
installDependencies()
128-
}
149+
} */
129150

130151
spawn.sync('npx', ['vite', '--open'], { stdio: 'inherit' })
131152

132-
function installDependencies() {
153+
async function installDependencies() {
133154
console.log(cyan('Checking pnpm version...'))
134155
const latestPNPMVersion = spawn.sync('npm', ['view', 'pnpm', 'version']).stdout.toString().trim()
135156
const currentPNPMVersion = spawn.sync('pnpm', ['-v']).stdout.toString().trim()

0 commit comments

Comments
 (0)