Skip to content

Commit

Permalink
feat(resolveModuleExportNames): resolve recursive star exports
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 10, 2022
1 parent dd63a31 commit 50991e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/analyze.ts
Expand Up @@ -154,8 +154,21 @@ export async function resolveModuleExportNames (id: string, opts?: ResolveOption
const url = await resolvePath(id, opts)
const code = await loadURL(url)
const exports = findExports(code)
// TODO: Support recursive for start exports
return exports.flatMap(exp => exp.names).filter(Boolean)

// Explicit named exports
const exportNames = new Set(exports.flatMap(exp => exp.names).filter(Boolean))

// Recursive * exports
for (const exp of exports) {
if (exp.type === 'star') {
const subExports = await resolveModuleExportNames(exp.specifier, { ...opts, url })
for (const subExport of subExports) {
exportNames.add(subExport)
}
}
}

return Array.from(exportNames)
}

// --- Internal ---
Expand Down
26 changes: 25 additions & 1 deletion test/exports.test.ts
@@ -1,3 +1,4 @@
import { join } from 'path'
import { describe, it, expect } from 'vitest'
import { ESMExport, findExports, findExportNames, resolveModuleExportNames } from '../src'

Expand Down Expand Up @@ -136,7 +137,7 @@ describe('fineExportNames', () => {
})

describe('resolveModuleExportNames', () => {
it('resolveModuleExportNames', async () => {
it('direct exports', async () => {
expect(await resolveModuleExportNames('pathe')).toMatchInlineSnapshot(`
[
"basename",
Expand All @@ -156,4 +157,27 @@ describe('resolveModuleExportNames', () => {
]
`)
})

it('star exports', async () => {
expect(await resolveModuleExportNames(new URL('./fixture/exports.mjs', import.meta.url).toString())).toMatchInlineSnapshot(`
[
"foo",
"_resolve",
"basename",
"delimiter",
"dirname",
"extname",
"format",
"isAbsolute",
"join",
"normalize",
"normalizeString",
"parse",
"relative",
"resolve",
"sep",
"toNamespacedPath",
]
`)
})
})
5 changes: 5 additions & 0 deletions test/fixture/exports.mjs
@@ -0,0 +1,5 @@
export * from 'pathe'

export { resolve as _resolve } from 'pathe'

export const foo = 'bar'

0 comments on commit 50991e4

Please sign in to comment.