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

Bump webextension-polyfill from 0.8.0 to 0.9.0 #3043

Merged
merged 7 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 40 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@
"webext-content-scripts": "^1.0.1",
"webext-detect-page": "^4.0.1",
"webext-dynamic-content-scripts": "^8.0.1",
"webext-messenger": "^0.17.1",
"webext-messenger": "^0.18.0",
"webext-patterns": "^1.1.1",
"webext-polyfill-kinda": "^0.10.0",
"webext-tools": "^0.3.0",
"webextension-polyfill": "^0.8.0",
"webextension-polyfill": "^0.9.0",
"whatwg-mimetype": "^3.0.0",
"yup": "^0.32.11"
},
Expand Down
25 changes: 1 addition & 24 deletions src/background/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import browser from "webextension-polyfill";
import { loadOptions, saveOptions } from "@/store/extensionsStorage";
import {
deploymentFactory,
Expand All @@ -28,8 +27,7 @@ import MockAdapter from "axios-mock-adapter";
import axios from "axios";
import { updateDeployments } from "@/background/deployment";

// @ts-expect-error No way to extend `globalThis` effectively
globalThis.browser = browser;
browser.permissions.contains = jest.fn().mockResolvedValue(true);

const axiosMock = new MockAdapter(axios);

Expand Down Expand Up @@ -93,27 +91,6 @@ jest.mock("webext-detect-page", () => ({
isContentScript: () => false,
}));

jest.mock("webextension-polyfill", () => {
const mock = jest.requireActual("webextension-polyfill");
Comment on lines -96 to -97
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twschiller "webextension-polyfill" is now being automatically applied by webpack so browser is "natively" available everywhere.

The same thing has already been happening in the tests, where jest-webextension-mock automatically polyfills the globalThis.browser

Missing methods can be added in testAfterEnv.js as seen below. Mocks are the usual browser.permissions.contains = jest.fn().mockResolvedValue(true)


return {
__esModule: true,
default: {
// Keep the existing local storage mock
...mock,
permissions: {
contains: jest.fn().mockResolvedValue(true),
},
runtime: {
openOptionsPage: jest.fn(),
getManifest: jest.fn().mockReturnValue({
version: "1.5.2",
}),
},
},
};
});

jest.mock("@/background/installer", () => ({
isUpdateAvailable: jest.fn().mockReturnValue(false),
}));
Expand Down
4 changes: 0 additions & 4 deletions src/extensionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// It must be the very first thing or telemetry will fail
// eslint-disable-next-line import/no-unassigned-import -- Import for side effects
import "@/extensionPolyfill";

// Init rollbar early so we get error reporting on the other initialization
import "@/telemetry/reportUncaughtErrors";

Expand Down
25 changes: 0 additions & 25 deletions src/extensionPolyfill.js

This file was deleted.

39 changes: 39 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// This cannot be a regular import because it turns `globals.d.ts` in a "module definition", which it isn't
type Browser = import("webextension-polyfill").Browser;

// https://stackoverflow.com/questions/43638454/webpack-typescript-image-import
declare module "*.svg" {
const CONTENT: string;
Expand Down Expand Up @@ -143,3 +146,39 @@ interface ErrorConstructor {
new (message?: string, options?: ErrorOptions): Error;
(message?: string, options?: ErrorOptions): Error;
}

// TODO: This overrides Firefox’ types. It's possible that the return types are different between Firefox and Chrome
interface ExtendedRuntime
extends Omit<Browser["runtime"], "requestUpdateCheck"> {
/*
* Requests an update check for this app/extension.
*/
requestUpdateCheck(): Promise<chrome.runtime.RequestUpdateCheckStatus>;
}

type Identity = Browser["identity"];

/**
* Gets an OAuth2 access token using the client ID and scopes specified in the oauth2 section of manifest.json.
*/
interface ExtendedIdentity extends Identity {
/**
* Gets an OAuth2 access token using the client ID and scopes specified in the oauth2 section of manifest.json.
*/
getAuthToken(details?: chrome.identity.TokenDetails): Promise<string>;

/**
* Removes an OAuth2 access token from the Identity API's token cache.
*/
removeCachedAuthToken(
details: chrome.identity.TokenInformation
): Promise<void>;
}

// @ts-expect-error See Firefox/requestUpdateCheck-related comment above
interface ChromeifiedBrowser extends Browser {
runtime: ExtendedRuntime;
identity: ExtendedIdentity;
}

declare const browser: ChromeifiedBrowser;
54 changes: 0 additions & 54 deletions src/globalsExtension.d.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/testAfterEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ global.jQuery = $;

// Disable onMessage handler, or else it will respond to `sendMessage` calls locally
global.browser.runtime.onMessage.addListener = jest.fn();

// @ts-expect-error API missing from mock https://github.com/clarkbw/jest-webextension-mock/issues/148
browser.permissions = {
contains: jest.fn(),
};

browser.runtime.getManifest = jest.fn().mockReturnValue({
version: "1.5.2",
});
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ module.exports = (env, options) =>
vendors: [
"react",
"react-dom",
"webextension-polyfill",
"jquery",
"lodash-es",
"js-beautify",
Expand Down Expand Up @@ -318,6 +317,7 @@ module.exports = (env, options) =>
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
browser: "webextension-polyfill",
}),

// This will inject the current ENVs into the bundle, if found
Expand Down