You can use the preprocessor
property of the html-loader
to achieve interpolation:preprocessor
I use it like this:
const INCLUDE_PATTERN = /<include src="(.+)"\s*\/?>(?:<\/include>)?/gi
const processNestedHtml = (content, loaderContext, dir = null) =>
!INCLUDE_PATTERN.test(content) ? content : content.replace(INCLUDE_PATTERN, (m, src) => {
const filePath = path.resolve(dir || loaderContext.context, src)
loaderContext.dependency(filePath)
return processNestedHtml(loaderContext.fs.readFileSync(filePath, 'utf8'), loaderContext, path.dirname(filePath))
})
And in webpack: module.rules
:
{
test: /\.html$/, loader: 'html-loader', options: {
preprocessor: processNestedHtml
}
}
Then you can put this <include>
directive in your html partials:
<include src="relative/path/to/partial">
This github issue addresses the problem: https://github.com/webpack-contrib/html-loader/issues/291