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

前端 未结 17 1604
时光说笑
时光说笑 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:35

    If you need multiple modifications, like when using ant design, you can combine multiple functions like this:

    const {
      override,
      removeModuleScopePlugin,
      fixBabelImports,
    } = require('customize-cra');
    
    module.exports = override(
      fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: 'css',
      }),
      removeModuleScopePlugin(),
    );
    
    0 讨论(0)
  • 2020-11-22 13:36

    If you only need to import a single file, such as README.md or package.json, then this can be explicitly added to ModuleScopePlugin()

    config/paths.js

    const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
    module.exports = {
      appPackageJson: resolveApp('package.json'),
      appReadmeMD:    resolveApp('README.md'),
    };
    

    config/webpack.config.dev.js + config/webpack.config.prod.js

    module.exports = {
      resolve: {
        plugins: [
          // Prevents users from importing files from outside of src/ (or node_modules/).
          // This often causes confusion because we only process files within src/ with babel.
          // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
          // please link the files into your node_modules/ and let module-resolution kick in.
          // Make sure your source files are compiled, as they will not be processed in any way.
          new ModuleScopePlugin(paths.appSrc, [
              paths.appPackageJson,
              paths.appReadmeMD         // README.md lives outside of ./src/ so needs to be explicitly included in ModuleScopePlugin()
          ]),
        ]
      }
    }
    
    0 讨论(0)
  • 2020-11-22 13:39

    Adding to Bartek Maciejiczek's answer, this is how it looks with Craco:

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

    0 讨论(0)
  • 2020-11-22 13:39

    You can try using simlinks, but in reverse.

    React won't follow simlinks, but you can move something to the source directory, and create a simlink to it.

    In the root of my project, I had a node server directory that had several schema files in it. I wanted to use them on the frontend, so I:

    • moved the files /src
    • in the termal, I cd'ed into where the schema files belonged in server
    • ln -s SRC_PATH_OF_SCHEMA_FILE

    This gave react what it was looking for, and node was perfectly happy including files through simlinks.

    0 讨论(0)
  • 2020-11-22 13:40

    Image inside public folder

      use image inside html extension
      <img src="%PUBLIC_URL%/resumepic.png"/>
    
      use image inside  js extension
      <img src={process.env.PUBLIC_URL+"/resumepic.png"}/>
    
    • use image inside js Extension
    0 讨论(0)
  • 2020-11-22 13:42

    install these two packages

    npm i --save-dev react-app-rewired customize-cra
    

    package.json

    "scripts": {
        - "start": "react-scripts start"
        + "start": "react-app-rewired start"
    },
    

    config-overrides.js

    const { removeModuleScopePlugin } = require('customize-cra');
    
    module.exports = function override(config, env) {
        if (!config.plugins) {
            config.plugins = [];
        }
        removeModuleScopePlugin()(config);
    
        return config;
    };
    
    
    0 讨论(0)
提交回复
热议问题