0

在一个简单的 Typescript 程序中,我require使用 Node FFI

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

进而

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

通过 webpack 将其链接起来

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.ContextReplacementPluginwebpack.config.js文件中应用。

这有点超出我的能力范围,但 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
      )
  ]

知道如何为 FFI 执行此操作吗?

4

2 回答 2

3

答案是:github issue comment on the Johnny-Five repo

引用 brodo 的回答,这就是你要阻止 webpack 被“绑定”和类似的东西缠住的方法:

... the webpack config looks like this:

module.exports = {
  plugins: [
    new webpack.ContextReplacementPlugin(/bindings$/, /^$/)
  ],
  externals: ["bindings"]
}
于 2016-11-05T21:24:18.037 回答
0

我也有类似的问题,不知何故,我设法解决了它。我先解释一下我的理解。

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

一般有两种类型的node_modules:

  1. 用于浏览器端(角度,rxjs 等)
  2. 用于 nodejs 端(express、ffi 等)

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

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

    "externals": {
        "bindings": "require('bindings')",
        "ffi": "require('ffi')"
    }
    
于 2017-09-18T06:37:48.507 回答