Webpack失败,出现Node FFI和Typescript-dynamic require错误



在一个简单的Typescript程序中,带有的require节点FFI

import  * as Electron   from  'electron';`
import  * as ffi        from  'ffi';`

然后

mylib = ffi.Library('libmoi', {
  'worker': [ 'string', [ 'string' ]  ],
  'test'  : [ 'string', []            ]
  } );

通过网络包将其连接起来会产生

WARNING in ./~/bindings/bindings.js
Critical dependencies:
76:22-40 the request of a dependency is an expression
76:43-53 the request of a dependency is an expression
 @ ./~/bindings/bindings.js 76:22-40 76:43-53

问题似乎是FFI有一个动态require,而修复方法似乎是在webpack.config.js文件中应用webpack.ContextReplacementPlugin

这有点超出我的能力范围,但Angular案例的一个例子是:

plugins: [
      new webpack.ContextReplacementPlugin(
        // The (\|/) piece accounts for path separators in *nix and Windows
        /angular(\|/)core(\|/)(esm(\|/)src|src)(\|/)linker/,
        root('./src') // location of your src
      )
  ]

知道如何为外国金融机构做到这一点吗?

答案如下:github发布对Johnny Five回购的评论

引用布罗多的回答,这就是你所做的,以阻止网络包被"绑定"和类似的东西所困扰:

... the webpack config looks like this:
module.exports = {
  plugins: [
    new webpack.ContextReplacementPlugin(/bindings$/, /^$/)
  ],
  externals: ["bindings"]
}

我也遇到了类似的问题,不知怎么的,我设法解决了它。我将首先解释我的理解。

webpack的主要工作是将单独的代码文件捆绑到一个文件中,默认情况下,它将树中引用的所有代码捆绑在一起。

通常有两种类型的节点模块:

  1. 用于浏览器端(angular、rxjs等(
  2. 用于nodejs端(express、ffi等(

捆绑浏览器端node_module更安全,但捆绑节点端node_mode并不安全,因为它们不是这样设计的。因此解决方案分为两个步骤:

  1. 在webpack.config.js文件中给定适当的目标(节点、电子等(,例如默认情况下为浏览器的"target":'electron-renderer'
  2. 在webpack.config.js文件中声明node_side模块为外部依赖项,例如

    "externals": {
        "bindings": "require('bindings')",
        "ffi": "require('ffi')"
    }
    

最新更新