将加密浏览器添加到 Gatsby 项目

San*_*rén 3 javascript stripe-payments reactjs webpack gatsby

我想将 use-shopping-cart ( https://useshoppingcart.com/ ) 添加到我的 Gatsby 项目中。当我尝试使用它时,出现此错误:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules 
by default.
This is no longer the case. Verify if you need this module and configure a
polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "crypto":
require.resolve("crypto-browserify") }'
    - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "crypto": false }
Run Code Online (Sandbox Code Playgroud)

如何添加crypto-browserify到盖茨比?作为插件里面gatsby-config.js

谢谢!

Fer*_*reu 6

这类问题 ( BREAKING CHANGE: webpack < 5 used to include polyfills for node.js...) 与webpack 在其新v5版本中删除polyfills的事实有关,这是use-shopping-cart.

应该通过安装固定crypto-browserify(通过npm i crypto-browserify),并添加以下退回到的WebPack压倒一切的配置,在您的gatsby-node.jsonCreateWebpackConfigAPI应该工作:

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
   resolve: {
      fallback: {
        crypto: require.resolve('crypto-browserify'),
      },
    },
  })
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想包含 polyfill,您可以使用这样的空模块:

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
   resolve: {
      fallback: {
        "crypto": false
      },
    },
  })
}
Run Code Online (Sandbox Code Playgroud)