-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuxt.ts
103 lines (94 loc) · 3.55 KB
/
nuxt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { DeepPartial } from "@alanscodelog/utils"
import { isArray } from "@alanscodelog/utils/isArray.js"
import { addComponent, addImports, addPlugin, createResolver, defineNuxtModule } from "@nuxt/kit"
import { defu } from "defu"
import { glob } from "fast-glob"
import path from "path"
import { type Config } from "tailwindcss"
import { theme } from "./src/theme.js"
import config from "./tailwind.config.js"
const knownDirectives = ["vExtractRootEl", "vResizableCols", "vResizeObserver", "vResizableCols"] as const
type Options = {
directives: (typeof knownDirectives[number])[] | "all"
}
// @ts-expect-error it does work...
declare module "nuxt/schema" {
interface NuxtOptions {
witchcraftComponents: Options
}
interface RuntimeConfig {
witchcraftComponents: Options
}
}
export default defineNuxtModule<Options>({
meta: {
name: "@alanscodelog/vue-components",
configKey: "witchcraftComponents",
},
defaults: {
directives: [],
},
async setup(opts, nuxt) {
const unknownDirectives = isArray(opts.directives) ? opts.directives.filter(_ => !knownDirectives.includes(_)) : []
if (unknownDirectives.length > 0 || (!isArray(opts.directives) && opts.directives !== "all")) {
throw new Error(`Witchcraft Components: Directives list contains unknown directives: ${unknownDirectives.join(",")}`)
}
nuxt.options.runtimeConfig.public.witchcraftComponents = defu(nuxt.options.runtimeConfig.public.witchcraftComponents as any, {
directives: opts.directives === "all" ? null : opts.directives,
})
const { resolve } = createResolver(import.meta.url)
const components = await glob(`${resolve("src/components")}/**/*.vue`, { onlyFiles: true, absolute: false })
await Promise.all(components.map(async component => addComponent({
filePath: component,
name: path.parse(component).name,
})))
nuxt.hook("tailwindcss:config" as any, (twConfig: DeepPartial<Config>) => {
twConfig.plugins = [...(twConfig.plugins ?? []), ...config.plugins]
twConfig.darkMode = config.darkMode
twConfig.theme ??= {}
twConfig.theme.configViewer = {
// https://github.com/rogden/tailwind-config-viewer/issues/84
themeReplacements: Object.fromEntries(
Object.entries(theme.css)
.map(([key, value]) => [`rgb(var(${key}) / <alpha-value>)`, `rgb(${value})`]),
),
}
twConfig.content ??= []
// todo check am not overriding anything wrong
;(twConfig.content as string[]).push(resolve("./src/**/*.vue"))
})
addImports([
{
name: "twMerge",
from: resolve("./src/helpers/twMerge.ts"),
},
{
name: "vDetectFlex",
from: resolve("./src/directives/vDetectFlex.ts"),
},
{
name: "vExtractRootEl",
from: resolve("./src/directives/vExtractRootEl.ts"),
},
{
name: "vResizableCols",
from: resolve("./src/directives/vResizableCols.ts"),
},
])
// console.log((await glob(`${resolve("src/composables")}/*.ts`, { onlyFiles: true, absolute: false, ignore: ["**/*/index.ts"]})))
await Promise.all(
(await glob(`${resolve("src/composables")}/*.ts`, { onlyFiles: true, absolute: false, ignore: ["**/*/index.ts"]}))
.map(async _ => addImports({
from: _,
name: path.parse(_).name,
})))
// console.log((await glob(`${resolve("src/directives")}/*.ts`, { onlyFiles: true, absolute: false, ignore: ["**/*/index.ts"]})))
await Promise.all(
(await glob(`${resolve("src/directives")}/*.ts`, { onlyFiles: true, absolute: false, ignore: ["**/*/index.ts"]}))
.map(async _ => addImports({
from: _,
name: path.parse(_).name,
})))
addPlugin(resolve("./src/nuxt/plugins/vue-plugin.ts"))
},
})