[英]How to include html partials with webpack (version 2) with the html-loader?
[英]Include HTML partials with Webpack
我使用Nunjucks并在我的Webpack中运行一个观察者/构建器。
var Nunjucks = require('nunjucks');
var webpack = require('webpack');
// Set up templating.
var nunjucks = Nunjucks.configure(path.resolve(__dirname, 'src'), {noCache: true});
// Initial Nunjucks render.
var html = nunjucks.render('index.html', getContext());
fs.writeFileSync('index.html', html);
// For development, watch HTML for changes to compile Nunjucks.
// The production Express server will handle Nunjucks by itself.
if (process.env.NODE_ENV !== 'production') {
fs.watch('src', {recursive: true}, (eventType, filename) => {
if (filename.indexOf('.html') === -1) {
return;
}
console.log(`${filename} updated.`);
try {
fs.writeFileSync('index.html', nunjucks.render('index.html', getContext()));
} catch (e) {
console.error(e);
}
});
}
// ...
我的树:
index.html // Compiled HTML.
src/
index.html // Template/Nunjucks HTML.
templates/
somePartial.html
然后包括:
{% include "./templates/somePartial.html" %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.