I created a browserify transform to transform .pug
files to preparsed Ractive JSON objects. That operation requires to transform Pug files to HTML and then pass to the Ractive.parse()
function.
What doesn't work after initial build is that when I change a relative Pug file, browserify doesn't recompile the parent file, so the change is not reflected to the bundle file. I'm using watchify
to watch the file changes.
Consider the following hello.js
file:
var x = require('./index.pug')
...
and index.pug
file:
b hello
include ./a.pug
I can obtain ["/path/to/index.pug", "/path/to/a.pug"]
inside my transform function. How can I pass it to the browserify to invalidate its relevant cache whenever index.pug
or a.pug
has been changed within the transform function?
My transform function is as follows:
var through2 = require('through2')
preparsePug = function(file, contents){
// returns {
// dependencies (Array),
// parsed (String)
// }
...
}
function isTemplate(file){
return /.*\.(pug)$/.test(file);
}
exports.ractivePreparserify = function(file){
var contents, write, flush;
if (!isTemplate(file)) {
return through();
}
contents = '';
write = function(chunk, enc, next){
contents += chunk.toString('utf-8');
next();
};
flush = function(cb){
try {
var x = preparsePug(file, contents);
this.push("module.exports = " + JSON.stringify(x.parsed));
return cb();
} catch (err) {
console.error("Preparserify error: ", err);
return this.emit('error', err);
}
};
return through2.obj(write, flush);
};