Webpack因Node FFI和Typescript而失败 - 动态需要错误

时间:2016-11-02 14:58:36

标签: node.js typescript webpack electron node-ffi

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

节点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.ContextReplacementPlugin文件中应用webpack.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做这个吗?

2 个答案:

答案 0 :(得分: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"]
}

答案 1 :(得分:0)

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

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

通常有两种类型的node_modules:

  1. 在浏览器端使用(angular,rxjs等)
  2. 用于nodejs侧(express,ffi等)
  3. 捆绑浏览器端node_module更安全但捆绑节点端node_module更安全,因为它们不是那样设计所以解决方案分为两个步骤:

    1. 在webpack.config.js文件中提供适当的目标(节点,电子等),例如[dcc32 Error] commutil.pas(1005): E2251 Ambiguous overloaded call to 'TextToFloat' System.SysUtils.pas(18332): Related method: function TextToFloat(PWideChar; var; TFloatValue; const TFormatSettings): Boolean; System.SysUtils.pas(18515): Related method: function TextToFloat(PAnsiChar; var; TFloatValue; const TFormatSettings): Boolean; 默认为浏览器
    2. 在webpack.config.js文件中将node_side模块声明为外部依赖项,例如

      "target":'electron-renderer'
相关问题