Home

Awesome

<div align="center"> <h1> <a href="https://pugjs.org"> <img height="120" src="https://cdn.rawgit.com/pugjs/pug-logo/eec436cee8fd9d1726d7839cbe99d1f694692c0c/SVG/pug-final-logo-_-colour-128.svg"> </a> <a href="https://github.com/webpack/webpack"> <img height="120" src="https://webpack.js.org/assets/icon-square-big.svg"> </a> <a href="https://github.com/webdiscus/pug-loader"><br> Pug Loader </a> </h1> <div>Webpack loader to render Pug templates</div> </div>

npm node node Test codecov node

The Pug Loader renders Pug templates into HTML or compiles it into a template function.

🚀 Install and Quick Start

💡 Highlights

Warning

Until today, 2024, only this Pug loader is maintained. Please support this project by giving it a star ⭐.
All other Pug loaders are dead and have not been maintained for a long time:

Note

Instead of html-webpack-plugin recommended to use pug-plugin or html-bundler-webpack-plugin .

The Pug Plugin allow to use a template as an entrypoint and generates static HTML or template function from Pug template containing source files of scripts, styles, images, fonts and other resources, similar to how it works in Vite.

Please see usage examples and the demo app Hello World.

Contents

  1. Install and Quick start
  2. Options
  3. Using modes
  4. Using Pug filters
  5. Passing data into Pug template
  6. Using resources
  7. Path Resolving
  8. Using with Angular
  9. Using with Vue
  10. Recipes
  11. Example Hello World!
  12. Example Pug filters
  13. More examples

<a id="install-and-quick-start" name="install-and-quick-start" href="#install-and-quick-start"></a>

Install and Quick start

Choose your way:

<a id="using-pug-plugin" name="using-pug-plugin"></a>

Using only the pug-plugin

The pug-plugin contains already the pug and pug-loader packages. For details and examples please see the pug-plugin site.

Install the pug-plugin:

npm install pug-plugin --save-dev

Install additional packages for styles:

npm install css-loader sass-loader sass --save-dev

Start with a Pug template. Add the link and script tags. You can include asset source files such as SCSS, JS, images, and other media files directly in a Pug template.

The plugin resolves script(src="...") link(href="...") and img(src="..." srcset="...") that references your script, style and image source files.

For example, there is the template ./src/views/home.pug:

html
  head
    //- variable from Webpack config
    title= title
    //- relative path to favicon source file
    link(href="./favicon.ico" rel="icon")
    //- relative path to SCSS source file
    link(href="./style.scss" rel="stylesheet")
    //- relative path to JS source file -->
    script(src="./main.js" defer="defer")
  body
    h1 Hello World!
    //- relative path to image source file
    img(src="./picture.png")

All source filenames should be relative to the entrypoint template, or you can use Webpack alias. The references are rewritten in the generated HTML so that they link to the correct output files.

The generated HTML contains URLs of the output filenames:

<html>
  <head>
    <title>Homepage</title>
    <link href="img/favicon.3bd858b4.ico" rel="icon" />
    <link href="css/style.05e4dd86.css" rel="stylesheet" />
    <script src="js/main.f4b855d8.js" defer="defer"></script>
  </head>
  <body>
    <h1>Hello World!</h1>
    <img src="img/picture.58b43bd8.png" />
  </body>
</html>

If the entry plugin option is a path, the plugin finds all templates automatically and keep the same directory structure in the output directory.

If the entry plugin option is an object, the key is an output filename without .html extension and the value is a template file.

Very simple and clear webpack.config.js, all relevant settings are in one place, in plugin options:

const path = require('path');
const PugPlugin = require('pug-plugin');

module.exports = {
  output: {
    path: path.join(__dirname, 'dist/'),
  },

  plugins: [
    new PugPlugin({
      // automatically processing all templates in the path
      entry: 'src/views/',
      // - OR - define many pages manually (key is output filename w/o `.html`)
      entry: {
        // simple page config w/o variables
        index: 'src/views/home.pug', // => dist/index.html
        // advanced page config with variables
        'news/sport': { // => dist/news/sport.html
          import: 'src/views/news/sport/index.pug', // template file
          data: { title: 'Sport news' }, // pass variables into template
        },
      },
      data: {...}, // pass global data into all templates
      js: {
        // JS output filename, used if `inline` option is false (defaults)
        filename: 'js/[name].[contenthash:8].js',
        //inline: true, // inlines JS into HTML
      },
      css: {
        // CSS output filename, used if `inline` option is false (defaults)
        filename: 'css/[name].[contenthash:8].css',
        //inline: true, // inlines CSS into HTML
      },
    })
  ],

  module: {
    rules: [
      {
        test: /\.(s?css|sass)$/,
        use: ['css-loader', 'sass-loader']
      },
      {
        test: /\.(ico|png|jp?g|webp|svg)$/,
        type: 'asset/resource',
        generator: {
          filename: 'img/[name].[hash:8][ext][query]',
        },
      },
    ],
  },
};

Note

No additional plugins or loader required.

<a id="using-html-webpack-plugin" name="using-html-webpack-plugin"></a>

Using with html-webpack-plugin

Warning

This way is not recommended!

Install the @webdiscus/pug-loader only if you use the html-webpack-plugin.

npm install @webdiscus/pug-loader html-webpack-plugin --save-dev

Install additional packages for styles:

npm install css-loader sass-loader sass --save-dev

Install additional plugin to extract CSS:

npm install mini-css-extract-plugin --save-dev

Using the html-webpack-plugin you should require a source asset file in the Pug template.

For example, there is the template ./src/views/home.pug:

html
  head
    //- variable from plugin options, very ugly access
    title= htmlWebpackPlugin.options.data.title
    //- relative path to favicon source file
    link(href=require("./favicon.ico") rel="icon")
    //- JS and CSS will be injected into HTML automatically, anywhere here
    //- Note: you have no control over the position or order of injected files
  body
    h1 Home
    //- relative path to image source file
    img(src=require("./picture.png"))

Very complex webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  output: {
    path: path.join(__dirname, 'dist/'),
    // JS output filename must be defined only here
    filename: 'js/[name].[contenthash:8].js',
  },

  entry: {
    // Source files of styles and scripts must be defined here, separately from their templates.
    // How to bind each generated bundle to the HTML page?
    // Answer: using the `chunks` plugin option.
    index: ['./src/views/home/main.js', './src/views/home/style.scss'],
    'news/sport': ['./src/views/news/sport/main.js', './src/views/news/sportstyle.scss'],
  },

  plugins: [
    // For one page must be initialized the plugin instance.
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src/views/home/index.pug'),
      // HTML output filename
      filename: 'index.html',
      // bind the generated JS and CSS files to this template via chunks,
      // this is a very terrible "crutch"
      chunks: ['index'],
      // pass variables into template,
      // access in template is very ugly: `htmlWebpackPlugin.options.data.title`
      data: { title: 'Home' }
    }),

    // For other page must be initialized yet one plugin instance.
    // It's very very bad practice and ugly syntax!
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src/views/news/sport/index.pug'),
      // HTML output filename
      filename: 'news/sport.html',
      // bind the generated JS and CSS files to this template via chunks option,
      // you're not confused yet using chunks?
      chunks: ['news/sport'],
      // access in template is very ugly: `htmlWebpackPlugin.options.data.title`,
      // using `pug-plugin`, the variable in Pug is accessible w/o any scope: `title` 
      // (of cause, in `pug-plugin` you can define a variable scope, if you want)
      data: { title: 'Sport news' }
    }),
    
    // ... Do you have the joy of adding yet one page using the HtmlWebpackPlugin?
    // No? Then try to use the `pug-plugin`.

    // Yet one plugin to extract CSS and inject one into HTML.
    new MiniCssExtractPlugin({
      // CSS output filename defined in another place, here
      filename: 'css/[name].[contenthash:8].css',
    }),
  ],

  module: {
    rules: [
      // requires to define the pug loader
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
      },
      {
        test: /\.(s?css|sass)$/,
        // requires additional MiniCssExtractPlugin loader
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
      },
      {
        test: /\.(png|jpe?g|ico)/,
        type: 'asset/resource',
        generator: {
          filename: 'img/[name].[hash:8][ext]',
        },
      },
    ],
  },
};

Why do many developers switch from Webpack to other bundlers? One of the reasons they cite is the complex configuration many different plugins and loaders for one simple thing - rendering an HTML page with assets.

The pug-plugin "changes the rule of the game". Just one plugin replaces the functionality of many plugins and loaders and makes configuration very simple and clear.

Using in JavaScript

A Pug template can be used in JavaScript code as template function with custom data.

Install the pug-loader.

npm install @webdiscus/pug-loader --save-dev

Change your webpack.config.js according to the following minimal configuration:

const path = require('path');

module.exports = {
  output: {
    path: path.join(__dirname, 'public/'),
    publicPath: '/', // must be defined any path, `auto` is not supported yet
  },
  entry: {
    index: './src/index.js', // load a Pug template in JS
  },
  module: {
    rules: [
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
      },
    ],
  },
};

Load a Pug template in JavaScript. Optional you can pass any data into generated template function.

./src/index.js

const tmpl = require('template.pug');
const html = tmpl({
  myVar: 'value',
});

<a id="options" name="options" href="#options"></a>

Original options

See original description of options

basedir

Type: string Default: /<br> The root directory of all absolute inclusion.

doctype

Type: string Default: html<br> Specifies the type of document. See available doctypes.

self

Type: boolean Default: false<br> Use the self as namespace for the local variables in template. It will speed up the compilation, but for access to variable, e.g. myVariable, you must write self.myVariable.

globals

Type: Array<string> Default: []<br> Add a list of global names to make accessible in templates.

filters

Type: object Default: undefined<br> Filters let to use other languages in Pug templates. You can add your own custom filters to Pug. See the build-in filters.

plugins

Type: Array<Object> Default: []<br> Plugins allow to manipulate Pug tags, template content in compile process. How it works see in source of pug.

compileDebug

Type: boolean Default: false<br> Includes the function source in the compiled template to improve error reporting.

pretty

Type: boolean Default: false<br> This option is deprecated by pugjs and always is false. Don't use it.

Additional options

mode

Warning

Since the version 2.11.0, the method option name is renamed into mode.
The method option is DEPRECATED.

The method values are renamed:

The method option name and old values can be used until the next major version.

Type: string Default: compile<br> Values:

Asset resources such as img(src=require('./image.jpeg')) are handled at compile time by the webpack using asset/resource.

esModule

Type: Boolean Default: false<br> Enable / disable ESM syntax in generated JS modules.
Values:

Note: The option esModule is irrelevant for the html mode, because it returns a pure HTML string.

💡 For generates smaller and faster template function, it is recommended to use following options:

{
  mode: 'render',
  esModule: true,
}

data

Type: Object Default: {}<br> The custom data will be passed in all Pug templates, it can be useful by pass global data.

⚠️ Limitation with the compile mode.
A string representing the source code of the function is limited by the function.toString(), see examples.
For native work of the function passed via the data loader option, use the render mode.

embedFilters

Type: Object Default: undefined<br> Enable embedded Pug filters. To enable a filter, add the following to the pug-loader options:

{
  embedFilters: {
    <FILTER_NAME> : <FILTER_OPTIONS> | <TRUE>,
  }
}

Where <FILTER_NAME> is the name of a built-in filter, the available filters see below. The filter can have options <FILTER_OPTIONS> as an object. If the filter has no options, use true as an option to enable the filter.

See the complete information on the pug filter site and in the sources.

watchFiles

Type: Array<RegExp|string> Default: [ /\.(pug|jade|js.{0,2}|.?js|ts.?|md|txt)$/i ]<br> This option allows you to configure watching of individual resolved dependencies.
The default value enables watching of Pug, scripts, markdown, etc. and ignores images, styles to avoid double processing via Webpack and via Pug's ist own compiler.

In some cases, you may want to use one SCSS file for styling and include another SCSS file with a Pug filter for code syntax highlighting. The first SCSS file is watched via Webpack, but changes in the second will be ignored.
For example, we want to watch for changes in all source examples such as main.c, colors.scss, etc. from the /code-samples/ folder, to do this, add to the watchFiles option:

{
  watchFiles: [
    /\\/code-samples\\/.+$/,
  ]
}

For watching of a file, add full path, for example:

{
  watchFiles: [
    path.join(__dirname, './src/config.yml'),
  ]
}

Note: Default value of watchFiles will be extends, not overridden.


<a id="mode-compile" name="mode-compile" href="#mode-compile"></a>

Using compile mode

This mode is used by default.
In JavaScript the required template will be compiled into template function.
In webpack config add to module.rules:

{
  test: /\.pug$/,
    loader: '@webdiscus/pug-loader',
    options: {
    mode: 'compile' // default mode `compile` can be omitted
  }
}

In JavaScript, the result of require() is a template function. Call the template function with some variables to render it to HTML.

const tmpl = require('template.pug');
const html = tmpl({ key: 'value' }); // the HTML string

To render the Pug direct into HTML, use the query parameter ?render.

// compile into template function, because loader option 'mode' defaults is 'compile'
const tmpl = require('template.pug');
const html = tmpl({ key: 'value' });

// render the Pug file into HTML, using the parameter 'render'
const html2 = require('template2.pug?render');

Note: If the query parameter render is set, then will be used rendering for this template, independent of the loader option mode. Variables passed in template with mode render will be used at compile time.


<a id="mode-render" name="mode-render" href="#mode-render"></a>

Using render mode

This mode will render the Pug into HTML at compile time.
In webpack config add to module.rules:

{
  test: /\.pug$/,
    loader: '@webdiscus/pug-loader',
    options: {
      mode: 'render',
    },
}

In JavaScript the result of require() is an HTML string.

const html = require('template.pug'); // the HTML string

To generate a template function for passing the data in Pug at realtime, use the query parameter ?compile.

// render into HTML, because loader option 'mode' is 'render'
const html = require('template.pug');

// compile into template function, using the parameter 'compile'
const tmpl2 = require('template2.pug?compile');
const html2 = tmpl2({ ... });

<a id="mode-html" name="mode-html" href="#mode-html"></a>

Using html mode

This mode will render the Pug to pure HTML and should be used with an additional loader to handle HTML.
In webpack config add to module.rules:

{
  test: /\.pug$/,
    use: [
    {
      loader: 'html-loader',
      options: {
        esModule: false, // allow to use the require() for load a template in JavaScript
      },
    },
    {
      loader: '@webdiscus/pug-loader',
      options: {
        mode: 'html',
      },
    },
  ],
}

In JavaScript the result of require() is an HTML string:

const html = require('template.pug'); // the HTML string

<a id="embed-filters" name="embed-filters" href="#embed-filters"></a>

Built-in filters

The goal of built-in filters is to use most useful lightweight filters without installation. The built-in filters are custom filters that are collected in one place. These filters can be simply enabled via an option.
See the complete information on the pug filter site and in the sources.

Defaults all built-in filters are disabled. Enable only filters used in your Pug templates.

<a id="filter-escape" name="filter-escape" href="#filter-escape"></a>

:escape

The filter replaces reserved HTML characters with their corresponding HTML entities to display these characters as text.

Filter options: none.

Enable the filter:

{
  test: /\.pug$/,
    loader: '@webdiscus/pug-loader',
    options: {
    // enable built-in filters
    embedFilters: {
      escape: true, // enable the :escape filter
    },
  },
},

Using the :escape filter in pug:

pre: code.language-html
:escape
<h1>Header</h1>

Generated HTML:

<pre>
  <code class="language-html">
    &lt;h1&gt;Header&lt;/h1&gt;
  </code>
</pre>

Inline syntax:

p.
The #[:escape <html>] element is the root element.<br>
Inside the #[:escape <html>] element there is a #[:escape <body>] element.

Generated HTML:

<p>The &lt;html&gt; element is the root element.<br>
  Inside the &lt;html&gt; element there is a &lt;body&gt; element.</p>

For more information and examples, see the :escape site.

<a id="filter-code" name="filter-code" href="#filter-code"></a>

:code

The filter wraps a content with the <code> tag.

Filter options:

Enable the filter:

{
  test: /\.pug$/,
    loader: '@webdiscus/pug-loader',
    options: {
    // enable built-in filters
    embedFilters: {
      // enable the :code filter
      code: {
        className: 'language-', // class name of `<code>` tag, needed for `prismjs` theme
      },
    },
  },
},

Usage examples:

Pug: #[:code function() { return true }]
Display: function() { return true }

Pug: #[:code:escape <div>]
Display: <div>

Pug: #[:code:highlight(html) <div class="container">content</div>]
Display highlighted code: <div class="container">content</div>

For more information and examples, see the :code site.

<a id="filter-highlight" name="filter-highlight" href="#filter-highlight"></a>

:highlight

The filter highlights code syntax.

Filter options:

Enable the filter:

{
  embedFilters: {
    highlight: {
      verbose: true,
        use: 'prismjs',
    },
  },
}

Usage example:

pre.language-: code
  :highlight(html)
    <!-- Comment -->
    <h1>Header</h1>
    <p>Text</p>

For more information and examples, see the :highlight site.

<a id="filter-markdown" name="filter-markdown" href="#filter-markdown"></a>

:markdown

The filter transform markdown to HTML and highlights code syntax.

The :markdown filter require the markdown-it and prismjs modules:

npm install -D markdown-it prismjs

Enable the filter:

{
  test: /.pug$/,
    loader: '@webdiscus/pug-loader',
    options: {
    // enable built-in filters
    embedFilters: {
      // enable :markdown filter
      markdown: {
        // enable highlighting in markdown
        highlight: {
          verbose: true,
            use: 'prismjs',
        },
      },
    },
  },
},

The highlight options:

Usage example:

  :markdown
    _HTML_
    ```html
    <!-- Comment -->
    <div class="container">
      <p>Paragraph</p>
    </div>
    ```
    _JavaScript_
    ```js
    const arr = [1, 2, 'banana'];
    ```

Display highlighted code blocks:

HTML

<!-- Comment -->
<div class="container">
  <p>Paragraph</p>
</div>

JavaScript

const arr = [1, 2, 'banana'];

For more information and examples, see the :markdown site.


<a id="passing-data-into-template" name="passing-data-into-template" href="#passing-data-into-template"></a>

Passing data into template

In JavaScript

By default, the Pug file is compiled as template function, into which can be passed an object with template variables.

const tmpl = require('template.pug');
const html = tmpl({
  myVar: 'value',
  foo: 'bar'
});

But how pass variables in template which is rendered into HTML?
Variables can be passed with query parameters.

const html = require('template.pug?myVar=value&foo=bar');

or as a JSON object:

const html = require('template.pug?' + JSON.stringify({ myVar: 'value', foo: 'bar' }));

Use variables myVar and foo in Pug template.

div The value of "myVar": #{myVar}
div The value of "foo": #{foo}

Usage of query parameters is legal and official documented feature of webpack loader.

In webpack.config.js

Pass myData object via query.

entry: {
  about: './src/pages/about.pug?myData=' + JSON.stringify({ title: 'About', options: { uuid: 'abc123' } })
}

Use the object myData in Pug template.

html
head
  title= myData.title
body
  div UUID: #{myData.options.uuid}

To pass global data to all Pug templates, add the loader options data as any object.

module.exports = {
  module: {
    rules: [
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
        options: {
          data: {
            htmlLang: 'en-EN',
            getKeywords: () => {
              const keywords = ['webpack', 'pug', 'loader'];
              return keywords.join(',');
            }
          }
        }
      },
    ],
  },
};

Use the custom data and function in pug.

html(lang=htmlLang)
head
  meta(name="keywords" content=getKeywords())
body

Passing data in HtmlWebpackPlugin

The user data can be passed into Pug template with two ways:

module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      title: 'The some page', // avaliable in Pug as `htmlWebpackPlugin.options.title`
      template: path.join(__dirname, 'src/index.pug?' + JSON.stringify({ myVar: 'value' })), // avaliable as `myVar`
      filename: 'index.html',
    }),
  ],
  module: {
    rules: [
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
      },
    ],
  },
};

Use the passed variables htmlWebpackPlugin.options and myVar in Pug template:

html
  head
    title= htmlWebpackPlugin.options.title
  body
    div= myVar

Load a static data in the pug

You can load data directly in pug.
data.json

[
  { "id": 1, "name": "abc" },
  { "id": 2, "name": "xyz" }
]

Require the JSON file in pug.

- var myData = require('./data.json')
each item in myData
  div #{item.id} #{item.name}

<a id="using-resources" name="using-resources" href="#using-resources"></a>

Using resources

To handle resources in Pug use the require() function:

img(src=require('./path/to/images/logo.png'))

For images, add the following rule to the webpack module:

module.exports = {
  module: {
    rules: [
      {
        test: /\.(png|jpg|jpeg|svg|ico)/,
        type: 'asset/resource',
        generator: {
          filename: 'assets/images/[name].[hash:8][ext]',
        },
      },
    ]
  },
};

For fonts, add the following rule to the webpack module:

module.exports = {
  module: {
    rules: [
      {
        test: /\.(woff2|woff|ttf|svg|eot)/,
        type: 'asset/resource',
        generator: {
          filename: 'assets/fonts/[name][ext]',
        },
      },
    ]
  },
};

More information about asset-modules see here.

Example of dynamic interpolation of image src in pug:

- files = ['image1.jpeg', 'image2.jpeg', 'image3.jpeg']
each file in files
  img(src=require(`./path/to/${file})`)

<a id="resolve-resource-path" name="resolve-resource-path" href="#resolve-resource-path"></a>

Path Resolving

<a id="resolve-webpack-alias" name="resolve-webpack-alias" href="#resolve-webpack-alias"></a>

Path aliases with Webpack

Recommended to use the Webpack alias to avoid relative paths in Pug.
For example, use the alias Images as path to images:

module.exports = {
  resolve: {
    alias: {
      Images: path.join(__dirname, 'src/assets/images/'),
    },
  }
};

The alias may be used with prefixes ~ or @.
For example, all following aliases resolves the same path:

img(src=require('Images/logo.png'))
img(src=require('~Images/logo.png'))
img(src=require('@Images/logo.png'))

<a id="resolve-tsconfig-alias" name="resolve-tsconfig-alias" href="#resolve-tsconfig-alias"></a>

Path aliases with TypeScript

Using TypeScript you can define an alias in tsconfig.json. But for performance is recommended to use the Webpack alias.
For example, add to tsconfig.json an alias to the paths option:

tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "Images/*": ["assets/images/*"]
    }
  }
}

Warning

The compile mode can resolve the filename as a string only and the filename can't be interpolated.

img(src=require('Images/logo.png')) // It works.

- const file = 'logo.png'
img(src=require('Images/' + file))  // ERROR: Can't be resolved with 'compile' mode. 

<a id="resolve-webpack-context" name="resolve-webpack-context" href="#resolve-webpack-context"></a>

Root path with Webpack context

You can use the Webpack context for a short path in Pug.
Define in Webpack config the context as path to sources:

module.exports = {
  context: path.resolve(__dirname, 'src'),
};

For example, your images are under the path PROJECT_PATH/src/assets/images/, then using the context you can use the root path (relative by context) anywhere:

img(src=require('/assets/images/logo.png'))

Note

You can use the basedir option of pug-loader for same effect:

module.exports = {
  module: {
    rules: [
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
        options: {
          basedir: path.resolve(__dirname, 'src')
        },
      },
    ],
   },
 };

<a id="resolve-relative-path" name="resolve-relative-path" href="#resolve-relative-path"></a>

Relative path

The file in the current- or subdirectory MUST start with ./:

img(src=require('./path/to/logo.png'))

The file in the parent directory MUST start with ../:

img(src=require('../images/logo.png'))

Warning

Following relative path will be resolved with render and html modes, but NOT with compile mode:

  img(src=require('../../images/logo.png'))

This is an interpolation limitation in Webpack.

<a id="resolve-interpolation" name="resolve-interpolation" href="#resolve-interpolation"></a>

Interpolation

You can use the filename as a variable.

Usage examples work with all modes:

- const file = 'logo.png'
img(src=require('./images/' + file))
img(src=require(`./images/${file}`))
img(src=require('../images/' + file))
img(src=require('Images/' + file)) // 'Images' is webpack alias
img(src=require(`Images/${file}`)

Warning

Limitation using the compile mode:
the variable MUST NOT contain a path, only a filename, because is interpolated at compile time.
For example, the 'compile' mode can't resolve following:

  - var file = '../images/logo.png'
  img(src=require(file))

Using a variable with render or html mode, the variable MAY contain a path, because is resolved at runtime.
Following example work only with render or html mode:

- const file = '../relative/path/to/logo.png'
img(src=require(file))
img(src=require('Images/' + file))

In current directory, the filename MUST start with ./:

- const file = './logo.png'
img(src=require(file))

<a id="using-with-angular" name="using-with-angular" href="#using-with-angular"></a>

Using with Angular

Install:

npm i --save-dev @webdiscus/pug-loader pug-plugin-ng

In pug-loader can be used the optional pug-plugin-ng to allow unquoted syntax of Angular: [(bananabox)]="val"

Create the file webpack.config.js in root directory of angular project:

module.exports = {
  module: {
    rules: [
      {
        test: /\.pug$/,
        loader: '@webdiscus/pug-loader',
        options: {
          mode: 'render',
          doctype: 'html',
          plugins: [require('pug-plugin-ng')],
        },
      },
    ],
  },
};

Bind the file webpack.config.js in the Angular config angular.json:

{
  // ...
  "projects": {
    // ...
    "architect": {
      "build": {
        // replace architect.build.builder with this value:
        "builder": "@angular-builders/custom-webpack:browser",
          // add the options:
          "options": {
          "aot": true,
            "customWebpackConfig": {
            "path": "./webpack.config.js" // the path to webpack.config.js
          },
          // ...
        },
        // ...
      },
      "serve": {
        // replace architect.serve.builder with this value:
        "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
          "browserTarget": "<app-name>:build"
        },
        // ...
      },
      // ...
    },
  },
},

In a component file, e.g. ./src/app/app.component.ts set the templateUrl with Pug file:

import { Component } from '@angular/core';

// the variable `description` will be passed into Pug template via resource query
const templateVars = '{"description": "Use Pug template with Angular."}';

@Component({
  selector: 'app-root',
  styleUrls: ['./app.component.css'],
  templateUrl: './app.component.pug?' + templateVars,
})
export class AppComponent {
  title = 'ng-app';
}

Create a Pug template, e.g. ./src/app/app.component.pug:

h1 Hello Pug!
p Description: #{description}

See the complete source of the example.


<a id="using-with-vue" name="using-with-vue" href="#using-with-vue"></a>

Using with Vue

Install:

npm i --save-dev @webdiscus/pug-loader

Change your vue.config.js according to the following minimal configuration:

const { defineConfig } = require('@vue/cli-service');

// additional pug-loader options, 
// e.g. to enable pug filters such as `:highlight`, `:markdown`, etc.
// see https://github.com/webdiscus/pug-loader#options
const pugLoaderOptions = {
};

module.exports = defineConfig({
  transpileDependencies: true,

  chainWebpack: (config) => {
    // clear all existing pug loaders
    const pugRule = config.module.rule('pug');
    pugRule.uses.clear();
    pugRule.oneOfs.clear();

    // exclude `pug-loader` from the witchery of the baggy `thread-loader` that is used in production mode
    const jsRule = config.module.rule('js');
    jsRule.exclude.add(/pug-loader/);
  },

  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.pug$/,
          oneOf: [
            // allow <template lang="pug"> in Vue components
            {
              resourceQuery: /^\?vue/u,
              loader: '@webdiscus/pug-loader',
              options: {
                mode: 'html', // render Pug into pure HTML string
                ...pugLoaderOptions,
              },
            },
            // allow import of Pug in JavaScript
            {
              loader: '@webdiscus/pug-loader',
              options: {
                mode: 'compile', // compile Pug into template function
                ...pugLoaderOptions,
              },
            },
          ],
        },
      ],
    },
  },
});

For additional information see please the discussion: How to configure the plugin for both Vue and non-Vue usage?

Using Pug in Vue template

<template lang='pug'>
  h1 Hello Pug!
  p Use the '@webdiscus/pug-loader'
</template>

Note

You can use an indent for Pug code in Vue template.

Using Pug in JavaScript

App.vue

<template>
  <div v-html='demo'></div>
</template>

<script>
  // import Pug as template function
  import demoTmpl from './views/demo.pug';
  
  // define custom data used in Pug template
  const locals = { colors: ['red', 'green', 'blue'] };
  // pass custom data in Pug template
  const demoHtml = demoTmpl(locals);

  export default {
    name: 'App',
    data() {
      return {
        demo: demoHtml
      }
    }
  }
</script>

demo.pug

each color in colors
  div(style=`color: ${color}`) #{color}

Note: The colors is external variable passed from App.vue.


<a id="recipes" name="recipes" href="#recipes"></a>

Recipes

Resolving the attribute srcset in img tag

img(srcset=`${require('./image1.jpeg')} 320w, ${require('./image2.jpeg')} 640w` src=require('./image.jpeg'))

output

<img srcset="/assets/image1.f78b30f4.jpeg 320w, /assets/image2.f78b30f4.jpeg 640w" src="/assets/image.f78b30f4.jpeg">

Using JavaScript in Pug

Use the require() for CommonJS files in Pug templates.
The JS module say-hello.js

module.exports = function(name) {
  return `Hello ${name}!`;
}

Use the module sayHello in Pug template.

- var sayHello = require('./say-hello')
h1 #{sayHello('pug')}

Testing

npm run test will run the unit and integration tests.
npm run test:coverage will run the tests with coverage.


Also See

License

ISC