@@ -23,18 +23,31 @@ import { resolveViteConfig } from './resolve-vite-config.js';
2323 * Since .d.ts files contain only type information, all imports/exports are
2424 * inherently type-only, so this transformation is always safe.
2525 */
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 $ / ;
26+ const EXTERNAL_DTS_INTERNAL_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 ) $ / ;
28+ const EXTERNAL_DTS_CONSUMER_RE =
29+ / l i g h t n i n g c s s O p t i o n s \. d \. t s $ | v i t e - p l u s - c o r e \/ d i s t \/ .* \. d \. t s $ / ;
30+ const EXTERNAL_DTS_FIX_RE = new RegExp (
31+ `${ EXTERNAL_DTS_INTERNAL_RE . source } |${ EXTERNAL_DTS_CONSUMER_RE . source } ` ,
32+ ) ;
2833
2934function externalDtsTypeOnlyPlugin ( ) {
3035 return {
3136 name : 'vite-plus:external-dts-type-only' ,
3237 transform : {
3338 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 {' ) ;
39+ handler ( code : string , id : string ) {
40+ if ( EXTERNAL_DTS_INTERNAL_RE . test ( id ) ) {
41+ // postcss/lightningcss internal files: transform all imports and exports
42+ return code
43+ . replace ( / ^ ( i m p o r t \s + ) (? ! t y p e \s ) / gm, 'import type ' )
44+ . replace ( / ^ ( e x p o r t \s + ) \{ / gm, 'export type {' ) ;
45+ }
46+ // Consumer files: only transform imports from postcss/lightningcss
47+ return code . replace (
48+ / ^ ( i m p o r t \s + ) (? ! t y p e \s ) ( .+ f r o m \s + [ ' " ] (?: p o s t c s s | l i g h t n i n g c s s ) [ ' " ] ) / gm,
49+ 'import type $2' ,
50+ ) ;
3851 } ,
3952 } ,
4053 } ;
0 commit comments