Pug + webpack-dev-server

7

我正在使用webpack v4,并尝试在webpack-dev-server中使用Pug,但是当我运行webpack-dev-server --mode development时,它没有提供已编译的Pug。请帮忙解决,谢谢。这是我的配置:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/js/main.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.pug$/,
        use: {
          loader: 'pug-loader',
          options: {
            pretty: true
          }
        }
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    hot: true,
    open: true,
    progress: true
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src/templates/pages/index.pug'),
      inject: false
    })
  ]
};


2
你得到答案了吗?我似乎有同样的问题,并且配置也相似。 - MattG
1个回答

0

你需要在HtmlWebpackPlugin中指定文件名,这样你就可以从localhost:3000或localhost:3000/index.html上获取你的html了。

devServer: {
     ...,
     port: 3000
}
...
plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, 'src/templates/pages/index.pug'),
      filename: 'index.html'
    })
  ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接