Skip to content

Commit fc90c26

Browse files
committed
fix(cli): suppress postcss/lightningcss MISSING_EXPORT warnings via transform plugin
1 parent e3607ec commit fc90c26

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

packages/cli/src/pack-bin.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import { readFileSync } from 'node:fs';
23
import module from 'node:module';
34

45
import {
@@ -13,6 +14,32 @@ import { cac } from 'cac';
1314

1415
import { 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+
/node_modules\/(postcss|lightningcss)\/.*\.d\.(ts|mts|cts)$|lightningcssOptions\.d\.ts$/;
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(/^(import\s+)(?!type\s)/gm, 'import type ')
37+
.replace(/^(export\s+)\{/gm, 'export type {');
38+
},
39+
},
40+
};
41+
}
42+
1643
const cli = cac('vp pack');
1744
cli.help();
1845

@@ -98,7 +125,14 @@ cli
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

Comments
 (0)