chore: improve building registry

This commit is contained in:
zernonia 2024-01-16 20:28:17 +08:00
parent 19293c300d
commit cba25e7390
3 changed files with 39 additions and 25 deletions

View File

@ -54,6 +54,7 @@
"@vue/tsconfig": "^0.5.1", "@vue/tsconfig": "^0.5.1",
"autoprefixer": "^10.4.16", "autoprefixer": "^10.4.16",
"lodash.template": "^4.5.0", "lodash.template": "^4.5.0",
"oxc-parser": "^0.2.0",
"pathe": "^1.1.2", "pathe": "^1.1.2",
"rimraf": "^5.0.5", "rimraf": "^5.0.5",
"tailwind-merge": "^2.0.0", "tailwind-merge": "^2.0.0",

View File

@ -1,6 +1,7 @@
import { readFile, readdir } from 'node:fs/promises' import { readFile, readdir } from 'node:fs/promises'
import { join, normalize, resolve } from 'pathe' import { join, normalize, resolve } from 'pathe'
import { compileScript, parse } from 'vue/compiler-sfc' import { compileScript, parse } from 'vue/compiler-sfc'
import oxc from 'oxc-parser'
import type { Registry } from '../../lib/registry' import type { Registry } from '../../lib/registry'
@ -134,31 +135,47 @@ async function buildUIRegistry(componentPath: string, componentName: string) {
async function getDependencies(filename: string) { async function getDependencies(filename: string) {
const code = await readFile(filename, { encoding: 'utf8' }) const code = await readFile(filename, { encoding: 'utf8' })
const parsed = parse(code, { filename })
const registryDependencies = new Set<string>() const registryDependencies = new Set<string>()
const dependencies = new Set<string>() const dependencies = new Set<string>()
if (parsed.descriptor.script?.content || parsed.descriptor.scriptSetup?.content) { const populateDeps = (source: string) => {
const compiled = compileScript(parsed.descriptor, { id: '' }) const peerDeps = DEPENDENCIES.get(source)
const taggedDeps = DEPENDENCIES_WITH_TAGS.get(source)
if (peerDeps !== undefined) {
if (taggedDeps !== undefined)
dependencies.add(taggedDeps)
else
dependencies.add(source)
peerDeps.forEach(dep => dependencies.add(dep))
}
Object.values(compiled.imports!).forEach((value) => { if (source.startsWith(REGISTRY_DEPENDENCY)) {
const source = value.source const component = source.split('/').at(-1)!
const peerDeps = DEPENDENCIES.get(source) registryDependencies.add(component)
const taggedDeps = DEPENDENCIES_WITH_TAGS.get(source) }
if (peerDeps !== undefined) { }
if (taggedDeps !== undefined)
dependencies.add(taggedDeps)
else
dependencies.add(source)
peerDeps.forEach(dep => dependencies.add(dep))
}
if (source.startsWith(REGISTRY_DEPENDENCY)) { if (filename.endsWith('.ts')) {
const component = source.split('/').at(-1)! const ast = oxc.parseSync(code, {
registryDependencies.add(component) sourceType: 'module',
} sourceFilename: filename,
}) })
const sources = JSON.parse(ast.program).body.filter((i: any) => i.type === 'ImportDeclaration').map((i: any) => i.source)
sources.forEach((source: any) => {
populateDeps(source.value)
})
}
else {
const parsed = parse(code, { filename })
if (parsed.descriptor.script?.content || parsed.descriptor.scriptSetup?.content) {
const compiled = compileScript(parsed.descriptor, { id: '' })
Object.values(compiled.imports!).forEach((value) => {
populateDeps(value.source)
})
}
} }
return { registryDependencies, dependencies } return { registryDependencies, dependencies }

View File

@ -156,6 +156,9 @@ importers:
lodash.template: lodash.template:
specifier: ^4.5.0 specifier: ^4.5.0
version: 4.5.0 version: 4.5.0
oxc-parser:
specifier: ^0.2.0
version: 0.2.0
pathe: pathe:
specifier: ^1.1.2 specifier: ^1.1.2
version: 1.1.2 version: 1.1.2
@ -3123,7 +3126,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [darwin] os: [darwin]
requiresBuild: true requiresBuild: true
dev: false
optional: true optional: true
/@oxc-parser/binding-darwin-x64@0.2.0: /@oxc-parser/binding-darwin-x64@0.2.0:
@ -3131,7 +3133,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [darwin] os: [darwin]
requiresBuild: true requiresBuild: true
dev: false
optional: true optional: true
/@oxc-parser/binding-linux-arm64-gnu@0.2.0: /@oxc-parser/binding-linux-arm64-gnu@0.2.0:
@ -3139,7 +3140,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: false
optional: true optional: true
/@oxc-parser/binding-linux-x64-gnu@0.2.0: /@oxc-parser/binding-linux-x64-gnu@0.2.0:
@ -3147,7 +3147,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [linux] os: [linux]
requiresBuild: true requiresBuild: true
dev: false
optional: true optional: true
/@oxc-parser/binding-win32-arm64-msvc@0.2.0: /@oxc-parser/binding-win32-arm64-msvc@0.2.0:
@ -3155,7 +3154,6 @@ packages:
cpu: [arm64] cpu: [arm64]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: false
optional: true optional: true
/@oxc-parser/binding-win32-x64-msvc@0.2.0: /@oxc-parser/binding-win32-x64-msvc@0.2.0:
@ -3163,7 +3161,6 @@ packages:
cpu: [x64] cpu: [x64]
os: [win32] os: [win32]
requiresBuild: true requiresBuild: true
dev: false
optional: true optional: true
/@parcel/watcher-android-arm64@2.3.0: /@parcel/watcher-android-arm64@2.3.0:
@ -11115,7 +11112,6 @@ packages:
'@oxc-parser/binding-linux-x64-gnu': 0.2.0 '@oxc-parser/binding-linux-x64-gnu': 0.2.0
'@oxc-parser/binding-win32-arm64-msvc': 0.2.0 '@oxc-parser/binding-win32-arm64-msvc': 0.2.0
'@oxc-parser/binding-win32-x64-msvc': 0.2.0 '@oxc-parser/binding-win32-x64-msvc': 0.2.0
dev: false
/p-finally@1.0.0: /p-finally@1.0.0:
resolution: {integrity: sha512-LICb2p9CB7FS+0eR1oqWnHhp0FljGLZCWBE9aix0Uye9W8LTQPwMTYVGWQWIw9RdQiDg4+epXQODwIYJtSJaow==} resolution: {integrity: sha512-LICb2p9CB7FS+0eR1oqWnHhp0FljGLZCWBE9aix0Uye9W8LTQPwMTYVGWQWIw9RdQiDg4+epXQODwIYJtSJaow==}