Skip to content

Commit 6a52bb2

Browse files
authored
fix: Use @wxt-dev/browser instead of @types/chrome (#1645)
1 parent 6f970ef commit 6a52bb2

File tree

13 files changed

+54
-58
lines changed

13 files changed

+54
-58
lines changed

packages/analytics/modules/analytics/client.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import type {
99
AnalyticsEventMetadata,
1010
AnalyticsProvider,
1111
} from './types';
12+
import { browser } from '@wxt-dev/browser';
1213

1314
const ANALYTICS_PORT = '@wxt-dev/analytics';
1415

1516
export function createAnalytics(config?: AnalyticsConfig): Analytics {
16-
if (typeof chrome === 'undefined' || !chrome?.runtime?.id)
17+
if (!browser?.runtime?.id)
1718
throw Error(
1819
'Cannot use WXT analytics in contexts without access to the browser.runtime APIs',
1920
);
@@ -51,13 +52,13 @@ function createBackgroundAnalytics(
5152
defineStorageItem<boolean>('local:wxt-analytics:enabled', false);
5253

5354
// Cached values
54-
const platformInfo = chrome.runtime.getPlatformInfo();
55+
const platformInfo = browser.runtime.getPlatformInfo();
5556
const userAgent = UAParser();
5657
let userId = Promise.resolve(userIdStorage.getValue()).then(
5758
(id) => id ?? globalThis.crypto.randomUUID(),
5859
);
5960
let userProperties = userPropertiesStorage.getValue();
60-
const manifest = chrome.runtime.getManifest();
61+
const manifest = browser.runtime.getManifest();
6162

6263
const getBackgroundMeta = () => ({
6364
timestamp: Date.now(),
@@ -178,7 +179,7 @@ function createBackgroundAnalytics(
178179
config?.providers?.map((provider) => provider(analytics, config)) ?? [];
179180

180181
// Listen for messages from the rest of the extension
181-
chrome.runtime.onConnect.addListener((port) => {
182+
browser.runtime.onConnect.addListener((port) => {
182183
if (port.name === ANALYTICS_PORT) {
183184
port.onMessage.addListener(({ fn, args }) => {
184185
// @ts-expect-error: Untyped fn key
@@ -194,7 +195,7 @@ function createBackgroundAnalytics(
194195
* Creates an analytics client for non-background contexts.
195196
*/
196197
function createFrontendAnalytics(): Analytics {
197-
const port = chrome.runtime.connect({ name: ANALYTICS_PORT });
198+
const port = browser.runtime.connect({ name: ANALYTICS_PORT });
198199
const sessionId = Date.now();
199200
const getFrontendMetadata = (): AnalyticsEventMetadata => ({
200201
sessionId,
@@ -252,8 +253,8 @@ function defineStorageItem<T>(
252253
): AnalyticsStorageItem<T> {
253254
return {
254255
getValue: async () =>
255-
(await chrome.storage.local.get(key))[key] ?? defaultValue,
256-
setValue: (newValue) => chrome.storage.local.set({ [key]: newValue }),
256+
(await browser.storage.local.get(key))[key] ?? defaultValue,
257+
setValue: (newValue) => browser.storage.local.set({ [key]: newValue }),
257258
};
258259
}
259260

packages/analytics/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
},
5353
"devDependencies": {
5454
"@aklinker1/check": "2.0.0",
55-
"@types/chrome": "^0.0.313",
5655
"@types/ua-parser-js": "^0.7.39",
5756
"publint": "^0.3.12",
5857
"typescript": "^5.8.3",
5958
"unbuild": "^3.5.0",
6059
"wxt": "workspace:*"
6160
},
6261
"dependencies": {
62+
"@wxt-dev/browser": "workspace:*",
6363
"ua-parser-js": "^1.0.40"
6464
}
6565
}

packages/analytics/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"compilerOptions": {
44
"paths": {
55
"#analytics": ["./.wxt/analytics/index.ts"]
6-
},
7-
"types": ["chrome"]
6+
}
87
},
98
"exclude": ["node_modules", "dist"]
109
}

packages/i18n/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"test": "buildc --deps-only -- vitest"
2727
},
2828
"dependencies": {
29+
"@wxt-dev/browser": "workspace:*",
2930
"chokidar": "^4.0.3",
3031
"confbox": "^0.1.8 || ^0.2.2",
3132
"fast-glob": "^3.3.3"
@@ -40,7 +41,6 @@
4041
},
4142
"devDependencies": {
4243
"@aklinker1/check": "2.0.0",
43-
"@types/chrome": "^0.0.313",
4444
"@types/node": "^20.17.6",
4545
"oxlint": "^0.16.8",
4646
"publint": "^0.3.12",

packages/i18n/src/__tests__/index.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import { createI18n } from '../index';
3+
import { browser } from '@wxt-dev/browser';
34

4-
const getMessageMock = vi.fn();
5-
6-
vi.stubGlobal('chrome', {
7-
i18n: {
8-
getMessage: getMessageMock,
9-
},
5+
vi.mock('@wxt-dev/browser', async () => {
6+
const { vi } = await import('vitest');
7+
return {
8+
browser: {
9+
i18n: {
10+
getMessage: vi.fn(),
11+
},
12+
},
13+
};
1014
});
15+
const getMessageMock = vi.mocked(browser.i18n.getMessage);
1116

1217
describe('createI18n', () => {
1318
beforeEach(() => {

packages/i18n/src/__tests__/types.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { beforeEach, describe, it, vi } from 'vitest';
22
import { createI18n } from '..';
3+
import { browser } from '@wxt-dev/browser';
34

4-
const getMessageMock = vi.fn();
5-
6-
vi.stubGlobal('chrome', {
7-
i18n: {
8-
getMessage: getMessageMock,
9-
},
5+
vi.mock('@wxt-dev/browser', async () => {
6+
const { vi } = await import('vitest');
7+
return {
8+
browser: {
9+
i18n: {
10+
getMessage: vi.fn(),
11+
},
12+
},
13+
};
1014
});
15+
const getMessageMock = vi.mocked(browser.i18n.getMessage);
1116

1217
const n: number = 1;
1318

packages/i18n/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
I18n,
88
Substitution,
99
} from './types';
10+
import { browser } from '@wxt-dev/browser';
1011

1112
export function createI18n<
1213
T extends I18nStructure = DefaultI18nStructure,
@@ -39,9 +40,9 @@ export function createI18n<
3940
if (sub?.length) {
4041
// Convert all substitutions to strings
4142
const stringSubs = sub?.map((sub) => String(sub));
42-
message = chrome.i18n.getMessage(key.replaceAll('.', '_'), stringSubs);
43+
message = browser.i18n.getMessage(key.replaceAll('.', '_'), stringSubs);
4344
} else {
44-
message = chrome.i18n.getMessage(key.replaceAll('.', '_'));
45+
message = browser.i18n.getMessage(key.replaceAll('.', '_'));
4546
}
4647
if (!message) {
4748
console.warn(`[i18n] Message not found: "${key}"`);

packages/i18n/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"types": ["chrome", "node"]
4+
"types": ["node"]
55
},
66
"exclude": ["node_modules/**", "dist/**"]
77
}

packages/storage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
"test": "buildc --deps-only -- vitest"
3131
},
3232
"dependencies": {
33+
"@wxt-dev/browser": "workspace:*",
3334
"async-mutex": "^0.5.0",
3435
"dequal": "^2.0.3"
3536
},
3637
"devDependencies": {
3738
"@aklinker1/check": "2.0.0",
38-
"@types/chrome": "^0.0.313",
3939
"@webext-core/fake-browser": "^1.3.2",
4040
"oxlint": "^0.16.8",
4141
"publint": "^0.3.12",

packages/storage/src/__tests__/index.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fakeBrowser } from '@webext-core/fake-browser';
22
import { describe, it, expect, beforeEach, vi, expectTypeOf } from 'vitest';
33
import { MigrationError, type WxtStorageItem, storage } from '../index';
4+
import { browser } from '@wxt-dev/browser';
45

56
/**
67
* This works because fakeBrowser is synchronous, and is will finish any number of chained
@@ -222,7 +223,7 @@ describe('Storage Utils', () => {
222223
describe('setMeta', () => {
223224
it('should set metadata at key+$', async () => {
224225
const existing = { v: 1 };
225-
await chrome.storage[storageArea].set({ count$: existing });
226+
await browser.storage[storageArea].set({ count$: existing });
226227
const newValues = {
227228
date: Date.now(),
228229
};
@@ -238,7 +239,7 @@ describe('Storage Utils', () => {
238239
'should remove any properties set to %s',
239240
async (version) => {
240241
const existing = { v: 1 };
241-
await chrome.storage[storageArea].set({ count$: existing });
242+
await browser.storage[storageArea].set({ count$: existing });
242243
const expected = {};
243244

244245
await storage.setMeta(`${storageArea}:count`, { v: version });
@@ -1265,7 +1266,7 @@ describe('Storage Utils', () => {
12651266

12661267
await item.removeValue();
12671268
// Make sure it's actually blank before running the test
1268-
expect(await chrome.storage.local.get()).toEqual({});
1269+
expect(await browser.storage.local.get()).toEqual({});
12691270
init.mockClear();
12701271

12711272
const [value1, value2] = await Promise.all([

0 commit comments

Comments
 (0)