Vite Plugin
The nexa-vite-plugin transforms .nexa Single-File Components into JavaScript during development and build.
Setup
ts
// vite.config.ts
import { defineConfig } from 'vite'
import { nexaPlugin } from 'nexa-vite-plugin'
export default defineConfig({
plugins: [nexaPlugin()],
})What It Does
- Resolves
.nexaimports — Handles module resolution for.nexafiles - Compiles SFC — Transforms
<script setup>,<template>, and<style scoped>blocks into JavaScript - Scoped CSS — Generates unique scope IDs and injects scoped styles at runtime
- HMR — Hot Module Replacement with component state preservation
- Source Maps — Maps compiled output back to original
.nexasource
HMR (Hot Module Replacement)
HMR is automatically enabled in development mode. When you edit a .nexa file:
- The component is recompiled
- The new version replaces the old one in-place
- Component state (signals) is preserved
- Styles are hot-updated without page reload
Import Resolution
The plugin resolves imports in the following order:
.nexafiles → compiled vianexa-compiler.jsfiles → if not found, tries.tsextension (for TypeScript projects)- All other files → delegated to Vite's default resolver
Build for Production
bash
vite buildIn production mode, the plugin:
- Generates optimized component code
- Strips HMR code (
import.meta.hotblocks) - Inlines scoped styles
Security Note
The plugin reads .nexa files from the filesystem and compiles them. Only files within your project directory should be compiled. Never serve .nexa compilation as a user-facing API.