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

feat(experimental): First-class support for excluding webextension-polyfill #847

Merged
merged 17 commits into from
Jul 24, 2024

Conversation

aklinker1
Copy link
Collaborator

@aklinker1 aklinker1 commented Jul 23, 2024

Warning

If you were previously using disableWebextensionPolyfill, here's how to migrate to the new setting:

experimental: {
- disableWebextensionPolyfill: true,
+ extensionApi: "chrome",
}

Apart of #784, this PR refactors the experimental flag for removing the webextension-polyfill from your bundled extension.

Before, it was a blanket replacement - even if a NPM package depended on the polyfill, the polyfill would be stripped out of the bundle, often times breaking the package's functionality.

Now, WXT will use wxt/browser/chrome instead of wxt/browser. This new module just exports the chrome or browser global based on the runtime browser. If a NPM package imports the polyfill, the polyfill will be included and the NPM package will function normally!

To enable this new functionality, add the following option to your wxt.config.ts:

export default defineConfig({
  extensionApi: "chrome",
});

Note that this is an experimental feature, and I need help testing it out!

Copy link

netlify bot commented Jul 23, 2024

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 5e1c94a
🔍 Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/66a170e464da980008c64044
😎 Deploy Preview https://deploy-preview-847--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@aklinker1 aklinker1 force-pushed the experimental-extension-api branch from 28fab4b to 4c684e7 Compare July 24, 2024 13:54
@@ -1,6 +1,6 @@
import { fakeBrowser } from '@webext-core/fake-browser';
import { describe, it, expect, beforeEach, vi, expectTypeOf } from 'vitest';
import { browser } from '../browser';
import { browser } from 'wxt/browser';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because we're using mkdist now, we can import from wxt/browser and it will be resolved from the NPM module instead of using the local file, and this will be replaced with wxt/browser/chrome when using extensionApi: "chrome"

Comment on lines 18 to 23
export const browser: AugmentedBrowser =
// @ts-expect-error
globalThis.browser?.runtime?.id == null
? globalThis.chrome
: // @ts-expect-error
globalThis.browser;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Use browser instead of chrome if it exists. Both Firefox and Safari provide a browser global, so browser will be used by default for those two browsers.

For firefox MV2, browser supports the promise API whereas chrome doesn't (see comment), meaning this is necessary for Firefox.

For Safari, I'm not 100% sure if there's a difference, so I'll need feedback from people with safari extensions to see if it's the right move.

* effects the extension API included at RUNTIME - during development, types
* depend on the import.
*
* NOTE: this only works if we import `wxt/browser` instead of using the relative path.
Copy link
Collaborator Author

@aklinker1 aklinker1 Jul 24, 2024

Choose a reason for hiding this comment

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

TODO: Add eslint rule to enforce this.

@aklinker1 aklinker1 changed the title feat(experimental): Replace disableWebextensionPolyfill with extensionApi feat(experimental): First-class support for excluding webextension-polyfill Jul 24, 2024
Copy link

codecov bot commented Jul 24, 2024

Codecov Report

Attention: Patch coverage is 79.20792% with 21 lines in your changes missing coverage. Please review.

Project coverage is 82.30%. Comparing base (5eb5be7) to head (5e1c94a).

Files Patch % Lines
packages/wxt/src/testing/wxt-vitest-plugin.ts 0.00% 17 Missing ⚠️
packages/wxt/src/browser/chrome.ts 20.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #847      +/-   ##
==========================================
- Coverage   82.42%   82.30%   -0.12%     
==========================================
  Files         112      113       +1     
  Lines        6280     6302      +22     
  Branches     1019     1028       +9     
==========================================
+ Hits         5176     5187      +11     
- Misses       1091     1101      +10     
- Partials       13       14       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aklinker1 aklinker1 marked this pull request as ready for review July 24, 2024 20:36
@aklinker1
Copy link
Collaborator Author

Tested new mode here: aklinker1/github-better-line-counts#34

Working like a charm!

@aklinker1 aklinker1 merged commit f1bb7cb into main Jul 24, 2024
16 checks passed
@aklinker1 aklinker1 deleted the experimental-extension-api branch July 24, 2024 22:09
Timeraa pushed a commit to Timeraa/wxt that referenced this pull request Jul 25, 2024
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

Successfully merging this pull request may close these issues.

1 participant