Chrome扩展Service Worker引入TensorFlow模块注册失败求助
Chrome扩展Service Worker打包后模块引用错误问题
我正在开发一款Chrome扩展,在Service Worker中使用TensorFlow模型,采用Parcel进行打包。使用HMR服务器时扩展运行正常,但构建后添加到Chrome时出现报错:Uncaught ReferenceError: $379e4f2c7c8c5f6b$exports is not defined
进而导致Service worker registration failed. Status code: 15。
已尝试的方案均无效:
- 移除
"type": "module" - 改用ES6语法
- 在HTML脚本中定义全局export变量
排查发现问题与模块导入直接相关:
- 不导入任何模块时,Service Worker可正常运行
- 仅导入universal-sentence-encoder时,扩展可安装,但交互时仍会抛出上述引用错误
当前导入代码如下:
import '@tensorflow/tfjs-backend-cpu/dist/index.js'; import '@tensorflow/tfjs-backend-webgl/dist/index.js'; import * as use from '@tensorflow-models/universal-sentence-encoder/dist/index.js'; import * as tf from '@tensorflow/tfjs-core/dist/index.js';
我认为导入方式是正确的(毕竟HMR环境中运行正常),想请教哪里操作有误?
附上相关配置文件:
manifest.json:
..., "background": { "service_worker": "service-worker.ts", "type": "module" }, ...
tsconfig.json:
{ "compilerOptions": { "outDir": "./dist", "target": "es2022", "moduleResolution": "node" } }
package.json:
{ "dependencies": { "@tensorflow-models/universal-sentence-encoder": "^1.3.3", "@tensorflow/tfjs": "^3.6.0", "@tensorflow/tfjs-converter": "^3.6.0", "@tensorflow/tfjs-core": "^3.6.0", "@tensorflow/tfjs-backend-cpu": "^3.6.0", "@tensorflow/tfjs-backend-webgl": "^3.6.0" }, "devDependencies": { "@parcel/config-webextension": "^2.8.3", "@types/chrome": "^0.0.238", "@typescript-eslint/eslint-plugin": "^5.60.0", "@typescript-eslint/parser": "^5.60.0", "buffer": "^5.5.0", "eslint": "^8.43.0", "parcel": "^2.8.3", "process": "^0.11.10", "typescript": "5.1.6" }, "scripts": { "copy-img": "if not exist \"dist/images\" mkdir \"dist/images\" && copy images \"dist/images\"", "start": "yarn copy-img && parcel watch manifest.json --host localhost", "build": "yarn copy-img && parcel build manifest.json" } }
内容的提问来源于stack exchange,提问作者tplat
相关产品推荐
相关产品推荐

