pugjs / pug

Pug – robust, elegant, feature rich template engine for Node.js
https://pugjs.org
21.66k stars 1.96k forks source link

How to provide my own implementation of pug-load? #2790

Open callumlocke opened 7 years ago

callumlocke commented 7 years ago

I asked about this a couple of years ago but it was never resolved.

I want to tell pug to use my own fork of pug-load instead of the real pug-load.

pug.compile(templateString, {
  filename: 'foo.pug',
  basedir: somePath,
  loader: myCustomPugLoadFork // THIS IS WHAT I WANT
});

But that loader option doesn't exist, as far as I can tell. Is there any way to configure pug to use my loader?

ForbesLindesay commented 7 years ago

You can't do this, but what you can do is pass in a plugin that overrides both the resolve and the read method:

const customLoadPlugin = {
  // if you do not provide both of these methods, the default implementation is used for the other method.
  resolve(filename, source, loadOptions) {
    // return a fully resolved path from the relative `filename` and the `source` filename
  },
  read(filename, loadOptions) {
    // return the pug source code as a string for `filename`.
  },
};

pug.compile(templateString, {
  filename: 'foo.pug',
  basedir: somePath,
  plugins: [customLoadPlugin],
});
ForbesLindesay commented 7 years ago

Currently this is un-documented, so there is still some work for us to do here.