我试图用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下)
编辑:更新了代码,仍然存在问题:
“错误中的错误:子编译失败:模块解析失败:意外令牌(1:0)您可能需要一个适当的加载程序来处理此文件类型。
SyntaxError:意外令牌(1:0)模块解析失败:意外令牌(1:2)您可能需要一个适当的加载程序来处理此文件类型。“
发布于 2017-12-15 07:28:59
编辑
(编辑: 2017-12-20将“装载机”移至“规则”)
默认情况下,"html- webpack -plugin“将模板解析为"下划线”(也称为template.html)模板,您的"template.html“没有错,错误是webpack未能解析"template.html”<%= require('html-loader!./footer.html') %>的‘html-加载程序’,因此您需要为template.html安装“html-加载程序”,并对其进行配置:
在命令行中:
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'
}
]
}现在您可以运行" webpack“了,,但是生成的"index.html”不是您所期望的,因为您的模板文件有".html“扩展名,webpack现在使用”html-加载程序“来加载"template.html”,而不是默认的“目录加载器”,为了解决这个问题,您可以将"template.html“重命名为"template.html.ejs”(或任何其他扩展名),以使“html--插件”回退。此外,在"template.html“上还有一些修改,删除”html-加载程序“!从中:
<%= 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!");环境:
运行webpack后的"/dist/index.html“内容:
<!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>如您所见,正确插入了"footer.html“的内容。
旧答案
方法1: 使用"es6“模板
npm install html-loader安装“html-加载程序”模块:{ rules:{ test: /.html$/,加载程序:‘’},}
interpolate标志以启用ES6模板字符串的内插语法,如下所示:插件:新HtmlWebpackPlugin({模板:‘!!html?插值!src/template.html’})
template.html以匹配ES6模板:webpack,它会工作的方法2:使用“下划线”模板的
遵循“方法1”步骤1和2,然后:
template: './src/template.html'”中将template: './src/template.html.ejs'更改为"webpack.config.js“https://stackoverflow.com/questions/47776459
复制相似问题