ser*_*off 26 html frontend webpack
我正在使用Webpack来编译我的脚本和HTML(使用html-webpack-plugin
).问题是,我有5个包含相同部分的HTML文件,我想将这些部分移动到单独的.html
文件中,然后将这些部分移动到include
每个HTML文件中.这样,如果我将更改这些较小的HTML文件,它将重新编译每个HTML文件以表示这些更改.
默认情况下,Webpack为.js文件执行此操作,但是我可以对HTML文件使用类似的东西吗?
小智 29
您可以<%= require('html!./partial.html') %>
在模板中使用.示例:https://github.com/jantimon/html-webpack-plugin/blob/master/examples/custom-template/template.html
pld*_*ldg 11
另一个略有不同的解
使用html-loader
与interpolate
选择.
https://github.com/webpack-contrib/html-loader#interpolation
{ test: /\.(html)$/,
include: path.join(__dirname, 'src/views'),
use: {
loader: 'html-loader',
options: {
interpolate: true
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在html页面中,您可以导入部分html和javascript变量.
<!-- Importing top <head> section -->
${require('./partials/top.html')}
<title>Home</title>
</head>
<body>
<!-- Importing navbar -->
${require('./partials/nav.html')}
<!-- Importing variable from javascript file -->
<h1>${require('../js/html-variables.js').hello}</h1>
<!-- Importing footer -->
${require('./partials/footer.html')}
</body>
Run Code Online (Sandbox Code Playgroud)
html-variables.js
看起来像这样:
const hello = 'Hello!';
const bye = 'Bye!';
export {hello, bye}
Run Code Online (Sandbox Code Playgroud)
唯一的缺点是你不能HtmlWebpackPlugin
像这样导入其他变量<%= htmlWebpackPlugin.options.title %>
(至少我找不到导入它们的方法)但对我来说这不是问题,只需在你的html中写标题或使用单独的javascript文件用于句柄变量.
归档时间: |
|
查看次数: |
14276 次 |
最近记录: |