|
| 1 | +import { transform } from '@vue/compiler-core' |
| 2 | +import Components from 'unplugin-vue-components/vite' |
| 3 | +import { isEntryFile } from '../shared/plugin/is-entry-file' |
| 4 | +import { Plugin } from 'vite' |
| 5 | +import { addImport, addVuePlugin, hasImport, hasVuePlugin } from '../shared/plugin/js' |
| 6 | +import { MagicString } from '@vue/compiler-sfc' |
| 7 | +import { logger } from '../logger' |
| 8 | +import { formatString } from '../shared/color' |
| 9 | + |
| 10 | +function installVuesticEssentialPlugin(ms: MagicString) { |
| 11 | + // Install vuestic essential plugin if not already installed |
| 12 | + if (!hasImport(ms, { named: 'createVuesticEssential', from: 'vuestic-ui' })) { |
| 13 | + addImport(ms, `import { createVuesticEssential } from 'vuestic-ui'`) |
| 14 | + } |
| 15 | + |
| 16 | + if (!hasVuePlugin(ms, 'createVuesticEssential')) { |
| 17 | + addVuePlugin(ms, 'createVuesticEssential()') |
| 18 | + } |
| 19 | + |
| 20 | + return ms |
| 21 | +} |
| 22 | + |
| 23 | +function installVuesticEssentialStyles(ms: MagicString) { |
| 24 | + // Add styles if not already imported |
| 25 | + if (!hasImport(ms, 'vuestic-ui/css')) { |
| 26 | + addImport(ms, `import 'vuestic-ui/css'`) |
| 27 | + } |
| 28 | + |
| 29 | + return ms |
| 30 | +} |
| 31 | + |
| 32 | +export const vuesticAutoImport = (options: { prefix?: string } = { prefix: 'Va' }) => { |
| 33 | + const prefix = options.prefix || 'Va' |
| 34 | + |
| 35 | + return [ |
| 36 | + { |
| 37 | + name: 'vuestic:auto-import', |
| 38 | + |
| 39 | + enforce: 'pre', |
| 40 | + |
| 41 | + transform(code, id) { |
| 42 | + if (!isEntryFile(id)) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + const ms = new MagicString(code) |
| 47 | + |
| 48 | + installVuesticEssentialPlugin(ms) |
| 49 | + installVuesticEssentialStyles(ms) |
| 50 | + |
| 51 | + return { |
| 52 | + code: ms.toString(), |
| 53 | + map: ms.generateMap({ hires: true }), |
| 54 | + } |
| 55 | + } |
| 56 | + } satisfies Plugin, |
| 57 | + Components({ |
| 58 | + dts: 'node_modules/.vuestic/components.d.ts', |
| 59 | + resolvers: [ |
| 60 | + (componentName) => { |
| 61 | + if (componentName.startsWith(prefix)) |
| 62 | + return { name: componentName, from: 'vuestic-ui' } |
| 63 | + }, |
| 64 | + ], |
| 65 | + }) as Plugin |
| 66 | + ] |
| 67 | +} |
0 commit comments