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,24 +40,34 @@ export default defineNuxtModule<ModuleOptions>({
|
||||||
try {
|
try {
|
||||||
readdirSync(resolve(COMPONENT_DIR_PATH))
|
readdirSync(resolve(COMPONENT_DIR_PATH))
|
||||||
.forEach(async (dir) => {
|
.forEach(async (dir) => {
|
||||||
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
|
try {
|
||||||
const content = readFileSync(filePath, { encoding: 'utf8' })
|
const filePath = await resolvePath(join(COMPONENT_DIR_PATH, dir, 'index'), { extensions: ['.ts', '.js'] })
|
||||||
const ast = parse(content)
|
const content = readFileSync(filePath, { encoding: 'utf8' })
|
||||||
|
const ast = oxc.parseSync(content, {
|
||||||
const exportedKeys: string[] = ast.program.body
|
sourceType: 'module',
|
||||||
// @ts-expect-error parse return any
|
sourceFilename: filePath,
|
||||||
.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),
|
|
||||||
})
|
})
|
||||||
})
|
const program = JSON.parse(ast.program)
|
||||||
|
|
||||||
|
const exportedKeys: string[] = program.body
|
||||||
|
// @ts-expect-error parse return any
|
||||||
|
.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) {
|
catch (err) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user