esbuild-plugin-pug
raw JSON → 0.0.8 verified Fri May 01 auth: no javascript
An esbuild plugin that enables importing .pug and .jade templates directly in JavaScript/TypeScript builds. Current version is 0.0.8, with infrequent releases. Unlike generic loaders, it wraps the full Pug compiler, supporting all Pug features (includes, mixins, interpolation) and returns compiled HTML strings. Use with esbuild for server-side rendering or static site generation without a separate build step.
Common errors
error Error: Cannot find module 'esbuild-plugin-pug' ↓
cause Missing dev dependency. Install the package locally.
fix
npm install --save-dev esbuild-plugin-pug
error TypeError: pugPlugin is not a function ↓
cause Incorrect import. The package exports a function that must be called to create the plugin.
fix
Change import: use pugPlugin() not pugPlugin
Warnings
gotcha The plugin does not support esbuild's watch mode or incremental builds correctly; file changes in .pug files may not trigger rebuilds. ↓
fix Use a separate watch mechanism or esbuild's built-in watch with onRebuild callback.
gotcha Pug options (pretty, filename, etc.) are not exposed. The plugin hardcodes `pretty: true` which may produce unwanted whitespace. ↓
fix Fork the plugin or precompile templates manually if you need to customize Pug compiler options.
gotcha The plugin returns the compiled template as a function that takes locals object; it does not handle async templates or filters (e.g., markdown). ↓
fix Precompile async templates separately or use a different plugin that supports esbuild's async plugins.
Install
npm install esbuild-plugin-pug yarn add esbuild-plugin-pug pnpm add esbuild-plugin-pug Imports
- default (pugPlugin)
import pugPlugin from 'esbuild-plugin-pug' - default (pugPlugin)
const pugPlugin = require('esbuild-plugin-pug') - type Plugin
import type { Plugin } from 'esbuild'
Quickstart
import esbuild from 'esbuild';
import pugPlugin from 'esbuild-plugin-pug';
await esbuild.build({
entryPoints: ['src/index.ts'],
bundle: true,
platform: 'node',
outfile: 'dist/bundle.js',
plugins: [
pugPlugin()
],
});
// Example template usage:
// In src/index.ts:
// import template from './template.pug';
// console.log(template()); // renders HTML