Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: unjs/mlly
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.5.7
Choose a base ref
...
head repository: unjs/mlly
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.5.8
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Aug 10, 2022

  1. fix(resolveModuleExportNames): filter out star exports

    TODO: to implement star
    pi0 committed Aug 10, 2022
    Copy the full SHA
    dd63a31 View commit details
  2. Copy the full SHA
    50991e4 View commit details
  3. chore(release): 0.5.8

    pi0 committed Aug 10, 2022
    Copy the full SHA
    e2e5717 View commit details
Showing with 59 additions and 4 deletions.
  1. +12 −0 CHANGELOG.md
  2. +1 −1 package.json
  3. +16 −2 src/analyze.ts
  4. +25 −1 test/exports.test.ts
  5. +5 −0 test/fixture/exports.mjs
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,18 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.5.8](https://github.com/unjs/mlly/compare/v0.5.7...v0.5.8) (2022-08-10)


### Features

* **resolveModuleExportNames:** resolve recursive star exports ([50991e4](https://github.com/unjs/mlly/commit/50991e491efba146dc3385da2b13a44eb3c13374))


### Bug Fixes

* **resolveModuleExportNames:** filter out star exports ([dd63a31](https://github.com/unjs/mlly/commit/dd63a31010d8b55d2fbdf4f910afda9b629b2bbb))

### [0.5.7](https://github.com/unjs/mlly/compare/v0.5.6...v0.5.7) (2022-08-03)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mlly",
"version": "0.5.7",
"version": "0.5.8",
"description": "Missing ECMAScript module utils for Node.js",
"repository": "unjs/mlly",
"license": "MIT",
18 changes: 16 additions & 2 deletions src/analyze.ts
Original file line number Diff line number Diff line change
@@ -147,14 +147,28 @@ export function findExports (code: string): ESMExport[] {
}

export function findExportNames (code: string): string[] {
return findExports(code).flatMap(exp => exp.names)
return findExports(code).flatMap(exp => exp.names).filter(Boolean)
}

export async function resolveModuleExportNames (id: string, opts?: ResolveOptions): Promise<string[]> {
const url = await resolvePath(id, opts)
const code = await loadURL(url)
const exports = findExports(code)
return exports.flatMap(exp => exp.names)

// 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 ---
26 changes: 25 additions & 1 deletion test/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'path'
import { describe, it, expect } from 'vitest'
import { ESMExport, findExports, findExportNames, resolveModuleExportNames } from '../src'

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

describe('resolveModuleExportNames', () => {
it('resolveModuleExportNames', async () => {
it('direct exports', async () => {
expect(await resolveModuleExportNames('pathe')).toMatchInlineSnapshot(`
[
"basename",
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from 'pathe'

export { resolve as _resolve } from 'pathe'

export const foo = 'bar'