Webpack 因 Node FFI 和 Typescript 失败 - 动态要求错误
Webpack fails with Node FFI and Typescript - dynamic require error
在一个简单的 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.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
)
]
知道如何为 FFI 执行此操作吗?
答案如下:github issue comment on the Johnny-Five repo
引用 brodo 的回答,这是你为阻止 webpack 被 "bindings" 和类似的东西缠住而做的:
... the webpack config looks like this:
module.exports = {
plugins: [
new webpack.ContextReplacementPlugin(/bindings$/, /^$/)
],
externals: ["bindings"]
}
我也遇到了类似的问题,不知怎么的,我设法解决了。我先说说我的理解。
webpack 的主要工作是将单独的代码文件打包成一个文件,默认情况下它会打包其树中引用的所有代码。
一般有两种node_modules:
- 用于浏览器端(angular、rxjs 等)
- 在 nodejs 端使用(express、ffi 等)
捆绑浏览器端更安全 node_module 但捆绑节点端 node_module 并不安全,因为它们不是那样设计的 所以解决方案分为两个步骤:
- 在 webpack.config.js 文件中给出适当的目标(节点、电子等)例如
"target":'electron-renderer'
默认情况下它是浏览器
在您的 webpack.config.js 文件中将 node_side 模块声明为外部依赖项,例如
"externals": {
"bindings": "require('bindings')",
"ffi": "require('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.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
)
]
知道如何为 FFI 执行此操作吗?
答案如下:github issue comment on the Johnny-Five repo
引用 brodo 的回答,这是你为阻止 webpack 被 "bindings" 和类似的东西缠住而做的:
... the webpack config looks like this:
module.exports = {
plugins: [
new webpack.ContextReplacementPlugin(/bindings$/, /^$/)
],
externals: ["bindings"]
}
我也遇到了类似的问题,不知怎么的,我设法解决了。我先说说我的理解。
webpack 的主要工作是将单独的代码文件打包成一个文件,默认情况下它会打包其树中引用的所有代码。
一般有两种node_modules:
- 用于浏览器端(angular、rxjs 等)
- 在 nodejs 端使用(express、ffi 等)
捆绑浏览器端更安全 node_module 但捆绑节点端 node_module 并不安全,因为它们不是那样设计的 所以解决方案分为两个步骤:
- 在 webpack.config.js 文件中给出适当的目标(节点、电子等)例如
"target":'electron-renderer'
默认情况下它是浏览器 在您的 webpack.config.js 文件中将 node_side 模块声明为外部依赖项,例如
"externals": { "bindings": "require('bindings')", "ffi": "require('ffi')" }