如何使用 rollupjs 将 @mediapipe/face_mesh 模块注入我的捆绑包中

How to inject the @mediapipe/face_mesh module into my bundle with rollupjs

提问人:Jacob Muchow 提问时间:3/4/2022 最后编辑:Goku - stands with PalestineJacob Muchow 更新时间:3/4/2022 访问量:496

问:

我是 Rollup 的新手,最近几天学到了很多东西。我正在制作一个带有 Rollup 的模块,该模块依赖于 ,它作为(我认为?)IIFE 提供。我有兴趣将face_mesh注入到我的输出文件中,而不是将其作为外部依赖项包含在内。@mediapipe/face_mesh

我正在使用 commonjs 和 node-resolve 插件来注入它——除了一位之外,一切都在工作。我对如何将脚本添加到输出索引 .js 有一些奇怪的行为

package.json

"source": "src/index.ts",
"main": "dist/index.js",
"dependencies": {
    "@mediapipe/face_mesh": "0.4.1633559619"
}

来源/index.ts

import { FaceMesh } from '@mediapipe/face_mesh'

const faceMesh = new FaceMesh()

rollup.config.js

export default {
  input: 'src/index.ts',
  output: [
    {
      file: pkg.main,
      format: 'cjs'
    }
  ],
  plugins: [
    typescript({
      typescript: require('typescript')
    }),
    commonjs({
      include: /\/node_modules\//
    }),
    nodeResolve({
      browser: true
    }),
    globals(),
    builtins()
  ]
}

face_mesh.js

(function(){/*

 Copyright The Closure Library Authors.
 SPDX-License-Identifier: Apache-2.0
*/
var v;function aa(a){var b=0;return function(){
    /* a bunch of minified code */
}).call(this);

dist/index.js(卷展输出)

var face_mesh = {};

(function(){/*

 Copyright The Closure Library Authors.
 SPDX-License-Identifier: Apache-2.0
*/
var v;function aa(a){var b=0;return function(){
    /* a bunch of minified code */
}).call(commonjsGlobal);

...

const faceMesh = new face_mesh.FaceMesh()

您可以看到node_resolve已将模块命名为“face_mesh”,并为其设置了一个导出对象。但是我相信 commonjs 会更改为 ..call(this).call(commonjsGlobal)

当我运行这个模块时,我收到一个错误。FaceMesh does not exist on type face_mesh

如果我在输出文件中更改为 ,那么它可以工作。根据输出,我认为它应该是什么,但“这个”被认为是全局范围而不是模块。commonjsGlobalface_mesh

我想我可以使用替换插件作为最后一步自动执行此更改,但这对我来说感觉很笨拙。我一直在寻找更好的方法。

有没有人知道如何处理这个问题?我做了很多搜索,但这似乎是一个不常见的问题。face_mesh模块的结构似乎并不常见,或者我对应该使用什么插件/工具来转译它有误解。

JavaScript JSON TypeScript 闭包 RollupJS

评论


答: 暂无答案