fix(build): don't rely on checkout directory name when externalizing types

The `typesExternal` regex matched the absolute build path against
`/vitepress/`, so repo-root `.d.ts` files (types/*) were only kept
external when the repo was checked out in a directory named "vitepress".
When built elsewhere, `DefaultTheme` got inlined into
`dist/node/index.d.ts`, orphaning the `declare module` augmentations
from `defaultTheme.ts` and breaking node-only options like
`search.options._render` (regressed in v2.0.0-alpha.18).

Compare paths resolved against this config's directory instead,
normalized for separators and drive-letter casing so it also works on
windows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5335/head
Divyansh Singh 1 month ago
parent cec499869f
commit e6ba9d8caa

@ -5,6 +5,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import { rm } from 'node:fs/promises'
import { builtinModules, createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import { type RollupOptions, defineConfig } from 'rollup'
import dts from 'rollup-plugin-dts'
import esbuild from 'rollup-plugin-esbuild'
@ -53,11 +54,27 @@ const esmBuild: RollupOptions = {
}
}
const typesExternal = [
...external,
/\/vitepress\/(?!(dist|node_modules|vitepress)\/).*\.d\.ts$/,
/^markdown-it(?:\/|$)/
]
// keep .d.ts files under the repo root (e.g. types/*) external so module
// augmentations in the bundle still target the same files users reference.
// compared on normalized resolved paths so this works regardless of the
// checkout location, path separators, or drive-letter casing.
const normalizePath = (id: string): string => {
const normalized = id.replaceAll('\\', '/')
return process.platform === 'win32' ? normalized.toLowerCase() : normalized
}
const root = normalizePath(fileURLToPath(new URL('.', import.meta.url)))
const typesExternal = (id: string): boolean => {
if (external.includes(id) || /^markdown-it(?:\/|$)/.test(id)) return true
const normalized = normalizePath(id)
return (
normalized.endsWith('.d.ts') &&
normalized.startsWith(root) &&
!normalized.startsWith(`${root}dist/`) &&
!normalized.startsWith(`${root}node_modules/`)
)
}
const dtsNode = dts({
respectExternal: true,

Loading…
Cancel
Save