Skip to content

Commit 411ebcc

Browse files
antfubenjamincanac
andauthored
fix(vite): write theme templates (#5355)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
1 parent 4cb0638 commit 411ebcc

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

docs/content/docs/1.getting-started/2.installation/2.vue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Internally, Nuxt UI relies on custom alias to resolve the theme types. If you're
138138
"compilerOptions": {
139139
"paths": {
140140
"#build/ui": [
141-
"./node_modules/@nuxt/ui/.nuxt/ui"
141+
"./node_modules/.nuxt-ui/ui"
142142
]
143143
}
144144
}

package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,12 @@
7777
]
7878
}
7979
},
80-
"imports": {
81-
"#build/ui/*": "./.nuxt/ui/*.ts",
82-
"#build/ui.css": "./.nuxt/ui.css"
83-
},
8480
"bin": {
8581
"nuxt-ui": "./cli/index.mjs"
8682
},
8783
"style": "./dist/runtime/index.css",
8884
"main": "./dist/module.mjs",
8985
"files": [
90-
".nuxt/ui",
91-
".nuxt/ui.css",
9286
"dist",
9387
"cli",
9488
"vue-plugin.d.ts"

playgrounds/vue/tsconfig.node.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
"strict": true,
1717
"noUnusedLocals": true,
1818
"noUnusedParameters": true,
19-
"noFallthroughCasesInSwitch": true
19+
"noFallthroughCasesInSwitch": true,
20+
21+
/* Aliases */
22+
"paths": {
23+
"#build/ui": [
24+
"./node_modules/.nuxt-ui/ui"
25+
]
26+
}
2027
},
2128
"include": ["vite.config.ts"]
2229
}

src/plugins/templates.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
13
import type { UnpluginOptions } from 'unplugin'
24
import type { NuxtUIOptions } from '../unplugin'
35
import { getTemplates } from '../templates'
@@ -10,9 +12,38 @@ export default function TemplatePlugin(options: NuxtUIOptions, appConfig: Record
1012
const templates = getTemplates(options, appConfig.ui)
1113
const templateKeys = new Set(templates.map(t => `#build/${t.filename}`))
1214

15+
async function writeTemplates(root: string) {
16+
const map: Record<string, string> = {}
17+
const dir = path.join(root, 'node_modules', '.nuxt-ui')
18+
for (const template of templates) {
19+
if (!template.write || !template.filename) {
20+
continue
21+
}
22+
const filePath = path.join(dir, template.filename)
23+
if (!fs.existsSync(path.dirname(filePath))) {
24+
fs.mkdirSync(path.dirname(filePath), { recursive: true })
25+
}
26+
fs.writeFileSync(filePath, await template.getContents!({} as any))
27+
28+
map[`#build/${template.filename}`] = filePath
29+
}
30+
return map
31+
}
32+
1333
return {
1434
name: 'nuxt:ui:templates',
1535
enforce: 'pre',
36+
vite: {
37+
async config(config) {
38+
const alias = await writeTemplates(config.root || process.cwd())
39+
40+
return {
41+
resolve: {
42+
alias
43+
}
44+
}
45+
}
46+
},
1647
resolveId(id) {
1748
if (templateKeys.has(id + '.ts')) {
1849
return id.replace('#build/', 'virtual:nuxt-ui-templates/') + '.ts'

0 commit comments

Comments
 (0)