html-loader在webpack.config.js配置后仍然无法正常工作。

huangapple go评论68阅读模式
英文:

html-loader doesn't work even though webpack.config.js is configured

问题

If I use require("./about.html"), then I get this error:

> Module parse failed: Unexpected token (1:0) You may need an
> appropriate loader to handle this file type, currently no loaders are
> configured to process this file.

I have installed html-loader, and have set up my webpack.config.js file, but I don't know why I'm still getting this error. Here is my webpack.config.js file:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          minimize: true,
        }
      },
    ],
  },
};
英文:

If I use require("./about.html"), then I get this error:

> Module parse failed: Unexpected token (1:0) You may need an
> appropriate loader to handle this file type, currently no loaders are
> configured to process this file.

I have installed html-loader, and have set up my webpack.config.js file, but I don't know why I'm still getting this error. Here is my webpack.config.js file:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          minimize: true,
        }
      },
    ],
  },
};

答案1

得分: 0

I found a solution that worked for me.

Instead of having the webpack.config file in the root directory, I can change my webpack configurations using react-app-rewired.

Further information here: https://github.com/timarney/react-app-rewired

Then, in the config-overrides.js file, I made these changes:

module.exports = function override(config, env) {
  //do stuff with the webpack config...
  
  let RuleToAdd = {
    test: /\.html$/,
    loader: "html-loader",
    options: {minimize: true}
  };
  config.module.rules.push(RuleToAdd);

  return config;
}

Now, using require("./about.html") works fine.

英文:

I found a solution that worked for me.

Instead of having the webpack.config file in the root directory, I can change my webpack configurations using react-app-rewired.

Further information here: https://github.com/timarney/react-app-rewired

Then, in the config-overrides.js file, I made these changes:

module.exports = function override(config, env) {
  //do stuff with the webpack config...
  
  let RuleToAdd = {
    test: /\.html$/,
    loader: "html-loader",
    options: {minimize: true}
  };
  config.module.rules.push(RuleToAdd);

  return config;
}

Now, using require("./about.html") works fine.

huangapple
  • 本文由 发表于 2023年5月7日 08:37:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定