diff --git a/packages/module/src/module.ts b/packages/module/src/module.ts index efbf7369..f3f8df20 100644 --- a/packages/module/src/module.ts +++ b/packages/module/src/module.ts @@ -1,7 +1,4 @@ -import { readFileSync, readdirSync } from 'node:fs' -import { join } from 'node:path' -import { addComponent, createResolver, defineNuxtModule } from '@nuxt/kit' -import { parseSync } from '@oxc-parser/wasm' +import { createResolver, defineNuxtModule } from '@nuxt/kit' // TODO: add test to make sure all registry is being parse correctly // Module options TypeScript interface definition @@ -29,52 +26,16 @@ export default defineNuxtModule({ async setup({ prefix, componentDir }, nuxt) { const COMPONENT_DIR_PATH = componentDir! const ROOT_DIR_PATH = nuxt.options.rootDir - const { resolve, resolvePath } = createResolver(ROOT_DIR_PATH) + const { resolve } = createResolver(ROOT_DIR_PATH) - // --- Register components --- - // Tell Nuxt to not scan `componentsDir` for auto imports as we will do it manually + // Register components + const componentsPath = resolve(COMPONENT_DIR_PATH) nuxt.hook('components:dirs', (dirs) => { dirs.unshift({ - path: resolve(COMPONENT_DIR_PATH), - extensions: [], + path: componentsPath, + extensions: ['.vue'], // Scan `.vue` only and skip `.ts` files as we use them inside components file only + prefix, }) }) - - // Manually scan `componentsDir` for components and register them for auto imports - try { - readdirSync(resolve(COMPONENT_DIR_PATH)) - .forEach(async (dir) => { - try { - const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] }) - const content = readFileSync(filePath, { encoding: 'utf8' }) - const ast = parseSync(content, { - sourceType: 'module', - sourceFilename: filePath, - }) - - const exportedKeys: string[] = ast.program.body - .filter(node => node.type === 'ExportNamedDeclaration') - // @ts-expect-error parse return any - .flatMap(node => node.specifiers.map(specifier => specifier.exported.name)) - .filter((key: string) => /^[A-Z]/.test(key)) - - exportedKeys.forEach((key) => { - addComponent({ - name: `${prefix}${key}`, // name of the component to be used in vue templates - export: key, // (optional) if the component is a named (rather than default) export - filePath: resolve(filePath), - }) - }) - } - catch (err) { - if (err instanceof Error) - console.warn('Module error: ', err.message) - } - }) - } - catch (err) { - if (err instanceof Error) - console.warn(err.message) - } }, })