Skip to content

Commit 8644efb

Browse files
authored
perf: include less files in typescript checker (#82)
1 parent 48cee62 commit 8644efb

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
"dev": "nuxi dev playground",
3333
"dev:build": "nuxi build playground",
3434
"dev:generate": "nuxi generate playground",
35-
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
35+
"dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground",
3636
"lint": "eslint ",
37-
"test": "vitest",
37+
"test": "vitest run",
38+
"verify": "pnpm dev:prepare && pnpm lint && pnpm test",
3839
"release": "pnpm test && release-it"
3940
},
4041
"dependencies": {

src/parser.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export function useComponentMetaParser (
3131
) {
3232
// const logger = consola.withScope('nuxt-component-meta')
3333

34+
/**
35+
* Initialize component data object from components
36+
*/
37+
let components: NuxtComponentMeta = { ...metaSources }
3438
const outputPath = join(outputDir, 'component-meta')
3539

3640
const isExcluded = (component: any) => {
@@ -70,26 +74,17 @@ export function useComponentMetaParser (
7074
{
7175
extends: `${rootDir}/tsconfig.json`,
7276
skipLibCheck: true,
73-
include: [
74-
'**/*',
75-
...componentDirs.map((dir) => {
76-
const path = typeof dir === 'string' ? dir : (dir?.path || '')
77-
if (path.endsWith('.vue')) {
78-
return path
79-
}
80-
return `${path}/**/*`
81-
})
82-
],
77+
include: componentDirs.map((dir) => {
78+
const path = typeof dir === 'string' ? dir : (dir?.path || '')
79+
const ext = path.split('.').pop()!
80+
return ['vue', 'ts', 'tsx', 'js', 'jsx'].includes(ext) ? path : `${path}/**/*`
81+
}),
8382
exclude: []
8483
},
8584
checkerOptions
8685
)
8786
}
8887

89-
/**
90-
* Initialize component data object from components
91-
*/
92-
const components: NuxtComponentMeta = { ...metaSources }
9388
const init = async () => {
9489
const meta = await import(outputPath + '.mjs').then((m) => m.default || m).catch(() => null)
9590

@@ -171,7 +166,7 @@ export function useComponentMetaParser (
171166

172167
if (component.meta.hash && component.fullPath.includes('/node_modules/')) {
173168
// We assume that components from node_modules don't change
174-
return
169+
return
175170
}
176171

177172
// Read component code
@@ -269,6 +264,13 @@ export function useComponentMetaParser (
269264
return {
270265
get checker () { return checker },
271266
get components () { return components },
267+
dispose() {
268+
checker.clearCache()
269+
// @ts-expect-error - Remove checker
270+
checker = null
271+
// Clear components cache
272+
components = {}
273+
},
272274
init,
273275
refreshChecker,
274276
stubOutput,

src/unplugin.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type ComponentMetaUnpluginOptions = { parser?: ComponentMetaParser, parserOption
55

66
// @ts-ignore -- arguments types are not correct
77
export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ parser, parserOptions }) => {
8-
const instance = parser || useComponentMetaParser(parserOptions)
8+
let instance = parser || useComponentMetaParser(parserOptions)
99
let _configResolved: any
1010

1111
return {
@@ -17,15 +17,22 @@ export const metaPlugin = createUnplugin<ComponentMetaUnpluginOptions>(({ parser
1717
return
1818
}
1919

20-
instance.fetchComponents()
21-
instance.updateOutput()
20+
instance?.fetchComponents()
21+
instance?.updateOutput()
22+
},
23+
buildEnd () {
24+
if (!_configResolved?.env.DEV && _configResolved?.env.PROD) {
25+
instance?.dispose()
26+
// @ts-expect-error -- Remove instance from memory
27+
instance = null
28+
}
2229
},
2330
vite: {
2431
configResolved (config) {
2532
_configResolved = config
2633
},
2734
handleHotUpdate ({ file }) {
28-
if (Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) {
35+
if (instance && Object.entries(instance.components).some(([, comp]: any) => comp.fullPath === file)) {
2936
instance.fetchComponent(file)
3037
instance.updateOutput()
3138
}

0 commit comments

Comments
 (0)