npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mish.dev/vite-convert-pug-in-html

v2.0.0

Published

A Vite plugin to seamlessly integrate Pug for multi-page applications, on-the-fly development server compilation, and component templating.

Readme

vite-convert-pug-in-html

A Vite plugin for a seamless, zero-config Pug integration in multi-page applications (MPA).

This plugin automatically detects your .pug pages, compiles them on-the-fly in the dev server with support for "pretty URLs" (e.g., /about), and builds them into a clean, nested directory structure for production.

Features

  • 🚀 Zero-Config MPA: Automatically detects all .pug files in your source directory to create a multi-page application without manual configuration.
  • ⚡️ Pretty URLs & Dev Server: Intercepts requests to clean URLs (like /about or /contact) and serves the correctly compiled Pug file on-the-fly.
  • 🔄 Hot Module Replacement (HMR): Full-page reloads when any .pug file (including partials via include or extends) is changed.
  • 🧩 Vite Alias Support: Use Vite aliases from your resolve.alias config directly in Pug include or extends statements.
  • 🌐 Global Data: Pass global variables and data to all your Pug templates using the locals option.
  • 📦 Vite-Native Asset Handling: Let Vite handle your assets naturally. Just link your scripts and styles (<script src="/main.ts">) in Pug, and Vite will bundle and inject them correctly.
  • 🔧 Extensible: Pass custom options directly to the Pug compiler via pugOptions.

Installation

npm install @mish.dev/vite-convert-pug-in-html -D

The plugin requires pug to be installed in your project:

npm install pug -D

Usage

Add the plugin to your vite.config.ts. For most projects, no options are required.

How It Works

The plugin automatically scans your project's root directory (by default, src if you've set root: 'src') for .pug files to build your application.

  • It ignores any file starting with _ (e.g., _layout.pug), treating them as partials.
  • src/index.pug becomes the site's root (/).
  • src/pages/about.pug becomes accessible at /about.
  • src/pages/contact/index.pug becomes accessible at /contact.

Recommended Project Structure:

.
├── src/
│   ├── components/
│   │   └── _header.pug
│   ├── pages/
│   │   ├── about.pug
│   │   └── contact/
│   │       └── index.pug
│   ├── templates/
│   │   └── _layout.pug
│   ├── index.pug
│   └── main.ts
└── vite.config.ts

Basic Zero-Config Setup

vite.config.ts:

import { defineConfig } from 'vite';
import { viteConvertPugInHtml } from '@mish.dev/vite-convert-pug-in-html';

export default defineConfig({
  // Tell Vite that your source code is in the 'src' directory
  root: 'src',

  plugins: [
    // That's it! No options needed for a standard setup.
    viteConvertPugInHtml(),
  ],

  build: {
    // Make sure Vite builds to the project root, not inside 'src'
    outDir: '../dist',
  },
});

When you run vite build, the plugin automatically generates:

  • dist/index.html (from src/index.pug)
  • dist/about/index.html (from src/pages/about.pug)
  • dist/contact/index.html (from src/pages/contact/index.pug)

In the dev server, you can access these pages at /, /about, and /contact.

Handling Assets (The Modern Vite Way)

Forget manual replacements. Just link your TypeScript/JavaScript entry point directly in your main layout file. Vite will handle the rest.

src/templates/_layout.pug:

doctype html
html
  head
    title My Awesome Site
    // Vite will automatically inject the compiled CSS here
  body
    block content

    // Just point to your TS/JS entry file.
    // Vite will bundle it and add the correct hashed path on build.
    script(src="/main.ts" type="module")

Passing Global Data with locals

Use the locals option to make data available in all your Pug templates. This is the perfect place for site-wide constants, helper functions, or environment variables.

vite.config.ts:

import { defineConfig } from 'vite';
import { viteConvertPugInHtml } from '@mish.dev/vite-convert-pug-in-html';

export default defineConfig({
  root: 'src',
  plugins: [
    viteConvertPugInHtml({
      locals: {
        SITE_NAME: 'My Awesome Company',
        PHONE: '+1 (800) 555-1234',
        CURRENT_YEAR: new Date().getFullYear(),
      },
    }),
  ],
  build: {
    outDir: '../dist',
  },
});

src/templates/_layout.pug:

doctype html
html
  head
    //- Variables from `locals` are globally available
    title= SITE_NAME
  body
    block content
    footer
      p &copy; #{CURRENT_YEAR} #{SITE_NAME}
      p Call us at: #{PHONE}

Vite Alias Support

The plugin automatically uses aliases from your resolve.alias config, making includes clean and maintainable.

vite.config.ts:

import { defineConfig } from 'vite';
import { viteConvertPugInHtml } from '@mish.dev/vite-convert-pug-in-html';
import { resolve } from 'path';

export default defineConfig({
  root: 'src',
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    },
  },
  plugins: [viteConvertPugInHtml()],
  build: {
    outDir: '../dist',
  },
});

src/pages/about.pug:

extends @/templates/_layout.pug

block content
  //- Use the '@' alias to include a component
  include @/components/_header.pug
  h1 About Us

Options

pugOptions

  • Type: PugOptions (from pug)
  • Default: {}

An object of options passed directly to pug.render(). Use this for advanced Pug features like filters or custom doctypes.

viteConvertPugInHtml({
  pugOptions: {
    pretty: true, // Make the output HTML readable (default)
  },
}),

locals

  • Type: Record<string, any>
  • Default: {}

An object of global variables that will be available in all your Pug templates. This is merged with pugOptions before rendering.

License

MIT