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);
};
It's the
watchifymodule that does the actual caching between builds.watchifyusesbrowserify's internal cache under the hood, but still we send our signal towatchifyto make browserify rebuild on dependency changes.As can be seen in the source code, a transform informs its browserify instance through watchify about its dependencies by
this.emit('file', dependency)signal.Above
ractivePreparserifyfunction should be:In order this to work, browserify should be configured exactly as explained here.