voidzero-dev / voidzero-dev/vite-plus

Ergonomics of vp pack + plugins

Ouverte
#2,632 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub
pending triage
Langage dominant
Rust
Étoiles
5.8k
Forks
262
Merge moyen
23 h 18 min
PR mergées (30 j)
139

Description

### 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.

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par le chemin d’extraction de configuration utilisé par `vp pack` et le comportement de `lazyPlugins` contrôlé par `VP_RESOLVING_CONFIG_METADATA` ; l’issue fournit une reproduction dans `vite.config.ts`. Exécutez `vp pack` avec le plugin fourni et suivez la manière dont root et `pack.plugins` sont transmis à tsdown. Le travail est considéré comme terminé lorsque les plugins de pack s’exécutent tout en restant lazy pour `vp lint` et `vp fmt`, et que la composition des plugins de root est clarifiée.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
build-system, tooling
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Active
Clarté
Plutôt claire
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.