Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ vite.config.ts.timestamp*
**/.vitepress/cache/*
**/.vitepress/.temp
**/temp.json
*.tgz
11 changes: 7 additions & 4 deletions packages/module/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shadcn-nuxt",
"type": "module",
"version": "2.2.0",
"version": "2.2.1-pre.1",
"description": "Add shadcn-vue module to Nuxt",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -35,15 +35,18 @@
"pub:next": "pnpm prepack && pnpm publish --no-git-checks --access public --tag next",
"pub:release": "pnpm prepack && pnpm publish --no-git-checks --access public"
},
"peerDependencies": {
"@nuxt/kit": "^3.19.2"
},
"dependencies": {
"@nuxt/kit": "^3.17.4",
"@nuxt/kit": "^3.19.2",
"oxc-parser": "catalog:"
},
"devDependencies": {
"@nuxt/eslint-config": "^1.4.1",
"@nuxt/module-builder": "^1.0.1",
"@nuxt/schema": "^3.17.4",
"@nuxt/test-utils": "^3.19.1",
"@nuxt/schema": "^3.19.0",
"@nuxt/test-utils": "^3.19.2",
"@nuxtjs/color-mode": "^3.5.2",
"@nuxtjs/tailwindcss": "^6.14.0",
"@types/node": "^22.15.21",
Expand Down
47 changes: 36 additions & 11 deletions packages/module/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { addComponent, addComponentsDir, createResolver, defineNuxtModule } from '@nuxt/kit'
import {
addComponent,
addComponentsDir,
createResolver,
defineNuxtModule,
findPath,
getLayerDirectories,
resolvePath as resolvePathNuxt,
useLogger,
} from '@nuxt/kit'
import { parseSync } from 'oxc-parser'

// TODO: add test to make sure all registry is being parse correctly
Expand Down Expand Up @@ -32,16 +41,32 @@ export default defineNuxtModule<ModuleOptions>({
async setup({ prefix, componentDir }, nuxt) {
const COMPONENT_DIR_PATH = componentDir!
const ROOT_DIR_PATH = nuxt.options.rootDir
const { resolve, resolvePath } = createResolver(ROOT_DIR_PATH)

// Components Auto Imports
const componentsPath = await resolvePath(COMPONENT_DIR_PATH)
const logger = useLogger('shadcn-nuxt')
logger.start('Setting up shadcn-nuxt module', { COMPONENT_DIR_PATH, ROOT_DIR_PATH })
// Build list of potential component directory paths from all layers
const layerDirectories = getLayerDirectories()
const potentialPaths = await Promise.all(
layerDirectories.map((layer) => {
let layerPath = ROOT_DIR_PATH
if ('cwd' in layer && typeof layer.cwd === 'string') {
layerPath = layer.cwd
}
if ('app' in layer && typeof layer.app === 'string') {
layerPath = layer.app
}
return resolvePathNuxt(COMPONENT_DIR_PATH, { cwd: layerPath })
}),
)

// Early return if directory doesn't exist
if (!existsSync(componentsPath)) {
console.warn(`Component directory does not exist: ${componentsPath}`)
return
}
logger.info('Checking', { potentialPaths })
// Use findPath to find the first existing component directory
const componentsPath = (await findPath(potentialPaths, {}, 'dir')) || ROOT_DIR_PATH

logger.info('Decided on', { componentsPath })

// Create resolver relative to the found components path
const { resolve, resolvePath } = createResolver(componentsPath)

// Tell Nuxt to not scan `componentsDir` for auto imports as we will do it manually
// See https://github.com/unovue/shadcn-vue/pull/528#discussion_r1590206268
Expand All @@ -57,7 +82,7 @@ export default defineNuxtModule<ModuleOptions>({
try {
await Promise.all(readdirSync(componentsPath).map(async (dir) => {
try {
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
const filePath = await resolvePath(join(componentsPath, dir, 'index'), { extensions: ['.ts', '.js'] })
const content = readFileSync(filePath, { encoding: 'utf8' })
const ast = parseSync(filePath, content, {
sourceType: 'module',
Expand Down
Loading