voidzero-dev / voidzero-dev/vite-plus
Ergonomics of vp pack + plugins
- Vorherrschende Sprache
- Rust
- Sterne
- 5.8k
- Forks
- 262
- Ø Merge
- 23 Std. 41 Min.
- Gemergte PRs (30 T.)
- 138
Beschreibung
### Description
Configuring plugins for library packaging via `vp pack` has confusing ergonomics and a potential bug when using `lazyPlugins`:
1. **Root `plugins` are ignored by `vp pack`**: In a library project using `vite.config.ts`, plugins configured at the root (`plugins: [...]`) are not forwarded to `tsdown` during `vp pack`. Users must define them redundantly under `pack.plugins`.
2. **`lazyPlugins` fails silently inside `pack.plugins`**: If a user attempts to wrap `pack.plugins` in `lazyPlugins(() => [...])` to avoid running plugin factories during `vp lint` or `vp fmt`, the plugins never execute during `vp pack`.
### Root Cause
When `vp pack` extracts the `tsdown` config from `vite.config.ts`, `VP_RESOLVING_CONFIG_METADATA` is set to `"1"`.
Inside `lazyPlugins`:
```js
function lazyPlugins(cb) {
if (process.env["VP_RESOLVING_CONFIG_METADATA"] === "1") return;
const result = cb();
return result;
}
```
Because the metadata environment marker is active during config extraction for vp pack, lazyPlugins returns undefined. Consequently, tsdown runs with no plugins.
Reproduction
In vite.config.ts:
```ts
import { defineConfig, lazyPlugins } from 'vite-plus';
function myPlugin() {
return {
name: 'test-plugin',
buildStart() {
console.log('--- PLUGIN EXECUTED ---');
},
};
}
export default defineConfig({
pack: {
entry: ['src/index.ts'],
// ❌ Fails: '--- PLUGIN EXECUTED ---' is never printed
plugins: lazyPlugins(() => [myPlugin()]),
// ⚠️ Works, but runs eagerly during `vp lint` / `vp fmt`:
// plugins: [myPlugin()],
},
// ❌ Ignored by `vp pack`:
// plugins: [myPlugin()],
});
```
Run:
```bash
vp pack
```
### Suggested solution
1. `lazyPlugins` should evaluate when resolving the build pipeline for `vp pack`, so plugins can be lazily loaded without executing during `vp check` / `vp fmt` / `vp lint`.
2. Ideally, plugins defined at the root (`plugins: [...]`) should either auto merge with `pack.plugins` by default when building libraries, or vite-plus should provide clear ergonomics/typing on how root Vite plugins vs. pack plugins compose. It's frankly confusing having two separate places to specify the plugin arrays.
### Alternative
_No response_
### Additional context
_No response_
### Validations
- [x] Read the [Contributing Guidelines](https://github.com/voidzero-dev/vite-plus/blob/main/CONTRIBUTING.md).
- [x] Confirm this request is for Vite+ itself and not for Vite, Vitest, tsdown, Rolldown, or Oxc.
- [x] Check that there isn't already an issue requesting the same feature.
Beitragsleitfaden
Rechercherichtung
Beginne mit dem von `vp pack` verwendeten Pfad zur Konfigurationsextraktion und dem durch `VP_RESOLVING_CONFIG_METADATA` gesteuerten Verhalten von `lazyPlugins`; das Issue enthält eine Reproduktion in `vite.config.ts`. Führe `vp pack` mit dem bereitgestellten Plugin aus und verfolge, wie root und `pack.plugins` an tsdown weitergeleitet werden. Erledigt ist es, wenn Pack-Plugins ausgeführt werden, während sie für `vp lint` und `vp fmt` lazy bleiben, und die Zusammensetzung der Root-Plugins geklärt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- build-system, tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100