From bd73c6447b1feeb6082dde6729c4f41e04a4b22e Mon Sep 17 00:00:00 2001 From: MuhammadM1998 Date: Sat, 4 May 2024 19:55:51 +0300 Subject: [PATCH] feat(module): auto generate `lib/utils` --- packages/module/src/module.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/module/src/module.ts b/packages/module/src/module.ts index f3f8df20..ec9d71af 100644 --- a/packages/module/src/module.ts +++ b/packages/module/src/module.ts @@ -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 // Module options TypeScript interface definition @@ -26,9 +27,24 @@ export default defineNuxtModule({ async setup({ prefix, componentDir }, nuxt) { const COMPONENT_DIR_PATH = componentDir! 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 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) nuxt.hook('components:dirs', (dirs) => { dirs.unshift({