Ram*_*gov 3 html javascript lodash webpack
我在index.html中创建了模板,用js生成html代码,代码如下.我的Webpack配置也在下面.当我用webpack-dev-server运行它时,我收到错误:标题未定义.不知何故,webpack尝试通过self解析'title',而不是将其委托给'lodash/template'.请帮我修改代码,我绝望了(.
import path from 'path';
import glob from 'glob';
import webpack from 'webpack';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
const inProduction = process.env.mode === 'production';
export default {
entry: {
app: [
'./src/scripts/main.js',
],
},
output: {
path: path.join(__dirname, 'build'),
filename: '[name].[chunkhash].js',
},
module: {
rules: [
{
test: /\.s[ac]ss$/,
use: ExtractTextPlugin.extract({
use: ['css-loader', 'sass-loader'],
fallback: 'style-loader',
}),
},
{
test: /\.js$/,
use: 'babel-loader',
exclude: '/node_modules',
},
],
},
plugins: [
new ExtractTextPlugin('[name].[chunkhash].css'),
new webpack.LoaderOptionsPlugin({
minimize: inProduction,
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, './src/index.html'),
}),
],
};Run Code Online (Sandbox Code Playgroud)
import temp from 'lodash/template'
import data from './data';
const titlePicDiscHalf = temp(document.getElementById('titlePicDiscHalf').innerHTML);
document.write(titlePicDiscHalf({ title: 'Hello World!' }));Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<script type="text/template" id="titlePicDiscHalf">
<div class="titlePicDiscHalf">
<div class="picture"></div>
<div class="title"><%=title%></div>
<div class="discription"></div>
<div class="buttons"></div>
</div>
</script>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
问题是html-webpack-plugin使用相同的模板标签<%= %>将bundle信息插入到模板中.
你有两个选择.
您可以将客户端lodash/template使用的分隔符更改为其他内容,因此Webpack会忽略它.例如:
_.templateSettings.interpolate = /<\$=([\s\S]+?)\$>/g;
Run Code Online (Sandbox Code Playgroud)
看看这个演示.
_.templateSettings.interpolate = /<\$=([\s\S]+?)\$>/g;
const temp = _.template
const titlePicDiscHalf = temp(document.getElementById('titlePicDiscHalf').innerHTML);
document.write(titlePicDiscHalf({ title: 'Hello World!' }));Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
<script type="text/template" id="titlePicDiscHalf">
<div class="titlePicDiscHalf">
<div class="picture"></div>
<div class="title">
<$=title$>
</div>
<div class="discription"></div>
<div class="buttons"></div>
</div>
</script>Run Code Online (Sandbox Code Playgroud)
ejs-loader单独安装并配置html-webpack-plugin为使用它来加载模板.在那里你可以改变你的分隔符.它可能看起来像这样:
plugins: [
new HtmlWebpackPlugin({
template: './index.html.ejs',
})
],
module: {
rules: [
{ test: /\.ejs$/, loader: 'ejs-loader', query: {
interpolate: /<\$=([\s\S]+?)\$>/g,
evaluate: /<\$([\s\S]+?)\$>/g,
}},
]
},
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用两组不同的分隔符配置模板,一组用于客户端软件包lodash模板,另一组用于html-webpack-plugin:
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title><$= htmlWebpackPlugin.options.title $></title>
</head>
<body>
<script type="text/template" id="titlePicDiscHalf">
<div class="titlePicDiscHalf">
<div class="picture"></div>
<div class="title"><%= title %></div>
<div class="discription"></div>
<div class="buttons"></div>
</div>
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
注意,<title><$= htmlWebpackPlugin.options.title $></title>由webpack使用,<div class="title"><%= title %></div>并由客户端lodash使用.
| 归档时间: |
|
| 查看次数: |
1948 次 |
| 最近记录: |