Files
NapCatQQ/.pnpm-store/v3/files/fa/9d96b44c51f7941daffc208d29c5323fbe1dcec1c036c77c5d7176364bf10c130e71702bc3057f148f085fb28545055ef3d3ffd2cd1d0f77cc14330ca659b1
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

76 lines
2.3 KiB
Plaintext

/**
* Filters out Vite plugins that have `apply: 'serve'` set.
*
* Since Rolldown operates in build mode, plugins marked with `apply: 'serve'`
* are intended only for Vite's dev server and should be excluded from the build process.
*
* @param plugins - Array of plugins (can include nested arrays)
* @returns Filtered array with serve-only plugins removed
*
* @example
* ```ts
* import { defineConfig } from 'rolldown';
* import { filterVitePlugins } from '@rolldown/pluginutils';
* import viteReact from '@vitejs/plugin-react';
*
* export default defineConfig({
* plugins: filterVitePlugins([
* viteReact(),
* {
* name: 'dev-only',
* apply: 'serve', // This will be filtered out
* // ...
* }
* ])
* });
* ```
*/
export function filterVitePlugins(plugins) {
if (!plugins) {
return [];
}
const pluginArray = Array.isArray(plugins) ? plugins : [plugins];
const result = [];
for (const plugin of pluginArray) {
// Skip falsy values
if (!plugin) {
continue;
}
// Handle nested arrays recursively
if (Array.isArray(plugin)) {
result.push(...filterVitePlugins(plugin));
continue;
}
// Check if plugin has apply property
const pluginWithApply = plugin;
if ('apply' in pluginWithApply) {
const applyValue = pluginWithApply.apply;
// If apply is a function, call it with build mode
if (typeof applyValue === 'function') {
try {
const shouldApply = applyValue({}, // config object
{ command: 'build', mode: 'production' });
if (shouldApply) {
result.push(plugin);
}
}
catch {
// If function throws, include the plugin to be safe
result.push(plugin);
}
} // If apply is 'serve', skip this plugin
else if (applyValue === 'serve') {
continue;
} // If apply is 'build' or anything else, include it
else {
result.push(plugin);
}
}
else {
// No apply property, include the plugin
result.push(plugin);
}
}
return result;
}