Skip to content

Commit

Permalink
feat: copy .d.ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 11, 2024
1 parent 97e886c commit 8d52904
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const externalPlugin = (meta: PackageJson, exports: Record<string, Record<string
const name = args.path.startsWith('@')
? args.path.split('/', 2).join('/')
: args.path.split('/', 1)[0]
if (name === meta.name) return { external: true }
const types = new Set(DependencyType.filter((type) => meta[type]?.[name]))
if (types.size === 0) throw new Error(`Missing dependency: ${name}`)
// devDependencies are bundled
Expand All @@ -84,14 +85,12 @@ const externalPlugin = (meta: PackageJson, exports: Record<string, Record<string
})

build.onResolve({ filter: /^\./, namespace: 'file' }, async (args) => {
console.log('before:', args.path)
const { path } = await build.resolve(args.path, {
namespace: 'internal',
importer: args.importer,
resolveDir: args.resolveDir,
kind: args.kind,
})
console.log('after:', path)
if (currentEntry === path || !exports[path]) return null
if (format === 'cjs') return { external: true }
// native ESM import should preserve extensions
Expand All @@ -102,6 +101,11 @@ const externalPlugin = (meta: PackageJson, exports: Record<string, Record<string
if (!relpath.startsWith('.')) relpath = './' + relpath
return { path: relpath, external: true }
})

build.onLoad({ filter: /\.d\.ts$/, namespace: 'file' }, async (args) => {
const contents = await fs.readFile(args.path)
return { loader: 'copy', contents }
})
},
})

Expand Down

0 comments on commit 8d52904

Please sign in to comment.