@purityjs/vite-plugin
v0.1.0
Published
Vite plugin for AOT template compilation in Purity
Readme
@purityjs/vite-plugin
AOT template compilation for Purity. Compiles html tagged templates at build time into direct DOM creation code.
Why
| | Without plugin | With plugin |
| ------------------ | ------------------- | --------------------- |
| Bundle | 8.13 kB gzip | 6.02 kB gzip |
| First render | JIT compile + cache | Pre-compiled, instant |
| CSP | Needs unsafe-eval | CSP-safe |
| Runtime parser | Shipped to browser | Eliminated |
Install
npm install -D @purityjs/vite-pluginSetup
// vite.config.ts
import { purity } from '@purityjs/vite-plugin';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [purity()],
});That's it. No other config needed.
What It Does
Your code:
html`<div @click=${handler}>${() => count()}</div>`;Compiled output:
const _e0 = document.createElement('div');
_e0.addEventListener('click', handler);
const _x0 = document.createTextNode('');
__watch(() => {
_x0.data = String(count());
});
_e0.appendChild(_x0);No runtime parsing. No new Function(). Direct DOM calls.
Options
purity({
include: ['.ts', '.js', '.tsx', '.jsx'], // file extensions to transform (default)
});How It Works
- Finds
htmltagged template literals in your source - Extracts template strings and expression positions
- Parses into AST using
@purityjs/core's parser - Generates optimized JS using
@purityjs/core's codegen - Replaces the
html...`` call with the compiled output - Removes
htmlfrom imports (dead code eliminated) - Adds
watchimport alias for reactive bindings
The plugin only transforms user code — framework internals are skipped.
License
MIT
