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
@types/chrome needs to be installed manually when using pnpm #1116
Is the extensionApi in config set to 'chrome'? Also try restarting ts-server on your editor.
yes, it is set to 'chrome' as configured after scaffolding the project via pnpm dlx wxt@latest init currently.
Although you are still experiencing differences in behavior due to the package manager?
yes, there seems to be an issue where browser.runtime fails to load properly.
without @types/chrome on the app devDependency
with @types/chrome on the app devDependency
plus, even in npm (or with @types/chrome in pnpm), the type inference fails for the snippet below from the docs while it works correctly with chrome namespace:
if this isn't a bug I may be missing something important here - would you have any suggestions?
@2wheeh Alright, there are two problems. I understood it too.
First, for some reason in the pnpm environment, the internal dependency @types/chrome can't be resolved and the typeof chrome is failing. The workaround to this problem is to include @types/chrome in the devDeps on the app side, as you already understood.
The second thing is that the interface as a type cannot be inferred from the browser as you say. This one is not a package-manager dependent problem. I think it's an implementation glitch in WXT.
The workaround over here is below, and either approach should be used.
import{Runtime}from"wxt/browser";// This types from webextension-polyfillfunctionhandler1(params: Runtime.MessageSender){}// This types from @types/chrome(need install `@types/chrome`)functionhandler2(params: chrome.runtime.MessageSender){}
Alright, so @types/chrome can't be resolved because when it's a subdependency, it's located at node_modules/.pnpm/@types/chrome. However, the triple-slash reference WXT adds (/// <reference types="@types/chrome" />) only looks for node_modules/@types/chrome, it doesn't look inside .pnpm directory, so it doesn't add the types correctly.
So the solution is to use import.resolve to find the true file path of @types/chrome, and use that in the reference instead of just "@types/chrome" here:
So the solution is to use import.resolve to find the true file path of @types/chrome, and use that in the reference instead of just "@types/chrome" here:
So getMainDeclarationEntry needs to be updated to resolve correct module path.
// TODO: Use this once we're fully running in ESM, see https://github.com/wxt-dev/wxt/issues/277
Activity
1natsu172 commentedon Oct 27, 2024
Is the
extensionApiin config set to'chrome'? Also try restarting ts-server on your editor. ref: https://wxt.dev/guide/essentials/extension-apis.html#disabling-the-polyfillAlthough you are still experiencing differences in behavior due to the package manager?
2wheeh commentedon Oct 28, 2024
@1natsu172
yes, it is set to 'chrome' as configured after scaffolding the project via
pnpm dlx wxt@latest initcurrently.yes, there seems to be an issue where
browser.runtimefails to load properly.without
@types/chromeon the app devDependencywith
@types/chromeon the app devDependencyplus, even in npm (or with
@types/chromein pnpm), the type inference fails for the snippet below from the docs while it works correctly withchromenamespace:if this isn't a bug I may be missing something important here - would you have any suggestions?
2wheeh commentedon Oct 28, 2024
as per #784 (comment), it seems to need to install
@types/chromewhen using pnpm.when
browseris a value it infers well but still when it is a type, it shows TS error.1natsu172 commentedon Oct 28, 2024
@2wheeh Alright, there are two problems. I understood it too.
First, for some reason in the
pnpmenvironment, the internal dependency@types/chromecan't be resolved and thetypeof chromeis failing. The workaround to this problem is to include@types/chromein the devDeps on the app side, as you already understood.Here's the code inside.
wxt/packages/wxt/src/browser/chrome.ts
Line 12 in 68183b6
The second thing is that the interface as a type cannot be inferred from the
browseras you say. This one is not a package-manager dependent problem. I think it's an implementation glitch in WXT.The workaround over here is below, and either approach should be used.
2wheeh commentedon Oct 28, 2024
@1natsu172 thank you for the very clear summary 👍🏻
aklinker1 commentedon Nov 25, 2024
Alright, so
@types/chromecan't be resolved because when it's a subdependency, it's located atnode_modules/.pnpm/@types/chrome. However, the triple-slash reference WXT adds (/// <reference types="@types/chrome" />) only looks fornode_modules/@types/chrome, it doesn't look inside.pnpmdirectory, so it doesn't add the types correctly.So the solution is to use
import.resolveto find the true file path of@types/chrome, and use that in the reference instead of just"@types/chrome"here:wxt/packages/wxt/src/core/generate-wxt-dir.ts
Line 42 in 54baf21
aklinker1 commentedon Nov 25, 2024
I'll try and get to this soon, but if someone else wants to give it a go, see my previous comment.
1natsu172 commentedon Nov 26, 2024
Another issue is more complex, so I have split it into a separate issue. #1213
2wheeh commentedon Nov 27, 2024
So
getMainDeclarationEntryneeds to be updated to resolve correct module path.but the thing is it seems not possible to use
import.meta.*here...ERROR Cannot use 'import.meta' outside a module:/something around
jitiorc12might be related?aklinker1 commentedon Nov 28, 2024
@2wheeh If you base off
0.20.0-breaking-changes, I've already merged the fix this issue. If you open a PR into that branch, it will go out in the next major version.But we already have a method for finding the path to a NPM package in a CJS/ESM environment without
import.meta:wxt/packages/wxt/src/core/resolve-config.ts
Lines 423 to 438 in 241c907
Forgot to link this. You could refactor this function to work with any NPM package. Then we could get this fix out before 0.20
aklinker1 commentedon Mar 28, 2025
Released in v0.20.0. Since we no longer use global namespaces for types, this is fixed!
https://wxt.dev/guide/essentials/extension-apis.html#accessing-types