我正在使用parcel处理web扩展的typescript。 JQuery及其类型定义已通过npm安装。 在我的typescript文件顶部,我有:
import $ from "jquery";
import "bootstrap";
但在运行时,Chrome报错称jquery未定义。 可以通过以下git示例来重现问题:https://github.com/lhk/parcel_jquery_bug_demo。
git clone https://github.com/lhk/parcel_jquery_bug_demo
cd parcel_jquery_bug_demo
npm install
parcel build src/manifest.json
现在你可以打开Chrome并加载dist文件夹
该Git仓库包含:
src/manifest.json
{
"manifest_version": 2,
"name": "pc",
"version": "0.0.1",
"description": "none",
"author": "",
"content_security_policy":"script-src 'self'; object-src 'self'",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"./content/index.ts"]
}
]
}
src/content/index.ts
import $ from "jquery";
import "bootstrap";
$(function () {
$('[data-toggle="popover"]').popover();
});
./package.json
{
"name": "pc",
"version": "0.1.0",
"description": "",
"author": "",
"license": "",
"devDependencies": {
"parcel-plugin-web-extension": "^1.4.0",
"typescript": "^3.1.3"
},
"dependencies": {
"@types/bootstrap": "^3.3.7",
"@types/jquery": "^3.3.10",
"bootstrap": "^3.3.7",
"jquery": "^3.3.1"
}
}
在chrome中加载扩展程序后,您可以加载任何网站。错误消息如下:
我正在使用以下工具:未捕获的引用错误:jQuery未定义
- Parcel 1.10.1
- Node v8.12.0
- npm 6.4.1
- ubuntu 18.04.1 64bit
- chrome 70
import $ from "jquery";
//import "bootstrap";
$(function () {
//$('[data-toggle="popover"]').popover();
$('[data-toggle="popover"]').click(function(){});
});
因此,我的TypeScript代码的依赖关系实际上是由Parcel处理的。但是Bootstrap还需要jQuery,而这种依赖关系似乎没有得到满足。