Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cross-fetch does not work in service workers of webextensions bundled with Vite #180

Open
pate313373 opened this issue Apr 7, 2024 · 0 comments

Comments

@pate313373
Copy link

Hi!

I'm using tsdav, which itself depends on cross-fetch in a service worker of a webextension, which is bundled with Vite.
However, it seems that when the service worker is being bundled, cross-fetch does not work well.

An outputs the following error: TypeError: Failed to execute 'fetch' on 'WorkerGlobalScope': Illegal invocation

Many thanks!

package.json

{
  "name": "vite-web-extension",
  "version": "1.0.0",
  "displayName": "Vite Web Extension",
  "author": "@samrum/vite-plugin-web-extension",
  "description": "A @samrum/vite-plugin-web-extension web extension",
  "type": "module",
  "scripts": {
    "build": "vite build",
    "watch": "vite build --watch --mode development --minify false",
    "dev": "vite",
    "serve:firefox": "web-ext run --start-url \"about:debugging#/runtime/this-firefox\"  --source-dir ./dist/",
    "serve:chrome": "web-ext run -t chromium --start-url \"https://example.com\" --source-dir ./dist/",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
  },
  "license": "MIT",
  "devDependencies": {
    "@rushstack/eslint-patch": "^1.3.3",
    "@samrum/vite-plugin-web-extension": "^5.1.0",
    "@types/chrome": "^0.0.254",
    "@types/webextension-polyfill": "^0.10.6",
    "@vitejs/plugin-vue2": "^1.1.2",
    "@vue/eslint-config-prettier": "^8.0.0",
    "eslint": "^8.49.0",
    "eslint-plugin-vue": "^9.20.1",
    "prettier": "^3.0.3",
    "vite": "^5.0.10",
    "web-ext": "^7.8.0"
  },
  "dependencies": {
    "@mdi/font": "^7.4.47",
    "gmap-vue": "^3.5.4",
    "ics": "^3.5.0",
    "leaflet": "^1.9.4",
    "lodash": "^4.17.21",
    "luxon": "^3.4.4",
    "pinia": "^2.0.16",
    "tsdav": "^2.0.5",
    "vue": "^2.7.7",
    "vue2-leaflet": "^2.7.1",
    "vuetify": "^2.7.1",
    "webextension-polyfill": "^0.10.0"
  },
  "overrides": {
    "tsdav": {
      "cross-fetch": "^4.0.0"
    }
  }
}

vite.config

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue2';
import webExtension from '@samrum/vite-plugin-web-extension';
import path from 'path';
import { getManifest } from './src/manifest';

// https://vitejs.dev/config/
export default defineConfig(() => {
	return {
		plugins: [
			vue({}),
			webExtension({
				manifest: getManifest(),
			}),
		],
		resolve: {
			alias: {
				'@': path.resolve(__dirname, './src'),
			},
		},
	};
});

background-script

import browser from 'webextension-polyfill';
import { DAVClient } from 'tsdav';
import { createEvent } from 'ics';
import _ from 'lodash';
// import { DateTime } from "luxon";

console.log('Background Script');

browser.runtime.onMessage.addListener(async (msg, sender) => {
	const operation = _.get(msg, 'operation');
	if (operation !== 'loadEvents') {
		console.log('message is not for me');
		return;
	}
	// chrome.tabs.create({ url: chrome.runtime.getURL("/pages/help.html") });
	try {
		const calDavClient = new DAVClient({
			serverUrl: 'https://caldav.icloud.com',
			credentials: { username: 'some@user.com', password: 'pass111' },
			authMethod: 'Basic',
			defaultAccountType: 'caldav',
		});
		return await calDavClient.login();
	} catch (error) {
		console.error(error);
	}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant