TL;博士
我想将一堆 *.tpl 文件预编译为可以在 main.js 中需要和使用的 templates.js 文件。我还希望 webpack 处于监视模式以捕获 .tpl 文件中的更改。
更长的描述
插件伪代码是这样的:
function Tmpl(options) {}
Tmpl.prototype.apply = function(compiler) {
compiler.plugin('compilation', (compilation) => {
// Get template files and bundle them all to one file
var code = getFilesAndBundleStuff();
// Write the output to a file to be required in main.js
fs.writeFileSync(path.join(__dirname, 'templates.js'), code);
});
};
module.exports = Tmpl;
将结果写入新文件的最后一部分,触发了一堆监视事件,事情变得很糟糕。我不确定“编译”是链中完成这项工作的正确位置,但我是 webpack 的新手,不知道该链是如何工作的。
整个事情在正常的 webpack 模式下运行良好,但是让它在 --watch 模式下运行会很棒。