HtmlWebpackInlineSourcePlugin TypeError:无法读取未定义的属性“ getHooks”

时间:2020-02-26 22:12:15

标签: webpack webpack-4

尝试构建以下Webpack文件时,标题出现错误:

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

module.exports = {
  module: {
    rules: [{
        test: /\.js$/,
        exclude: /(node_modules)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.(scss)$/,
        use: [{
          loader: 'style-loader', // inject CSS to page
        }, {
          loader: 'css-loader', // translates CSS into CommonJS modules
        }, {
          loader: 'postcss-loader', // Run post css actions
          options: {
            plugins: function() { // post css plugins, can be exported to postcss.config.js
              return [
                require('precss'),
                require('autoprefixer')
              ];
            }
          }
        }, {
          loader: 'sass-loader' // compiles Sass to CSS
        }]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html',
      inlineSource: '.(js|css)$'
    }),
    new HtmlWebpackInlineSourcePlugin()
  ],

  entry: './src/index.js',

  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },

  mode: 'development'
};

知道为什么吗?

FWIW我正在使用纱线工作区。

1 个答案:

答案 0 :(得分:3)

HtmlWebpackInlineSourcePlugininvoking getHooks method of HtmlWebpackPlugin during compilation

此方法仅在版本4 and up of HtmlWebpackPlugin中可用。

这就是the documentation recommends使用此版本及更高版本的原因。

您可以通过安装html-webpack-plugin@next

获得此版本。