fix: enable webextension-polyfill usage via webpack ProvidePlugin (#351)

* Enable usage via `ProvidePlugin`
* Use `globalThis`
* Update lint to forbid `browser` global usage
This commit is contained in:
Federico Brigante 2022-01-19 04:12:53 +08:00 committed by GitHub
parent 31ed31a6e0
commit 780518ed1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 7 deletions

View File

@ -13,6 +13,7 @@
}, },
"globals": { "globals": {
"globalThis": true,
}, },
"rules": { "rules": {

View File

@ -2,10 +2,13 @@
"env": { "env": {
"browser": true, "browser": true,
"node": false, "node": false,
"webextensions": true, // Don't use `webextensions` because it enables the browser global.
// We want to use globalThis.browser instead:
// https://github.com/mozilla/webextension-polyfill/pull/351
}, },
"globals": { "globals": {
// Allow the `module` global, but not the `require(…)` function // Allow the `module` global, but not the `require(…)` function
"module": false, "module": false,
"chrome": true,
}, },
} }

View File

@ -6,7 +6,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict"; "use strict";
if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.prototype) { if (typeof globalThis != "object" || typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) {
throw new Error("This script should only be loaded in a browser extension.");
}
if (typeof globalThis.browser === "undefined" || Object.getPrototypeOf(globalThis.browser) !== Object.prototype) {
const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received."; const CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE = "The message port closed before a response was received.";
const SEND_RESPONSE_DEPRECATION_WARNING = "Returning a Promise is the preferred way to send a reply from an onMessage/onMessageExternal listener, as the sendResponse will be removed from the specs (See https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage)"; const SEND_RESPONSE_DEPRECATION_WARNING = "Returning a Promise is the preferred way to send a reply from an onMessage/onMessageExternal listener, as the sendResponse will be removed from the specs (See https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage)";
@ -540,13 +544,9 @@ if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.
return wrapObject(extensionAPIs, staticWrappers, apiMetadata); return wrapObject(extensionAPIs, staticWrappers, apiMetadata);
}; };
if (typeof chrome != "object" || !chrome || !chrome.runtime || !chrome.runtime.id) {
throw new Error("This script should only be loaded in a browser extension.");
}
// The build process adds a UMD wrapper around this file, which makes the // The build process adds a UMD wrapper around this file, which makes the
// `module` variable available. // `module` variable available.
module.exports = wrapAPIs(chrome); module.exports = wrapAPIs(chrome);
} else { } else {
module.exports = browser; module.exports = globalThis.browser;
} }