英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论