Skip to content

Commit

Permalink
refactor(api): rollup resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Aug 21, 2023
1 parent 7128dd5 commit bac5adb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-rivers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vue-macros/api': patch
---

refactor rollup resolve
112 changes: 55 additions & 57 deletions packages/api/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,87 +41,85 @@ export const RollupResolve = () => {
return result
}

const resolve =
(ctx: PluginContext): ResolveTSFileIdImpl =>
async (id, importer) => {
async function tryPkgEntry() {
const deepMatch = id.match(deepImportRE)
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : id

try {
const pkgPath = (await ctx.resolve(`${pkgId}/package.json`, importer))
?.id
if (!pkgPath) return
const pkg = JSON.parse(await readFile(pkgPath, 'utf-8'))
const pkgRoot = path.resolve(pkgPath, '..')

if (deepMatch) {
if (pkg.typesVersions) {
const pkgPath = id.replace(`${pkgId}/`, '')
for (const version of Object.values(pkg.typesVersions)) {
for (const [entry, subpaths] of Object.entries(
version as Record<string, string[]>
)) {
if (pkgPath !== entry.replace('*', pkgPath)) continue
for (const subpath of subpaths) {
const resolved = path.resolve(
pkgRoot,
subpath.replace('*', pkgPath)
)
if (isDts(resolved) && existsSync(resolved)) return resolved
}
const resolve = (ctx: PluginContext): ResolveTSFileIdImpl => {
async function tryPkgEntry(id: string, importer: string) {
const deepMatch = id.match(deepImportRE)
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : id

try {
const pkgPath = (await ctx.resolve(`${pkgId}/package.json`, importer))
?.id
if (!pkgPath) return
const pkg = JSON.parse(await readFile(pkgPath, 'utf-8'))
const pkgRoot = path.resolve(pkgPath, '..')

if (deepMatch) {
if (pkg.typesVersions) {
const pkgPath = id.replace(`${pkgId}/`, '')
for (const version of Object.values(pkg.typesVersions)) {
for (const [entry, subpaths] of Object.entries(
version as Record<string, string[]>
)) {
if (pkgPath !== entry.replace('*', pkgPath)) continue
for (const subpath of subpaths) {
const resolved = path.resolve(
pkgRoot,
subpath.replace('*', pkgPath)
)
if (isDts(resolved) && existsSync(resolved)) return resolved
}
}
}

const resolvedIds = exports(pkg, id, { conditions: ['types'] })
if (!resolvedIds) return
const resolved = resolvedIds.find(
(id) => isDts(id) && existsSync(id)
)
if (resolved) return resolved
} else {
const types = pkg.types || pkg.typings
if (!types) return

const entry = path.resolve(pkgRoot, types)
if (existsSync(entry)) return entry
}
} catch {}
}

const tryResolve = async (id: string) => {
try {
return (
(await ctx.resolve(id, importer))?.id ||
(await ctx.resolve(`${id}.d`, importer))?.id
)
} catch {}
const resolvedIds = exports(pkg, id, { conditions: ['types'] })
if (!resolvedIds) return
const resolved = resolvedIds.find((id) => isDts(id) && existsSync(id))
if (resolved) return resolved
} else {
const types = pkg.types || pkg.typings
if (!types) return

const entry = path.resolve(pkgRoot, types)
if (existsSync(entry)) return entry
}
} catch {}
}

return
}
async function tryResolve(id: string, importer: string) {
try {
return (
(await ctx.resolve(id, importer))?.id ||
(await ctx.resolve(`${id}.d`, importer))?.id
)
} catch {}

return
}

return async (id, importer) => {
const cached = resolveCache.get(importer)?.get(id)
if (cached) return cached

if (id[0] !== '.') {
const entry = await tryPkgEntry()
if (id[0] !== '.' && id[0] !== '/') {
const entry = await tryPkgEntry(id, importer)
if (entry) return withResolveCache(id, importer, entry)
}

let resolved = await tryResolve(id)
let resolved = await tryResolve(id, importer)
if (!resolved) return
if (existsSync(resolved)) {
collectReferencedFile(importer, resolved)
return withResolveCache(id, importer, resolved)
}

resolved = await tryResolve(resolved)
resolved = await tryResolve(resolved, importer)
if (resolved && existsSync(resolved)) {
collectReferencedFile(importer, resolved)
return withResolveCache(id, importer, resolved)
}
}
}

const handleHotUpdate: NonNullable<Plugin['handleHotUpdate']> = ({
file,
Expand Down

0 comments on commit bac5adb

Please sign in to comment.