voidzero-dev / voidzero-dev/vite-plus
Ergonomics of vp pack + plugins
- Lingua principale
- Rust
- Stelle
- 5.8k
- Fork
- 262
- Merge medio
- 1g 34m
- PR unite (30g)
- 135
Descrizione
### 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.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Start with the config-extraction path used by `vp pack` and the `lazyPlugins` behavior controlled by `VP_RESOLVING_CONFIG_METADATA`; the issue provides a reproduction in `vite.config.ts`. Run `vp pack` with the supplied plugin and trace how root and `pack.plugins` are forwarded to tsdown. Done means pack plugins execute while remaining lazy for `vp lint` and `vp fmt`, with root-plugin composition clarified.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- build-system, tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Attiva
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 48/100