feat: use oxc-parser to get ExportNamedDeclaration node
This commit is contained in:
parent
641344dcf3
commit
228b0b970e
|
|
@ -1,7 +1,7 @@
|
||||||
import { readFileSync, readdirSync } from 'node:fs'
|
import { readFileSync, readdirSync } from 'node:fs'
|
||||||
import { join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
import { addComponent, createResolver, defineNuxtModule } from '@nuxt/kit'
|
import { addComponent, createResolver, defineNuxtModule } from '@nuxt/kit'
|
||||||
import { parse } from 'recast'
|
import oxc from 'oxc-parser'
|
||||||
|
|
||||||
// Module options TypeScript interface definition
|
// Module options TypeScript interface definition
|
||||||
export interface ModuleOptions {
|
export interface ModuleOptions {
|
||||||
|
|
@ -40,11 +40,16 @@ export default defineNuxtModule<ModuleOptions>({
|
||||||
try {
|
try {
|
||||||
readdirSync(resolve(COMPONENT_DIR_PATH))
|
readdirSync(resolve(COMPONENT_DIR_PATH))
|
||||||
.forEach(async (dir) => {
|
.forEach(async (dir) => {
|
||||||
|
try {
|
||||||
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
|
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
|
||||||
const content = readFileSync(filePath, { encoding: 'utf8' })
|
const content = readFileSync(filePath, { encoding: 'utf8' })
|
||||||
const ast = parse(content)
|
const ast = oxc.parseSync(content, {
|
||||||
|
sourceType: 'module',
|
||||||
|
sourceFilename: filePath,
|
||||||
|
})
|
||||||
|
const program = JSON.parse(ast.program)
|
||||||
|
|
||||||
const exportedKeys: string[] = ast.program.body
|
const exportedKeys: string[] = program.body
|
||||||
// @ts-expect-error parse return any
|
// @ts-expect-error parse return any
|
||||||
.filter(node => node.type === 'ExportNamedDeclaration')
|
.filter(node => node.type === 'ExportNamedDeclaration')
|
||||||
// @ts-expect-error parse return any
|
// @ts-expect-error parse return any
|
||||||
|
|
@ -58,6 +63,11 @@ export default defineNuxtModule<ModuleOptions>({
|
||||||
filePath: resolve(filePath),
|
filePath: resolve(filePath),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if (err instanceof Error)
|
||||||
|
console.warn('Module error: ', err.message)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user