The create-react-app imports restriction outside of src directory

前端 未结 17 1811
时光说笑
时光说笑 2020-11-22 13:20

I am using create-react-app. I am trying to call an image from my public folder from a file inside my src/components. I am receiving this error message.

17条回答
  •  野性不改
    2020-11-22 13:57

    I think Lukas Bach solution to use react-app-rewired in order to modify webpack config is a good way to go, however, I wouldn't exclude the whole ModuleScopePlugin but instead whitelist the specific file that can be imported outside of src:

    config-overrides.js

    const ModuleScopePlugin = require("react-dev-utils/ModuleScopePlugin");
    const path = require("path");
    
    module.exports = function override(config) {
      config.resolve.plugins.forEach(plugin => {
        if (plugin instanceof ModuleScopePlugin) {
          plugin.allowedFiles.add(path.resolve("./config.json"));
        }
      });
    
      return config;
    };
    

提交回复
热议问题