0

我想导入一个html文件并转换成json。

const DocHtml = require('../../Shared/assests/index.html');
const template = { __html: DocHtml };

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/,
        use: {
          loader: 'html-loader',
          options: {
            attrs: [':data-src']
          }
        }
      },
    ],
  },
};

但它引发了一个错误,说......

./src/User/Shared/assests/index.html 1:0
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. See https://webpack.js.org/concepts#loaders
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml">
| <head>
4

1 回答 1

1

经过大量研究,我想通了。

// Add this to react node-module webpack.config.js file
module.exports = {
  module: {
    loaders: [
      { 
        test: /\.html$/,
        exclude: /node_modules/,
        include: /src/User/Shared/ + /assests/,
        loader: 'html-loader'
      }
    ],
  },
};
于 2021-04-20T10:54:24.167 回答