UNPKG

sqlocal

Version:

SQLocal makes it easy to run SQLite3 in the browser, backed by the origin private file system.

56 lines (53 loc) 1.39 kB
import type { Plugin, UserConfig } from 'vite'; /** * Represents the configuration that SQLocal's Vite plugin accepts. * @see {@link https://sqlocal.dev/guide/setup#vite-configuration} */ export type VitePluginConfig = { /** * If set to `false`, the plugin will not add the * HTTP response headers required for * [cross-origin isolation](https://sqlocal.dev/guide/setup#cross-origin-isolation) * to the Vite development server. * @default true */ coi?: boolean; }; /** * A Vite plugin that tweaks some Vite settings for building apps * that use SQLocal. * @see {@link https://sqlocal.dev/guide/setup#vite-configuration} */ export default function vitePluginSQLocal( config: VitePluginConfig = { coi: true } ): Plugin<UserConfig> { return { name: 'vite-plugin-sqlocal', enforce: 'pre', config(config): UserConfig { return { optimizeDeps: { ...config.optimizeDeps, exclude: [ ...(config.optimizeDeps?.exclude ?? []), 'sqlocal', '@sqlite.org/sqlite-wasm', ], }, worker: { ...config.worker, format: 'es', }, }; }, configureServer(server): void { if (config.coi !== false) { server.middlewares.use((_, res, next) => { res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp'); res.setHeader('Cross-Origin-Opener-Policy', 'same-origin'); next(); }); } }, }; }