NodeJS 将Polyfill添加到craco,发出添加回退“resolve.fallback”

dsekswqp  于 2022-12-12  发布在  Node.js
关注(0)|答案(1)|浏览(327)

出现错误:
如果要包括多边形填充,则需要:- 添加回退'resolve.回退:{“path”:require.resolve(“path-browserify”)}' - install 'path-browserify'如果您不想包含多边形填充,可以使用如下的空模块:resolve.fallback:{“路径”:假}
Package.Json:

"dependencies": {
  "path-browserify": "^1.0.1",
  //...
},
"scripts": {
  "contracts:compile:abi": "typechain --target ethers-v5 --out-dir src/abis/types \"./src/abis/**/*.json\"",
  "start": "craco start",
  "build": "craco build",
  "test": "craco test --coverage"
}

我没有一个webpack.config,我也没有使用react-rewired-scripts,我认为Craco做了类似的事情,但TBH我不完全理解。
这是我的craco.config.js

module.exports = {
  babel: {
    plugins: ['@vanilla-extract/babel-plugin'],
  },
  webpack: {
    plugins: [
      new VanillaExtractPlugin(),
      new DefinePlugin({
        'process.env.REACT_APP_GIT_COMMIT_HASH': JSON.stringify(commitHash.toString()),
      }),
    ],
    configure: (webpackConfig) => {
      const instanceOfMiniCssExtractPlugin = webpackConfig.plugins.find(
        (plugin) => plugin instanceof MiniCssExtractPlugin
      )
      if (instanceOfMiniCssExtractPlugin !== undefined) instanceOfMiniCssExtractPlugin.options.ignoreOrder = true
      return webpackConfig
    },
  },
}

如何在此处添加多边形填充?

xu3bshqb

xu3bshqb1#

为了解决这个问题,我在configure方法中添加了fallback选项:

configure: (webpackConfig) => {

  webpackConfig.resolve.fallback = webpackConfig.resolve.fallback || {}
  webpackConfig.resolve.fallback.path = webpackConfig.resolve.fallback.path ?? require.resolve("path-browserify")

  //...
},

相关问题