DriftJS Unplugin
driftjs-unplugin is the official build-tool agnostic plugin powered by unplugin that turns .drift Single File Components into standard ECMAScript Modules (ESM) across Vite, Rollup, Webpack, esbuild, and Rspack.
Installation
pnpm add -D driftjs-unplugin
Usage
Vite
// vite.config.ts
import { defineConfig } from 'vite';
import { vite } from 'driftjs-unplugin';
export default defineConfig({
plugins: [vite()],
});
Rollup
// rollup.config.js
import { rollup } from 'driftjs-unplugin';
export default {
plugins: [rollup()],
};
Webpack
// webpack.config.js
const { webpack } = require('driftjs-unplugin');
module.exports = {
plugins: [webpack()],
};
esbuild
// esbuild.config.js
import { build } from 'esbuild';
import { esbuild } from 'driftjs-unplugin';
build({
plugins: [esbuild()],
});
Rspack
// rspack.config.js
const { rspack } = require('driftjs-unplugin');
module.exports = {
plugins: [rspack()],
};
Options
export interface DriftPluginOptions {
/** Enable compiler debug diagnostics and bytecode dump. Defaults to false. */
debug?: boolean;
}
How It Works
Source Transformation Hook
When the bundler encounters an import matching *.drift:
- It invokes
compileToESM(src, options)fromdriftjs-compiler. - The compiler runs lexing, parsing, AST transformation, and bytecode generation, then emits CSP-safe ESM JavaScript code.
- The synthetic ESM module is returned to the bundler for packaging.
Hot Module Replacement Hook (Vite)
During development (vite dev), editing any .drift file triggers the plugin's HMR handler:
- The module graph node for the file is invalidated.
- A reload notification is pushed through Vite's WebSocket channel to instantly update the running client.