11#!/usr/bin/env node
2+ import { readFileSync } from 'node:fs' ;
23import module from 'node:module' ;
34
45import {
@@ -13,6 +14,32 @@ import { cac } from 'cac';
1314
1415import { resolveViteConfig } from './resolve-vite-config.js' ;
1516
17+ /**
18+ * Rolldown plugin that transforms value imports/exports to type-only in external
19+ * packages' .d.ts files. Some packages (e.g. postcss, lightningcss) use
20+ * `import { X }` and `export { X } from` instead of their type-only equivalents,
21+ * which causes MISSING_EXPORT warnings from the DTS bundler.
22+ *
23+ * Since .d.ts files contain only type information, all imports/exports are
24+ * inherently type-only, so this transformation is always safe.
25+ */
26+ const EXTERNAL_DTS_FIX_RE =
27+ / n o d e _ m o d u l e s \/ ( p o s t c s s | l i g h t n i n g c s s ) \/ .* \. d \. ( t s | m t s | c t s ) $ | l i g h t n i n g c s s O p t i o n s \. d \. t s $ / ;
28+
29+ function externalDtsTypeOnlyPlugin ( ) {
30+ return {
31+ name : 'vite-plus:external-dts-type-only' ,
32+ transform : {
33+ filter : { id : { include : [ EXTERNAL_DTS_FIX_RE ] } } ,
34+ handler ( code : string ) {
35+ return code
36+ . replace ( / ^ ( i m p o r t \s + ) (? ! t y p e \s ) / gm, 'import type ' )
37+ . replace ( / ^ ( e x p o r t \s + ) \{ / gm, 'export type {' ) ;
38+ } ,
39+ } ,
40+ } ;
41+ }
42+
1643const cli = cac ( 'vp pack' ) ;
1744cli . help ( ) ;
1845
98125 ? viteConfig . pack
99126 : [ viteConfig . pack ?? { } ] ;
100127 for ( const packConfig of packConfigs ) {
101- const resolvedConfig = await resolveUserConfig ( { ...packConfig , ...flags } , flags ) ;
128+ const merged = { ...packConfig , ...flags } ;
129+ // Inject plugin to fix MISSING_EXPORT warnings from external .d.ts files
130+ // (postcss, lightningcss use `import` instead of `import type` in their .d.ts)
131+ // Inject plugin to fix MISSING_EXPORT warnings from external .d.ts files
132+ // (postcss, lightningcss use `import`/`export` instead of `import type`/`export type`)
133+ const existingPlugins = Array . isArray ( merged . plugins ) ? merged . plugins : [ ] ;
134+ merged . plugins = [ ...existingPlugins , externalDtsTypeOnlyPlugin ( ) ] ;
135+ const resolvedConfig = await resolveUserConfig ( merged , flags ) ;
102136 configs . push ( ...resolvedConfig ) ;
103137 }
104138
0 commit comments