-
-
Notifications
You must be signed in to change notification settings - Fork 294
feat(experimental): First-class support for excluding webextension-polyfill
#847
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
Changes from 6 commits
8d5fbb2
4c684e7
2f7fb30
b1ef71a
7b32dd7
e4ec4a2
14bae66
7eb0a36
d64af5d
0aca961
71620a0
ac9a438
24e4abc
7177405
229dfc8
c05f42c
5e1c94a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,7 +264,7 @@ | |
await project.build({ | ||
experimental: { | ||
// Simplify the build output for comparison | ||
includeBrowserPolyfill: false, | ||
extensionApi: 'chrome', | ||
}, | ||
vite: () => ({ | ||
build: { | ||
|
@@ -275,7 +275,7 @@ | |
}); | ||
|
||
expect(await project.serializeFile('.output/chrome-mv3/background.js')) | ||
.toMatchInlineSnapshot(` | ||
Check failure on line 278 in packages/wxt/e2e/tests/output-structure.test.ts
|
||
".output/chrome-mv3/background.js | ||
---------------------------------------- | ||
import { l as logHello, i as initPlugins } from "./chunks/_virtual_wxt-plugins-OjKtWpmY.js"; | ||
|
@@ -345,7 +345,7 @@ | |
await project.build({ | ||
experimental: { | ||
// Simplify the build output for comparison | ||
includeBrowserPolyfill: false, | ||
extensionApi: 'chrome', | ||
}, | ||
vite: () => ({ | ||
build: { | ||
|
@@ -356,7 +356,7 @@ | |
}); | ||
|
||
expect(await project.serializeFile('.output/chrome-mv3/background.js')) | ||
.toMatchInlineSnapshot(` | ||
Check failure on line 359 in packages/wxt/e2e/tests/output-structure.test.ts
|
||
".output/chrome-mv3/background.js | ||
---------------------------------------- | ||
var _background = function() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/// <reference types="chrome" /> | ||
|
||
export type Browser = typeof chrome; | ||
|
||
export type AugmentedBrowser = Browser & { | ||
runtime: WxtRuntime; | ||
i18n: WxtI18n; | ||
}; | ||
|
||
export interface WxtRuntime { | ||
// Overriden per-project | ||
} | ||
|
||
export interface WxtI18n { | ||
// Overriden per-project | ||
} | ||
|
||
export const browser: AugmentedBrowser = | ||
// @ts-expect-error | ||
globalThis.browser?.runtime?.id == null | ||
? globalThis.chrome | ||
: // @ts-expect-error | ||
globalThis.browser; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use For firefox MV2, 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* @module wxt/browser | ||
*/ | ||
export * from './webextension-polyfill'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ResolvedConfig } from '../../../../types'; | ||
import type * as vite from 'vite'; | ||
|
||
/** | ||
* Apply the experimental config for which extension API is used. This only | ||
* 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Add eslint rule to enforce this. |
||
*/ | ||
export function resolveExtensionApi(config: ResolvedConfig): vite.Plugin { | ||
return { | ||
name: 'wxt:resolve-extension-api', | ||
config() { | ||
// Only apply the config if we're disabling the polyfill | ||
if (config.experimental.extensionApi === 'webextension-polyfill') return; | ||
|
||
return { | ||
resolve: { | ||
alias: [ | ||
{ find: /^wxt\/browser$/, replacement: 'wxt/browser/chrome' }, | ||
], | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { fakeBrowser } from 'wxt/testing'; | ||
|
||
export const browser = fakeBrowser; |
There was a problem hiding this comment.
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 fromwxt/browser
and it will be resolved from the NPM module instead of using the local file, and this will be replaced withwxt/browser/chrome
when usingextensionApi: "chrome"