我正在尝试使用React和ES6建立一个网站。 我正在使用Webpack来使用Babel来转换我的JS,它运行正常。 现在我需要知道如何在Pug(或HTML)中编写我的模板,并将其添加到Webpack工作流程中。 我希望我的构建文件夹有两个文件:
bundle.js
index.html
文件是从我的index.pug
文件编译的 一个示例webpack.config.js
文件会有所帮助,但我真正想要的只是如何执行此操作的一般概念。
谢谢!
您需要首先安装几个webpack插件才能使用带有webpack的pug模板。
使用htmlwebpack插件,您可以指定您的pug模板文件
new HtmlWebpackPlugin({
template : './index.pug',
inject : true
})
pug模板文件将由pug-loader加载。
{
test: /\.pug$/,
include: path.join(__dirname, 'src'),
loaders: [ 'pug-loader' ]
},
一个示例webpack配置文件可以如下所示 -
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const isTest = process.env.NODE_ENV === 'test'
module.exports = {
devtool: 'eval-source-map',
entry: {
app: [
'webpack-hot-middleware/client',
'./src/app.jsx'
]
},
output: {
path : path.join(__dirname, 'public'),
pathinfo : true,
filename : 'bundle.js',
publicPath : '/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new ExtractTextPlugin("style.css", { allChunks:false }),
isTest ? undefined : new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
}),
new HtmlWebpackPlugin({
template : './index.pug',
inject : true
})
].filter(p => !!p),
resolve: {
extensions: ['', '.json', '.js', '.jsx']
},
module: {
loaders: [
{
test : /\.jsx?$/,
loader : 'babel',
exclude : /node_modules/,
include : path.join(__dirname, 'src')
},
{
test : /\.scss?$/,
loader : ExtractTextPlugin.extract("style-loader", "css-loader!autoprefixer-loader!sass-loader"),
include : path.join(__dirname, 'sass')
},
{
test : /\.png$/,
loader : 'file'
},
{
test : /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
loader : 'file'
},
{
test: /\.pug$/,
include: path.join(__dirname, 'src'),
loaders: [ 'pug-loader' ]
},
{
include : /\.json$/,
loaders : ["json-loader"]
}
]
}
}