Javascript 在网页包插件中添加依赖项
是否可以从网页包插件中添加依赖项?我正在生成使用模板的文件,当这些模板更改时,我希望Javascript 在网页包插件中添加依赖项,javascript,webpack,Javascript,Webpack,是否可以从网页包插件中添加依赖项?我正在生成使用模板的文件,当这些模板更改时,我希望webpack--watch触发另一个构建 以下是插件: function BlahPlugin (options) { this.options = options; } BlahPlugin.prototype.apply = function (compiler) { // This is the file that I'd like to "watch" var template = this
webpack--watch
触发另一个构建
以下是插件:
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;
这是从这个完整的工作项目中提取的:
如果运行
/node\u modules/.bin/webpack--watch
并修改js文件,编译将自动触发并生成已编译的js文件和out.txt(如BlahPlugin中所指定)。但是如果您更改了在webpack配置中指定并在BlahPlugin中使用的tmpl.txt文件,则编译不会重新触发。(这是意料之中的)。但这正是我希望发生的事情,我如何告诉Webpack“监视”该文件?我通过添加以下内容修复了此问题:
compiler.plugin("emit", function (compilation, callback) {
compilation.fileDependencies.push(path.join(compiler.context, template));
// ...
});
我还更新了要点,因此您可以在那里看到完整的修复:
注意:这是可行的,但有点麻烦。更清楚地解释您的需求please@Darshan好的,这不是很清楚,我已经更新了我的问题。希望你现在能帮忙。谢谢你的时间:)这不是黑客。您可以自由修改
fileDependencies
数组,以添加在此编译过程中需要关注的任何源。您如何理解“emit”和“fileDependencies”的含义,以及将它们组合在一起的正确方法?除了逐行调试,还有其他方法吗?我的意思是我可以这样做,但是太无聊了。而你往往会失去大局。@jchnxu这篇关于插件模式的文档可能会有所帮助,这里记录了“emit”编译器钩子:@thatismatt我讨厌这种设计,因为它没有封装(你必须知道整个数据流才能改变一点点***,但你仍然担心破坏其他插件)。但是你的链接非常有用!