feat(module): auto generate lib/utils
This commit is contained in:
parent
bddb61a1ea
commit
bd73c6447b
|
|
@ -1,4 +1,5 @@
|
||||||
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
import { addTemplate, createResolver, defineNuxtModule, findPath, useLogger } from '@nuxt/kit'
|
||||||
|
import { UTILS } from '../../cli/src/utils/templates'
|
||||||
|
|
||||||
// TODO: add test to make sure all registry is being parse correctly
|
// TODO: add test to make sure all registry is being parse correctly
|
||||||
// Module options TypeScript interface definition
|
// Module options TypeScript interface definition
|
||||||
|
|
@ -26,9 +27,24 @@ export default defineNuxtModule<ModuleOptions>({
|
||||||
async setup({ prefix, componentDir }, nuxt) {
|
async setup({ prefix, componentDir }, nuxt) {
|
||||||
const COMPONENT_DIR_PATH = componentDir!
|
const COMPONENT_DIR_PATH = componentDir!
|
||||||
const ROOT_DIR_PATH = nuxt.options.rootDir
|
const ROOT_DIR_PATH = nuxt.options.rootDir
|
||||||
|
const UTILS_ALIAS = '@/lib/utils' // Use the same path from the cli for backward compatibility
|
||||||
const { resolve } = createResolver(ROOT_DIR_PATH)
|
const { resolve } = createResolver(ROOT_DIR_PATH)
|
||||||
|
const logger = useLogger('shadcn-nuxt')
|
||||||
|
|
||||||
// Register components
|
// `lib/utils`
|
||||||
|
const utilsTemplate = addTemplate({
|
||||||
|
filename: 'shadcn-nuxt/utils.ts',
|
||||||
|
getContents: () => UTILS,
|
||||||
|
write: true,
|
||||||
|
})
|
||||||
|
nuxt.options.alias = { [UTILS_ALIAS]: utilsTemplate.dst, ...nuxt.options.alias } // We add our alias to the top to avoid conflicts with nuxt built in `@` alias
|
||||||
|
|
||||||
|
// Recommend to remove the root `lib/utils` if exists
|
||||||
|
const isRootUtilsExists = await findPath('./lib/utils.ts', { cwd: ROOT_DIR_PATH })
|
||||||
|
if (isRootUtilsExists)
|
||||||
|
logger.warn('[shadcn-nuxt] `lib/utils.ts` is auto generated by the module and can be safely removed.')
|
||||||
|
|
||||||
|
// Components Auto Imports
|
||||||
const componentsPath = resolve(COMPONENT_DIR_PATH)
|
const componentsPath = resolve(COMPONENT_DIR_PATH)
|
||||||
nuxt.hook('components:dirs', (dirs) => {
|
nuxt.hook('components:dirs', (dirs) => {
|
||||||
dirs.unshift({
|
dirs.unshift({
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user