You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Experimental Feedback] Exclude webextension-polyfill via extensionApi: "chrome" #868
Install @types/chrome, the package providing types when using the chrome API. It provides more up-to-date types with much better support for MV3 features.
$ pnpm i -D @types/chrome
Regenerate project types:
$ pnpm wxt prepare
(Optional) Restart your editor or language server so your editor is using the new types
(Optional) Ignore this step if you're using auto-imports. If you've disabled auto-imports, you'll need to update all your browser imports.
If you skip this step, everything should still work, but your editor will continue using the old types instead of @types/chrome.
- import { browser } from 'wxt/browser';+ import { browser } from 'wxt/browser/chrome';
Testing
Are there any type errors? (Remember to run wxt prepare before this)
Any runtime errors in dev mode?
$ wxt
Any runtime errors when loading production builds?
$ wxt zip
If you're supporting Firefox/Safari, are there any runtime errors when ran there?
Note
If you have any feedback, problems, type errors, or runtime issues, please share them below so I can get them fixed. Goal is to ship this option as the default in the next major version, v0.20.0
Is there a way to make code like this take advantage of the types in @types/chrome?
Currently the types for "wxt/browser" are exported from webextension-polyfill.
Because of the way the @types/chrome is written with namespaces, I can't really rename chrome to browser... But I'd prefer to export these types without using the word "chrome".
changed the title [-][Experimental Feedback] Exclude webextension-polyfill via `extensionApi: "chrome"`[/-][+][Experimental Feedback] Exclude `webextension-polyfill` via `extensionApi: "chrome"`[/+]on Aug 16, 2024
What about aliasing webextension-polyfill to some wrapper (maybe a virtual entrypoint)? For example, I want to use webext-core packages, but they have dependencies on webextension-polyfill.
What about aliasing webextension-polyfill to some wrapper (maybe a virtual entrypoint)? For example, I want to use webext-core packages, but they have dependencies on webextension-polyfill.
So there's two approaches:
Just remove webextension-polyfill from WXT so it's not included in your extension when importing wxt/browser
Setup an alias to completely remove the polyfill from ALL dependencies and sub-dependencies, like webext-core
extensionApi implements the first approach. That way if a dependency depends on the polyfill, it doesn't break the dependency.
If you want to fully remove the polyfill from all dependencies, you can setup the alias yourself:
You need to add all dependencies that rely on webextension-polyfill to the ssr.noExternal option so that Vite knows to process those modules and have the alias take effect.
But be aware this can break any dependencies that rely on polyfill-specific behaviors. This WAS the behavior with the old experimental option before this one, but I changed it to just remove the polyfill from WXT to avoid breaking other dependencies.
Triple slash directives are removed at packages/wxt/dist/browser/chrome.d.ts to build. And pnpm will hoist @type/chrome. WXT can't find the path because didn't explicitly import it...…The theory is this, but not sure how to consider pnpm. 🤷
Agree to include it in the dependencies as a workaround for now.
or make sure to include .wxt/wxt.d.ts somewhere else in your typescript project:
could you please elaborate on what is meant with "include somewhere else"? The <reference.../> XML from the docs is confusing, as ts configs are usually json.
So not sure if I understand it correctly, but I've put it into the root tsconfig.base.json as
"include": ["./.wxt/wxt.d.ts"]
Haven't had any issues so far, other than IntelliJ-related issues with linting, so I guess it's not totally wrong
could you please elaborate on what is meant with "include somewhere else"? The <reference.../> XML from the docs is confusing, as ts configs are usually json.
could you please elaborate on what is meant with "include somewhere else"? The <reference.../> XML from the docs is confusing, as ts configs are usually json.
Yup, that works too. I only recommend references because that way you don't have to worry about merging your includes in the tsconfig file if it extends another TSConfig.
Hi @aklinker1 , I encountered something related to this which might be of interest. I was using @types/chrome and it worked for most of the things. However at one place when I wanted to use a firefox specific property, but typescript complained that the property is not available. Here's a relevant example:
The previousTabId is not available in chrome, only in firefox. This property is not shown when using @types/chrome, but works with webextension-polyfill. What will be the right way in this case? I know I should check the api availability as per docs, and with that a @ts-ignore will be good ?
@aabidk20 in this case, I'd recommend not relying on the Firefox-only property if you're supporting chrome as well. That way you only have to manage one implementation, even if it is more complex.
But if you want to keep using it, yes, @ts-ignoreing it is probably the right thing to do.
Activity
aiktb commentedon Jul 29, 2024
Is there a way to make code like this take advantage of the types in
@types/chrome?Currently the types for "wxt/browser" are exported from
webextension-polyfill.aklinker1 commentedon Jul 29, 2024
@aiktb If you installed
@types/chrome, you should be able to access these types through thechromeglobal.Because of the way the
@types/chromeis written with namespaces, I can't really renamechrometobrowser... But I'd prefer to export these types without using the word "chrome".Timeraa commentedon Jul 29, 2024
Will test it out in my extension in a few and report back issues if I encounter them
Timeraa commentedon Jul 30, 2024
Tested and works just fine!
1natsu172 commentedon Jul 30, 2024
Tested and works me too. If there's anything that have an impact at runtime, let me know, and I'll test it.
typed-sigterm commentedon Aug 1, 2024
tested, works fine too :)
refactor: 不使用 `webextension-polyfill`
[-][Experimental Feedback] Exclude webextension-polyfill via `extensionApi: "chrome"`[/-][+][Experimental Feedback] Exclude `webextension-polyfill` via `extensionApi: "chrome"`[/+]AsakuraMizu commentedon Sep 19, 2024
What about aliasing
webextension-polyfillto some wrapper (maybe a virtual entrypoint)? For example, I want to use webext-core packages, but they have dependencies onwebextension-polyfill.aklinker1 commentedon Sep 19, 2024
So there's two approaches:
webextension-polyfillfrom WXT so it's not included in your extension when importingwxt/browserwebext-coreextensionApiimplements the first approach. That way if a dependency depends on the polyfill, it doesn't break the dependency.If you want to fully remove the polyfill from all dependencies, you can setup the alias yourself:
But be aware this can break any dependencies that rely on polyfill-specific behaviors. This WAS the behavior with the old experimental option before this one, but I changed it to just remove the polyfill from WXT to avoid breaking other dependencies.
9 remaining items
1natsu172 commentedon Oct 28, 2024
Triple slash directives are removed at
packages/wxt/dist/browser/chrome.d.tsto build. And pnpm will hoist@type/chrome. WXT can't find the path because didn't explicitly import it...…The theory is this, but not sure how to consider pnpm. 🤷Agree to include it in the dependencies as a workaround for now.
1natsu172 commentedon Oct 28, 2024
@aklinker1 I submitted workaround PR. #1119
mklueh commentedon Oct 29, 2024
@aklinker1
could you please elaborate on what is meant with "include somewhere else"? The
<reference.../>XML from the docs is confusing, as ts configs are usually json.So not sure if I understand it correctly, but I've put it into the root tsconfig.base.json as
Haven't had any issues so far, other than IntelliJ-related issues with linting, so I guess it's not totally wrong
2wheeh commentedon Nov 4, 2024
@mklueh
https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html
It means to include that directive somewhere at the global level.
mklueh commentedon Nov 4, 2024
Thanks.
To be honest using this feels totally wrong.
Is there any reason why it can't be added via
tsconfig.includesin the extension's tsconfig.json?Looks that it does what it should and its much cleaner than putting these references in files other than tsconfig
But maybe I'm missing the point here...
aklinker1 commentedon Nov 13, 2024
Yup, that works too. I only recommend references because that way you don't have to worry about merging your
includesin the tsconfig file if it extends another TSConfig.aabidk20 commentedon Jan 6, 2025
Hi @aklinker1 , I encountered something related to this which might be of interest. I was using
@types/chromeand it worked for most of the things. However at one place when I wanted to use a firefox specific property, but typescript complained that the property is not available. Here's a relevant example:The
previousTabIdis not available in chrome, only in firefox. This property is not shown when using@types/chrome, but works with webextension-polyfill. What will be the right way in this case? I know I should check the api availability as per docs, and with that a@ts-ignorewill be good ?aklinker1 commentedon Jan 6, 2025
@aabidk20 in this case, I'd recommend not relying on the Firefox-only property if you're supporting chrome as well. That way you only have to manage one implementation, even if it is more complex.
But if you want to keep using it, yes,
@ts-ignoreing it is probably the right thing to do.aabidk20 commentedon Jan 6, 2025
Ok, that's good to know. Thanks!
aklinker1 commentedon Mar 28, 2025
Alright, thanks for your feedback everyone, checkout v0.20.0 for the new
browserobject based on@types/chrome.