【问题标题】:How to use a js file from outside of create-react-app directory, with craco?如何在 craco 中使用 create-react-app 目录之外的 js 文件?
【发布时间】:2022-01-05 06:49:22
【问题描述】:

我正在为我的 create-react-app 使用 craco,并且我正在尝试从另一个目录导入 javascript 文件,该目录位于应用程序 src 的外部。

我听说这是可能的,但在他们的文档中没有找到任何关于此的内容,至少不是我所看到的。

我的应用是使用 React 版本 17.0.2 构建的,以防万一。

知道如何实现吗?

【问题讨论】:

    标签: javascript reactjs import create-react-app craco


    【解决方案1】:

    我的问题的解决方案是将其添加到我的 craco.config.ts 导出对象中:

    https://github.com/facebook/create-react-app/issues/9127#issuecomment-646989707

    您也需要在导出对象的根目录中添加它(例如:module.exports = { ...):

        webpack: {
            alias: {},
            plugins: {
                add: [], /* An array of plugins */
                remove: [],  /* An array of plugin constructor's names (i.e. "StyleLintPlugin", "ESLintWebpackPlugin" ) */
            },
            configure: webpackConfig => {
                const scopePluginIndex = webpackConfig.resolve.plugins.findIndex(
                  ({ constructor }) => constructor && constructor.name === 'ModuleScopePlugin'
                );
          
                webpackConfig.resolve.plugins.splice(scopePluginIndex, 1);
                return webpackConfig;
            }
        },
    

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2020-03-31
      • 1970-01-01
      • 2020-05-21
      • 2017-11-22
      相关资源
      最近更新 更多