Vite Plugin API Reference
zone5() Vite plugin function options and behavior.
Import
import { zone5 } from 'zone5/vite'; Function Signature
function zone5(options?: Zone5PluginOptions): Plugin Options
interface Zone5PluginOptions {
cwd?: string;
basePath?: string;
} | Option | Type | Default | Description |
|---|---|---|---|
cwd | string | undefined | Working directory for loading .zone5.toml. If not specified, uses process.cwd(). |
basePath | string | undefined | URL prefix for assets. Overrides SvelteKit’s paths.base when specified. Useful for deploying to subdirectories. |
Return Value
Returns a Vite Plugin object.
Basic Usage
// vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import { zone5 } from 'zone5/vite';
export default defineConfig({
plugins: [
zone5(),
sveltekit()
]
}); Deploying to a Subdirectory
When deploying to a subdirectory (e.g., GitHub Pages at /<repo-name>/), use the BASE_PATH environment variable for both SvelteKit and Zone5:
// svelte.config.ts
const config = {
kit: {
paths: {
base: process.env.BASE_PATH || '',
},
},
}; // vite.config.ts
export default defineConfig({
plugins: [
zone5({ basePath: process.env.BASE_PATH || '' }),
sveltekit()
]
}); Then build with the environment variable set:
BASE_PATH=/my-app pnpm build Plugin Order
The zone5() plugin must come before sveltekit() in the plugins array.
plugins: [
zone5(), // First
sveltekit() // After zone5
] This is required because Zone5 needs to intercept module resolution before SvelteKit processes imports.
Behavior
Module Resolution
The plugin intercepts imports with the ?z5 query parameter:
import photo from './image.jpg?z5'; When detected:
- Resolves the import to a virtual module (prefixed with
\0) - Processes the image if not cached
- Returns an ES module with the
ItemFeaturedata
Virtual Module Generation
For each ?z5 import, the plugin generates an ES module:
// Generated module content
export const type = "Feature";
export const id = "abc123";
export const geometry = null;
export const properties = { /* ... */ };
export const assets = [ /* ... */ ];
export default { type, id, geometry, properties, assets }; Development Middleware
In development and preview modes, the plugin adds middleware to serve processed images:
- URL pattern:
/@zone5/<image-hash>/<width>.jpg - Serves from the
.zone5/cache directory - Sets
Cache-Control: max-age=360000
Build Output
During production builds:
- Processes all imported images
- Copies
.zone5/cache to the output directory - Images are served from
/<namespace>/URL path
Configuration Loading
The plugin loads configuration from .zone5.toml:
- Starts from
cwdparameter (orprocess.cwd()) - Walks up directory tree until config found
- Uses defaults if no config file exists
Supported File Types
Currently supports:
.jpg/.jpegfiles with?z5query parameter
Related
- Configuration Reference -
.zone5.tomloptions - Vite Module Resolution - How module resolution works
- Add to Existing Project - Setup tutorial