Add dependency in webpack plugin

Can I add a dependency from the Webpack plugin? I create files that use templates, when these templates change, I would like to webpack --watchrun another assembly.

Here is the plugin:

function BlahPlugin (options) { this.options = options; }

BlahPlugin.prototype.apply = function (compiler) {

  // This is the file that I'd like to "watch"
  var template = this.options.template;

  compiler.plugin('emit', function (compilation, callback) {
    var body = Object.keys(compilation.assets).join("\n");
    require("fs").readFile(template, "utf8", function (err, data) {
      var content = data.replace("{{body}}", body);
      compilation.assets["out.txt"] = {
        source: function () { return content; },
        size:   function () { return content.length; }
      };
      callback();
    });
  });
};

module.exports = BlahPlugin;

This is taken from this full working draft: https://gist.github.com/thatismatt/519d11b2c902791bb74b

If you run ./node_modules/.bin/webpack --watchand modify the js file, compilation automatically starts and creates the compiled js files and out.txt (as indicated in BlahPlugin). But if you change the tmpl.txt file, which is specified in the webpack configuration and used in BlahPlugin, compilation does not restart. (This is to be expected). But this is what I want, how can I tell Webpack to “see” this file?

+4
1

, :

compiler.plugin("emit", function (compilation, callback) {
  compilation.fileDependencies.push(path.join(compiler.context, template));
  // ...
});

, : https://gist.github.com/thatismatt/519d11b2c902791bb74b

: , .

+5

All Articles