我尝试使用HTML Webpack插件创建静态HTML部分的设置,但遇到了一些错误。这是我当前的配置:
webpack.config.js
const webpack = require('webpack');
const path = require('path');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssets = require('optimize-css-assets-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
let config = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, './public'),
filename: 'app.js'
},
module: {
loaders: [{
test: /\.html$/,
loader: 'html-loader'
}],
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.scss$/,
use: ['css-hot-loader'].concat(ExtractTextWebpackPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'sass-loader', 'postcss-loader'],
})),
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: ['file-loader?context=src/assets/images/&name=images/[path][name].[ext]', {
loader: 'image-webpack-loader',
query: {
mozjpeg: {
progressive: true,
},
gifsicle: {
interlaced: false,
},
optipng: {
optimizationLevel: 4,
},
pngquant: {
quality: '75-90',
speed: 3,
},
},
}],
exclude: /node_modules/,
include: __dirname,
},
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/template.html.ejs'
}),
new ExtractTextWebpackPlugin('main.css')
],
devServer: {
contentBase: path.resolve(__dirname, './public'),
historyApiFallback: true,
inline: true,
open: true
},
devtool: 'eval-source-map'
}
module.exports = config;
if (process.env.NODE_ENV === 'production') {
module.exports.plugins.push(
new webpack.optimize.UglifyJsPlugin(),
new OptimizeCSSAssets()
);
}
template.html.ejs (位于./src
下)
<%=require('./header.html')%>
<body>
testing schmesting
</body>
<%=require('./footer.html')%>
</html>
(footer.html和header.html位于./src
下)
编辑:更新了代码,仍然发出:
&#34;错误中的错误:子编译失败:
模块解析失败:意外的令牌(1:0)
您可能需要一个适当的加载程序来处理此文件类型。
SyntaxError:意外的令牌(1:0)
模块解析失败:意外的令牌(1:2)
您可能需要一个合适的加载程序来处理此文件类型。&#34;
答案 0 :(得分:9)
编辑
(编辑:2017-12-20移动&#34;装载机&#34;到&#34;规则&#34;)
默认情况下,&#34; html-webpack-plugin&#34;将模板解析为&#34; underscore&#34; (也称为lodash)模板,你的&#34; template.html&#34;没错,错误是由webpack无法解决的问题导致的.html-loader&#39;为您的&#34; template.html&#34;
<%= require('html-loader!./footer.html') %>
,所以你需要安装&#34; html-loader&#34;对于webpack,并配置它:
在命令行中:
npm install html-loader
并为webpack配置,编辑webpack.config.js
:
...
module: {
rules: [
// ...
{
test: /\.html$/, // tells webpack to use this loader for all ".html" files
loader: 'html-loader'
}
]
}
现在你可以运行&#34; webpack&#34;您将看到没有错误,但是生成的&#34; index.html&#34;不是你期望的,因为你的模板文件有&#34; .html&#34;扩展,webpack现在使用&#34; html-loader&#34;加载&#34; template.html&#34;而不是默认&#34; lodash loader&#34;,要解决此问题,您可以重命名&#34; template.html&#34; to&#34; template.html.ejs&#34; (或任何其他扩展名)制作&#34; html-webpack-plugin&#34;倒退。除了&#34; template.html&#34;,删除&#34; html-loader!&#34;还有一点点变化。从它:
<%= require('./footer.html') %>
现在它应该可以工作。
编辑发布我的代码以供参考:
/src/template.html.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>template</h1>
<%=require('./footer.html')%>
</body>
</html>
/src/footer.html
<footer>this is a footer</footer>
/webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
let config = {
entry: {
index: './src/js/index'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.html$/,
loader: 'html-loader'
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/template.html.ejs'
})
]
}
module.exports = config;
/package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"html-loader": "^0.5.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.10.0"
}
}
/src/js/index.js
console.log("A test page!");
环境:
&#34; /dist/index.html"的内容;运行webpack之后:
<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>template</h1>
<footer>this is a footer</footer>
<script type="text/javascript" src="index.js"></script></body>
</html>
正如您可以看到&#34; footer.html&#34;的内容正确插入。
OLD ANSWER
方法1: Using "es6" template
npm install html-loader
module: { rules: [{ test: /\.html$/, loader: 'html-loader' }], }
interpolate
标志以启用ES6模板字符串的插值语法,如下所示:plugins: [ new HtmlWebpackPlugin({ template: '!!html-loader?interpolate!src/template.html' }) ]
template.html
以匹配ES6模板:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> template </body> ${require('./footer.html')} </html>
webpack
,它将方法2:使用&#34;下划线&#34;模板
关注&#34;方法1&#34;步骤1和2,然后:
template: './src/template.html'
更改为template: './src/template.html.ejs'