HtmlWebpackPlugin - ERROR in Error: The loader "[...]/html-webpack-plugin - /lib/loader.js!/[...]/src/index.html" didn't return html

Lhe*_*hew 3 javascript build webpack

I'm trying to load an HTML template using HtmlWebpackPlugin and it seems not working. If I try to load the plugin without arguments it works. Any ideas?

The index.html file that I'm trying to load is in the right place, just to consider

package.json:

{
 ...
  "devDependencies": {
    "@babel/core": "^7.7.4",
    "@storybook/html": "^5.2.8",
    "babel-loader": "^8.0.6",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "css-loader": "^3.2.1",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.13.0",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.1",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.3",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "axios": "^0.19.0"
  }
}

Run Code Online (Sandbox Code Playgroud)

webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  devtool: 'source-map',
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },

  module: {
    rules: [
      {
        test: /\.s[ac]ss$/i,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'sass-loader'
          }
        ]
      },
      {
        test: /.(ts|tsx)?$/,
        loader: 'ts-loader',
        include: [path.resolve(__dirname, 'src')],
        exclude: [/node_modules/]
      }
    ]
  },
  plugins: [
    new webpack.ProgressPlugin(),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'src/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css'
    })
  ],
  devServer: {
    hot: true,
    host: '0.0.0.0'
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js', '.scss', '.css']
  }
};
Run Code Online (Sandbox Code Playgroud)

tru*_*ktr 22

对我来说,解决方案是确保所有内容都已更新。

我在 Webpack 5 上,除此之外的所有内容都是html-webpack-plugin最新的。我html-webpack-plugin从 v4更新到 v5,问题就解决了。

  • 我想让您知道,您回答了一年前的问题,仅仅 7 小时后就解决了我的问题。:) (2认同)
  • 天哪,同样,奇怪的。这一定是由于 webpack 最近发生了重大变化,尽管我不记得升级了它...... (2认同)
  • 这实在是太奇怪了!!我突然遇到了这个问题,并坚持我们的产品版本,最终可以通过升级最新版本来解决它! (2认同)

Mr.*_*Mr. 3

来自html-webpack-plugin 文档

不要设置任何加载程序

默认情况下(如果您没有以任何方式指定任何加载程序)会启动后备 lodash 加载程序。

{
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,用作.html模板扩展可能会意外触发另一个加载程序。

请先按照文档查看是否存在加载程序冲突。

  • 我收到这个错误。我没有加载程序冲突。我没有为“.html”文件设置任何加载程序。 (3认同)