|
1 | | -import type { Feature } from './types' |
| 1 | +import type { Feature, FeatureContext } from './types' |
2 | 2 | import { existsSync, rmSync } from 'node:fs' |
3 | 3 | import { readFile, writeFile } from 'node:fs/promises' |
4 | 4 | import { loadFile } from 'magicast' |
5 | 5 | import { addNuxtModule, addVitePlugin } from 'magicast/helpers' |
6 | 6 | import { join } from 'pathe' |
7 | 7 | import rootPkg from './dependencies/package.json' with { type: 'json' } |
8 | 8 |
|
9 | | -export const unocss: Feature = { |
10 | | - name: 'unocss', |
11 | | - apply: async ({ cwd, pkg, isTypescript, isNuxt }) => { |
12 | | - // Add UnoCSS dependencies |
13 | | - pkg.devDependencies = pkg.devDependencies || {} |
14 | | - pkg.devDependencies['unocss'] = rootPkg.dependencies['unocss'] |
| 9 | +async function applyUnocssBase ( |
| 10 | + { cwd, pkg, isTypescript, isNuxt }: FeatureContext, |
| 11 | + options: { presetVuetify?: boolean } = {}, |
| 12 | +) { |
| 13 | + pkg.devDependencies = pkg.devDependencies || {} |
| 14 | + pkg.devDependencies['unocss'] = rootPkg.dependencies['unocss'] |
| 15 | + pkg.devDependencies['@unocss/transformer-directives'] = rootPkg.dependencies['@unocss/transformer-directives'] |
| 16 | + if (options.presetVuetify) { |
| 17 | + pkg.devDependencies['unocss-preset-vuetify'] = rootPkg.dependencies['unocss-preset-vuetify'] |
| 18 | + } |
15 | 19 |
|
16 | | - const tailwindCss = isNuxt ? join(cwd, 'app/assets/css/tailwind.css') : join(cwd, 'src/tailwind.css') |
| 20 | + const tailwindCss = isNuxt ? join(cwd, 'app/assets/css/tailwind.css') : join(cwd, 'src/tailwind.css') |
17 | 21 |
|
18 | | - if (existsSync(tailwindCss)) { |
19 | | - rmSync(tailwindCss) |
20 | | - } |
| 22 | + if (existsSync(tailwindCss)) { |
| 23 | + rmSync(tailwindCss) |
| 24 | + } |
21 | 25 |
|
22 | | - // Remove Tailwind dependencies |
23 | | - if (pkg.devDependencies) { |
24 | | - delete pkg.devDependencies['tailwindcss'] |
25 | | - delete pkg.devDependencies['@tailwindcss/vite'] |
26 | | - delete pkg.devDependencies['@nuxtjs/tailwindcss'] |
27 | | - } |
| 26 | + if (pkg.devDependencies) { |
| 27 | + delete pkg.devDependencies['tailwindcss'] |
| 28 | + delete pkg.devDependencies['@tailwindcss/vite'] |
| 29 | + delete pkg.devDependencies['@nuxtjs/tailwindcss'] |
| 30 | + } |
| 31 | + |
| 32 | + if (isNuxt) { |
| 33 | + pkg.devDependencies['@unocss/nuxt'] = rootPkg.dependencies['@unocss/nuxt'] |
| 34 | + |
| 35 | + const configPath = join(cwd, 'nuxt.config.ts') |
| 36 | + const mod = await loadFile(configPath) |
| 37 | + addNuxtModule(mod, '@unocss/nuxt') |
| 38 | + |
| 39 | + await writeFile(configPath, mod.generate().code) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + const ext = isTypescript ? 'ts' : 'js' |
| 44 | + const viteConfigPath = join(cwd, `vite.config.m${ext}`) |
| 45 | + const mod = await loadFile(viteConfigPath) |
| 46 | + |
| 47 | + addVitePlugin(mod, { |
| 48 | + from: 'unocss/vite', |
| 49 | + constructor: 'UnoCSS', |
| 50 | + }) |
| 51 | + |
| 52 | + await writeFile(viteConfigPath, mod.generate().code) |
28 | 53 |
|
29 | | - if (isNuxt) { |
30 | | - pkg.devDependencies['@unocss/nuxt'] = rootPkg.dependencies['@unocss/nuxt'] |
31 | | - |
32 | | - const configPath = join(cwd, 'nuxt.config.ts') |
33 | | - const mod = await loadFile(configPath) |
34 | | - addNuxtModule(mod, '@unocss/nuxt') |
35 | | - |
36 | | - await writeFile(configPath, mod.generate().code) |
37 | | - } else { |
38 | | - const ext = isTypescript ? 'ts' : 'js' |
39 | | - const viteConfigPath = join(cwd, `vite.config.m${ext}`) |
40 | | - const mod = await loadFile(viteConfigPath) |
41 | | - |
42 | | - addVitePlugin(mod, { |
43 | | - from: 'unocss/vite', |
44 | | - constructor: 'UnoCSS', |
45 | | - }) |
46 | | - |
47 | | - await writeFile(viteConfigPath, mod.generate().code) |
48 | | - |
49 | | - // Add import to main.ts |
50 | | - // Try src/main.ts or src/index.ts |
51 | | - const mainFiles = ['src/main.ts', 'src/main.js'] |
52 | | - for (const file of mainFiles) { |
53 | | - const filePath = join(cwd, file) |
54 | | - if (existsSync(filePath)) { |
55 | | - const content = await readFile(filePath, 'utf8') |
56 | | - await writeFile(filePath, content.replace(/\/\/ Styles/g, '// Styles\nimport \'virtual:uno.css\'')) |
57 | | - break |
58 | | - } |
59 | | - } |
| 54 | + const mainFiles = ['src/main.ts', 'src/main.js'] |
| 55 | + for (const file of mainFiles) { |
| 56 | + const filePath = join(cwd, file) |
| 57 | + if (existsSync(filePath)) { |
| 58 | + const content = await readFile(filePath, 'utf8') |
| 59 | + await writeFile(filePath, content.replace(/\/\/ Styles/g, '// Styles\nimport \'virtual:uno.css\'')) |
| 60 | + break |
60 | 61 | } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export const unocss: Feature = { |
| 66 | + name: 'unocss', |
| 67 | + apply: async ctx => { |
| 68 | + await applyUnocssBase(ctx) |
| 69 | + }, |
| 70 | +} |
| 71 | + |
| 72 | +export const unocssWind4: Feature = { |
| 73 | + name: 'unocss-wind4', |
| 74 | + apply: async ctx => { |
| 75 | + await applyUnocssBase(ctx) |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +export const unocssVuetify: Feature = { |
| 80 | + name: 'unocss-vuetify', |
| 81 | + apply: async ctx => { |
| 82 | + await applyUnocssBase(ctx, { presetVuetify: true }) |
61 | 83 | }, |
62 | 84 | } |
0 commit comments