Skip to content

Commit

Permalink
feat(tsc): detect dts outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 4, 2024
1 parent 062dfe2 commit f0c742a
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions packages/tsc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as fs from 'node:fs/promises'
import { join } from 'path'
import { Context, cwd, PackageJson } from 'yakumo'
import { compile, load } from 'tsconfig-utils'
import { compile, load, TsConfig } from 'tsconfig-utils'
import * as atsc from 'atsc'
import * as dtsc from 'dtsc'
import { extname } from 'node:path'

declare module 'yakumo' {
interface PackageConfig {
Expand All @@ -15,6 +16,7 @@ declare module 'yakumo' {

interface Node {
bundle: boolean
config: TsConfig
path: string
meta: PackageJson
prev: string[]
Expand All @@ -29,9 +31,42 @@ async function prepareBuild(nodes: Node[]) {
}, null, 2))
}

function getFiles(meta: PackageJson, outDir: string) {
function addExport(pattern?: string) {
if (!pattern) return
if (pattern.startsWith('./')) pattern = pattern.slice(2)
if (!pattern.startsWith(outDir + '/')) return
const ext = extname(pattern)
pattern = pattern.slice(outDir.length + 1, -ext.length)
if (pattern.endsWith('.d')) {
pattern = pattern.slice(0, -2)
}
files.add(pattern)
}

// TODO: support null targets
function addConditionalExport(pattern?: PackageJson.Exports) {
if (!pattern) return
if (typeof pattern === 'string') return addExport(pattern)
if ('types' in pattern) return addConditionalExport(pattern.types)
if ('typings' in pattern) return addConditionalExport(pattern.typings)
for (const key in pattern) {
addConditionalExport(pattern[key])
}
}

const files = new Set<string>()
addExport(meta.main)
addExport(meta.module)
addConditionalExport(meta.exports)
return [...files]
}

async function bundleNodes(nodes: Node[]) {
for (const node of nodes) {
// TODO: support multiple entry points
await dtsc.build(join(cwd, node.path))
console.log('dtsc:', node.path + '/lib/index.d.ts')
}
}

Expand Down Expand Up @@ -64,9 +99,11 @@ export function apply(ctx: Context) {
if (!meta.main && !meta.exports) continue
const fullpath = join(cwd, path)
try {
const tsconfig = await load(fullpath)
const bundle = !!tsconfig.compilerOptions?.outFile
nodes[meta.name] = { bundle, path, meta, prev: [], next: new Set() }
const config = await load(fullpath)
if (!config || config.compilerOptions?.noEmit) continue
const files = getFiles(meta, config.compilerOptions?.outDir || 'lib')
const bundle = !!config.compilerOptions?.outFile || files.length === 1 && !!meta.exports
nodes[meta.name] = { config, bundle, path, meta, prev: [], next: new Set() }
} catch {}
}

Expand Down

0 comments on commit f0c742a

Please sign in to comment.