diff --git a/package.json b/package.json index 25dabd6..deee089 100644 --- a/package.json +++ b/package.json @@ -32,9 +32,10 @@ "dev": "nuxi dev playground", "dev:build": "nuxi build playground", "dev:generate": "nuxi generate playground", - "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground", + "dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground", "lint": "eslint ", - "test": "vitest", + "test": "vitest run", + "verify": "pnpm dev:prepare && pnpm lint && pnpm test", "release": "pnpm test && release-it" }, "dependencies": { diff --git a/src/parser.ts b/src/parser.ts index 49fec09..88d8289 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -31,6 +31,10 @@ export function useComponentMetaParser ( ) { // const logger = consola.withScope('nuxt-component-meta') + /** + * Initialize component data object from components + */ + let components: NuxtComponentMeta = { ...metaSources } const outputPath = join(outputDir, 'component-meta') const isExcluded = (component: any) => { @@ -70,26 +74,17 @@ export function useComponentMetaParser ( { extends: `${rootDir}/tsconfig.json`, skipLibCheck: true, - include: [ - '**/*', - ...componentDirs.map((dir) => { - const path = typeof dir === 'string' ? dir : (dir?.path || '') - if (path.endsWith('.vue')) { - return path - } - return `${path}/**/*` - }) - ], + include: componentDirs.map((dir) => { + const path = typeof dir === 'string' ? dir : (dir?.path || '') + const ext = path.split('.').pop()! + return ['vue', 'ts', 'tsx', 'js', 'jsx'].includes(ext) ? path : `${path}/**/*` + }), exclude: [] }, checkerOptions ) } - /** - * Initialize component data object from components - */ - const components: NuxtComponentMeta = { ...metaSources } const init = async () => { const meta = await import(outputPath + '.mjs').then((m) => m.default || m).catch(() => null) @@ -171,7 +166,7 @@ export function useComponentMetaParser ( if (component.meta.hash && component.fullPath.includes('/node_modules/')) { // We assume that components from node_modules don't change - return + return } // Read component code @@ -269,6 +264,13 @@ export function useComponentMetaParser ( return { get checker () { return checker }, get components () { return components }, + dispose() { + checker.clearCache() + // @ts-expect-error - Remove checker + checker = null + // Clear components cache + components = {} + }, init, refreshChecker, stubOutput, diff --git a/src/unplugin.ts b/src/unplugin.ts index ef97c4f..54238a0 100644 --- a/src/unplugin.ts +++ b/src/unplugin.ts @@ -5,7 +5,7 @@ type ComponentMetaUnpluginOptions = { parser?: ComponentMetaParser, parserOption // @ts-ignore -- arguments types are not correct export const metaPlugin = createUnplugin(({ parser, parserOptions }) => { - const instance = parser || useComponentMetaParser(parserOptions) + let instance = parser || useComponentMetaParser(parserOptions) let _configResolved: any return { @@ -17,15 +17,22 @@ export const metaPlugin = createUnplugin(({ parser return } - instance.fetchComponents() - instance.updateOutput() + instance?.fetchComponents() + instance?.updateOutput() + }, + buildEnd () { + if (!_configResolved?.env.DEV && _configResolved?.env.PROD) { + instance?.dispose() + // @ts-expect-error -- Remove instance from memory + instance = null + } }, vite: { configResolved (config) { _configResolved = config }, handleHotUpdate ({ file }) { - if (Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) { + if (instance && Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) { instance.fetchComponent(file) instance.updateOutput() }