【发布时间】:2018-05-26 08:45:49
【问题描述】:
我正在尝试使用 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: Unexpected token (1:0)
模块解析失败:意外令牌 (1:2)
你可能需要一个合适的加载器来处理这个文件类型。”
【问题讨论】:
标签: javascript html webpack