Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto-extract untranspiled monorepo packages #333

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions auto-extract-test-case/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function function1<A, B = string>(a: A, b: B) {
return [a, b]
}
7 changes: 7 additions & 0 deletions auto-extract-test-case/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "auto-extract-test-case",
"private": true,
"version": "0.0.0",
"type": "module",
"main": "./index.ts"
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/estree": "^1.0.5",
"@types/node": "^20.11.8",
"@vitest/coverage-v8": "^1.2.2",
"auto-extract-test-case": "workspace:^",
"bumpp": "^9.3.0",
"conventional-changelog-cli": "^4.1.0",
"eslint": "8.56.0",
Expand Down
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
packages:
- playground
- auto-extract-test-case
11 changes: 10 additions & 1 deletion src/node/extract-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { resolveModuleExportNames } from 'mlly'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import { dirname, join } from 'pathe'
import type { Import, PackagePreset } from '../types'
import { scanExports } from './scan-dirs'

const CACHE_PATH = /* #__PURE__ */ join(os.tmpdir(), 'unimport')
let CACHE_WRITEABLE: boolean | undefined
Expand Down Expand Up @@ -36,6 +37,7 @@ async function extractExports(name: string, url?: string, cache = true) {
const packageJson = await readPackageJSON(packageJsonPath)
const version = packageJson.version
const cachePath = join(CACHE_PATH, `${name}@${version}`, 'exports.json')
const mainPath = join(dirname(packageJsonPath), packageJson.main ?? '')

/* c8 ignore next 8 */
if (cache && CACHE_WRITEABLE === undefined) {
Expand All @@ -51,7 +53,10 @@ async function extractExports(name: string, url?: string, cache = true) {
if (useCache && existsSync(cachePath))
return JSON.parse(await fsp.readFile(cachePath, 'utf-8'))

const scanned = await resolveModuleExportNames(name, { url })
const scanned = isUntranspiled(mainPath)
? (await scanExports(mainPath, true)).map(i => i.name)
: await resolveModuleExportNames(name, { url })

/* c8 ignore next 4 */
if (useCache) {
await fsp.mkdir(dirname(cachePath), { recursive: true })
Expand All @@ -70,3 +75,7 @@ function isWritable(filename: string): boolean {
return false
}
}

function isUntranspiled(path: string) {
return path.match(/\.tsx?$/)
}
14 changes: 14 additions & 0 deletions test/auto-extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ describe('auto-extract', () => {
]
`)
})
it('local package', async () => {
const result = await resolvePreset({
package: 'auto-extract-test-case',
})

expect(result).toMatchInlineSnapshot(`
[
{
"from": "auto-extract-test-case",
"name": "function1",
},
]
`)
})
})