3

Using pug-loader how can I make some vairable accessible to all pug/jade files. For example in express app I could do:

app.locals.assetPath = path.resolve('public/assets');

and the variable assetPath would be available in all jade files. But with webpack I am not able to do the same.

In my webpack.config.js I tried following but could not get work:

{
  test: /.pug$/,
  loader: 'pug',
  query: {
    root: path.join(__dirname, 'src/app'),

    /*globals: {assetPath: '/hard/coded/value'}*/ //WILL NOT WORK

    /*locals: {assetPath: '/hard/coded/value'}*/ //WILL NOT WORK

    /*locals: {assetPath: '/hard/coded/value'}, globals: ['assetPath']*/ //WILL NOT WORK
  }
Lekhnath
  • 4,532
  • 6
  • 36
  • 62

1 Answers1

4

From my experience, you have two options:

  • use pug-html-loader instead;
  • if you are using HtmlWebpackPlugin plugin, add custom properties to its config. They will be accessible through all pug templates.

Please, take a look at the detailed answer concerning the second and the first options.

Here is also the configuration example for the second option:

pug rule:

{
  test: /\.pug$/,
  loader: 'pug-loader',
  options: {
    // Use `self` namespace to hold the locals
    // Not really necessary
    self: true,
  },
}

HtmlWebpackPlugin options:

{
  filename: 'index.html',
  template: 'src/html/index.pug',
  // Your custom variables:
  production: (process.env.NODE_ENV === 'production')
}

index.pug template:

- const production = self.htmlWebpackPlugin.options.production

if production
  link( rel="stylesheet", type="text/css", href="style.css" )
Community
  • 1
  • 1
edloidas
  • 316
  • 3
  • 8