I am currently using parcel to process typescript for a web extension. I have installed JQuery and its type definitions via npm. In my typescript file, I have the following at the top:
import $ from "jquery";
import "bootstrap";
However, when running runtime, Chrome gives an error that jquery is not defined. You can find a minimal example to reproduce this problem on 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
You can then open chrome and load the dist folder.
The github repository includes:
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"
}
}
Once you've loaded the extension in chrome, you can visit any website. The error message will be:
Uncaught ReferenceError: jQuery is not defined
I am using:
- Parcel 1.10.1
- Node v8.12.0
- npm 6.4.1
- ubuntu 18.04.1 64bit
- chrome 70
I believe the issue lies in the import of bootstrap. The code below functions properly:
import $ from "jquery";
//import "bootstrap";
$(function () {
//$('[data-toggle="popover"]').popover();
$('[data-toggle="popover"]').click(function(){});
});
It seems that while parcel handles the dependencies for my typescript code, bootstrap requires jQuery separately, which is causing the error.