voidzero-dev / voidzero-dev/vite-plus

Ergonomics of vp pack + plugins

Open
#2,632 0 comments 1 reaction 0 assignees View on GitHub
pending triage
Dominant language
Rust
Stars
5.8k
Forks
262
Avg merge
1d 34m
Merged PRs (30d)
135

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.

Contributor guide

Open the contributing guide

Research direction

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.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.