I'm building a Chrome extension that has both a popup and a background-page. I'm following this tutorial that focuses on using Angular to create the popup and uses @angular-builders/custom-webpack
to add another Webpack entrypoint for the background-page so I can also use TypeScript in there.
custom-webpack.config.js
module.exports = {
entry: { background: 'src/background.ts' },
}
All well and good, popup works with Angular, background-page works with TypeScript.
background/test.ts
export const helloWorld = () => {
console.log('Hello, World!');
}
background.html
<script type="module" src="runtime.js"></script>
<script type="module" src="background.js"></script>
background.ts
//import { helloWorld } from "./background/test";
console.log(">>chrome.runtime.onInstalled")
//helloWorld();
Now the issue is that I cannot use any import
's in the background-page script. If I uncomment the lines above, Webpack does produce the right output but but the extension doesn't work anymore
I don't see the console entry:
>>chrome.runtime.onInstalled
Also no errors come up, it just ceases to function...
Any guidance would be greatly appreciated
background.js (first lines of output)
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["background"],{
/***/ "./src/background.ts":
/*!***************************!*\
!*** ./src/background.ts ***!
\***************************/
/*! exports provided: ɵ0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "ɵ0", function() { return ɵ0; });
/* harmony import */ var tslib__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! tslib */ "./node_modules/tslib/tslib.es6.js");
/* harmony import */ var _background_test__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./background/test */ "./src/background/test.ts");
console.log(">>chrome.runtime.onInstalled");
Object(_background_test__WEBPACK_IMPORTED_MODULE_1__["helloWorld"])();
Edit: added manifest.json
{
"manifest_version": 2,
"name": "My extension",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_icon": "assets/icon.png",
"default_popup": "index.html"
},
"background": {
"page": "background.html",
"persistent": false
},
"permissions": [
"identity",
"storage",
"<all_urls>"
],
"icons": {
"256": "assets/logo.png"
}
}
Edit: added angular.json
snippet from project property
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./custom-webpack.config.js"
},
...
}
}