reactjs 使用craco进行的topLevelAwait实验无效

b1payxdu  于 2023-04-29  发布在  React
关注(0)|答案(1)|浏览(135)

我尝试使用CRACO实现topLevelAwait,同时添加一些polyfill,但它无法检测topLevelAwait设置的代码行。如果我取出polyfill的代码行,我只得到polyfill错误,而不是topLevelAwait is not enabled错误,如果我添加polyfill的行,polyfill工作,但我有topLevelAwait is not enabled错误。
这就是我的 *craco。config.js * 文件看起来像:

module.exports = {
    webpack: {
      configure: {
        experiments: {
          topLevelAwait: true,
        },
      },
      
      configure: webpackConfig => {
        const scopePluginIndex = webpackConfig.resolve.plugins.findIndex(
          ({ constructor }) => constructor && constructor.name === 'ModuleScopePlugin'
        );

        webpackConfig.resolve.plugins.splice(scopePluginIndex, 1);
        webpackConfig['resolve'] = {
          fallback: {
            path: require.resolve("path-browserify"),
            crypto: require.resolve("crypto-browserify"),
            stream: require.resolve("stream-browserify"),
            'crypto-browserify': require.resolve('crypto-browserify'),
            os: require.resolve("os-browserify/browser"),
            url: require.resolve("url/"),
            assert: require.resolve("assert/"),
          },
        }
        return webpackConfig;
      },
    },
  };

所有的帮助是赞赏,谢谢。

798qvoo8

798qvoo81#

我遇到了类似的问题,使用overrideWebpackConfig钩子覆盖了配置:

//craco.config.js

module.exports = {
  /* other configs */

  overrideWebpackConfig: ({ webpackConfig }) => {
    
    webpackConfig.experiments = {
      ...webpackConfig.experiments,
      topLevelAwait: true,
    };

    /** other configs*/

    return webpackConfig;
  }
}

相关问题