A Vite plugin for server-side resource handling with virtual entry support and CSS bundling capabilities.
- π Virtual Entry Support: Handle virtual entry points in your Vite development server
- π¨ CSS Bundling: Automatically bundle CSS-only virtual modules into single CSS files
- π Folder Exposure: Expose custom folders as served files through the dev server
- β‘ Caching: Built-in caching for improved performance
- π§ TypeScript Support: Full TypeScript support with type definitions
- π Multi-Entry Integration: Perfect companion for virtual-multi-entry-plugin
yarn add vite-virtual-entry-server-plugin
# or
npm install vite-virtual-entry-server-plugin
# or
pnpm add vite-virtual-entry-server-pluginimport { defineConfig } from 'vite';
import { serverPlugin } from 'vite-virtual-entry-server-plugin';
export default defineConfig({
plugins: [
serverPlugin()
],
build: {
rollupOptions: {
input: {
main: 'src/main.ts',
'virtual:main': 'virtual:main',
'virtual:css': 'virtual:css'
}
}
}
});import { defineConfig } from 'vite';
import { serverPlugin } from 'vite-virtual-entry-server-plugin';
export default defineConfig({
plugins: [
serverPlugin({
exposedFolders: ['public', 'assets']
})
]
});Creates a Vite plugin for server-side resource handling.
options(optional):ServerPluginOptionsexposedFolders(optional):string[]- Array of folder paths to expose through the dev server
A Vite plugin object that can be used in your Vite configuration.
The plugin automatically detects virtual entries (those starting with virtual:) and:
- CSS-only virtual entries: If a virtual entry only imports CSS files, it generates a synthetic CSS bundle with
@importstatements - JavaScript virtual entries: Serves the transformed JavaScript code normally
When exposedFolders is provided, the plugin:
- Recursively scans the specified folders for files
- Creates middleware to serve each file at its relative path
- Transforms files through Vite's transformation pipeline
- Serves files with appropriate content types
The plugin implements intelligent caching:
- Entry code is cached after first transformation
- CSS bundles are cached for repeated requests
- Cached content is served with appropriate headers
This plugin is particularly useful when used alongside the virtual-multi-entry-plugin. The virtual-multi-entry-plugin allows you to create multiple virtual entry points from a single source, which pairs perfectly with this server plugin's virtual entry handling capabilities.
import { defineConfig } from 'vite';
import { serverPlugin } from 'vite-virtual-entry-server-plugin';
import { virtualMultiEntry } from 'virtual-multi-entry-plugin';
export default defineConfig({
plugins: [
virtualMultiEntry({
entries: {
'main': {
files: ['./src/main.js', './src/styles.css'],
type: 'app'
},
'admin': {
files: ['./src/admin.js', './src/admin.css'],
type: 'app'
}
}
}),
serverPlugin({
exposedFolders: ['public']
})
]
});This combination allows you to:
- Create multiple virtual entries from your source files
- Handle CSS-only virtual entries with automatic bundling
- Serve virtual entries through the development server
- Cache virtual entry outputs for better performance
- Node.js >= 20.19.0 (for Vite 7.x)
- Vite >= 7.1.7
The plugin includes full TypeScript support with exported types:
import { serverPlugin, type ServerPluginOptions } from 'vite-virtual-entry-server-plugin';
const options: ServerPluginOptions = {
exposedFolders: ['public']
};
const plugin = serverPlugin(options);This project uses Vite as both the development server and bundler.
yarn dev- Start the Vite development serveryarn build- Build the library for productionyarn preview- Preview the production buildyarn test- Run tests in watch modeyarn test:run- Run tests onceyarn test:ui- Run tests with UI
src/
βββ index.ts # Main entry point
βββ server.plugin.ts # Plugin implementation
βββ __tests__/ # Test files
βββ server-plugin.test.ts
The build process generates:
dist/index.es.js- ES module builddist/index.d.ts- TypeScript declarations- Source maps for debugging
To publish this package to npm:
- Build:
yarn build- Create production build - Test:
yarn test:run- Run test suite - Publish:
yarn publish- Publish to npm
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see the LICENSE file for details.
- Initial release
- Virtual entry support
- CSS bundling for CSS-only virtual entries
- Folder exposure functionality
- TypeScript support
- Comprehensive test coverage