Skip to content

Commit d74f6b0

Browse files
authored
feat: Extract wxt/storage to its own package, @wxt-dev/storage (#1129)
1 parent 62ea796 commit d74f6b0

File tree

15 files changed

+1166
-899
lines changed

15 files changed

+1166
-899
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ on:
1313
- module-solid
1414
- module-svelte
1515
- module-vue
16+
- storage
1617
- unocss
1718
- wxt
1819

.github/workflows/sync-releases.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ on:
77
default: wxt
88
type: choice
99
options:
10-
- wxt
11-
- module-react
12-
- module-vue
13-
- module-svelte
14-
- module-solid
1510
- auto-icons
1611
- i18n
12+
- module-react
13+
- module-solid
14+
- module-svelte
15+
- module-vue
16+
- storage
17+
- wxt
1718

1819
jobs:
1920
sync:

docs/.vitepress/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { version as wxtVersion } from '../../packages/wxt/package.json';
1313
import { version as i18nVersion } from '../../packages/i18n/package.json';
1414
import { version as autoIconsVersion } from '../../packages/auto-icons/package.json';
1515
import { version as unocssVersion } from '../../packages/unocss/package.json';
16+
import { version as storageVersion } from '../../packages/storage/package.json';
1617

1718
const title = 'Next-gen Web Extension Framework';
1819
const titleSuffix = ' – WXT';
@@ -87,7 +88,7 @@ export default defineConfig({
8788
),
8889
]),
8990
navItem('Other Packages', [
90-
navItem(`wxt/storage — ${wxtVersion}`, '/storage'),
91+
navItem(`@wxt-dev/storage — ${storageVersion}`, '/storage'),
9192
navItem(`@wxt-dev/auto-icons — ${autoIconsVersion}`, '/auto-icons'),
9293
navItem(`@wxt-dev/i18n — ${i18nVersion}`, '/i18n'),
9394
navItem(`@wxt-dev/unocss — ${unocssVersion}`, '/unocss'),

docs/storage.md

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,47 @@ outline: deep
66

77
[Changelog](https://github.com/wxt-dev/wxt/blob/main/packages/wxt/CHANGELOG.md)
88

9-
WXT provides a simplified API to replace the `browser.storage.*` APIs. Use the `storage` auto-import from `wxt/storage` or import it manually to get started:
9+
A simplified wrapper around the extension storage APIs.
10+
11+
## Installation
12+
13+
### With WXT
14+
15+
This module is built-in to WXT, so you don't need to install anything.
1016

1117
```ts
1218
import { storage } from 'wxt/storage';
1319
```
1420

15-
> [!IMPORTANT]
16-
> To use the `wxt/storage` API, the `"storage"` permission must be added to the manifest:
17-
>
18-
> ```ts
19-
> // wxt.config.ts
20-
> export default defineConfig({
21-
> manifest: {
22-
> permissions: ['storage'],
23-
> },
24-
> });
25-
> ```
21+
If you use auto-imports, `storage` is auto-imported for you, so you don't even need to import it!
22+
23+
### Without WXT
24+
25+
Install the NPM package:
26+
27+
```sh
28+
npm i @wxt-dev/storage
29+
pnpm add @wxt-dev/storage
30+
yarn add @wxt-dev/storage
31+
bun add @wxt-dev/storage
32+
```
33+
34+
```ts
35+
import { storage } from '@wxt-dev/storage';
36+
```
37+
38+
## Storage Permission
39+
40+
To use the `wxt/storage` API, the `"storage"` permission must be added to the manifest:
41+
42+
```ts
43+
// wxt.config.ts
44+
export default defineConfig({
45+
manifest: {
46+
permissions: ['storage'],
47+
},
48+
});
49+
```
2650

2751
## Basic Usage
2852

@@ -51,7 +75,7 @@ await storage.watch<number>(
5175
await storage.getMeta<{ v: number }>('local:installDate');
5276
```
5377

54-
For a full list of methods available, see the [API reference](/api/reference/wxt/storage/interfaces/WxtStorage).
78+
For a full list of methods available, see the [API reference](/api/reference/@wxt-dev/storage/interfaces/WxtStorage).
5579

5680
## Watchers
5781

@@ -134,7 +158,7 @@ const unwatch = showChangelogOnUpdate.watch((newValue) => {
134158
});
135159
```
136160

137-
For a full list of properties and methods available, see the [API reference](/api/reference/wxt/storage/interfaces/WxtStorageItem).
161+
For a full list of properties and methods available, see the [API reference](/api/reference/@wxt-dev/storage/interfaces/WxtStorageItem).
138162

139163
### Versioning
140164

@@ -330,4 +354,4 @@ await storage.setItems([
330354
]);
331355
```
332356
333-
Refer to the [API Reference](/api/reference/wxt/storage/interfaces/WxtStorage) for types and examples of how to use all the bulk APIs.
357+
Refer to the [API Reference](/api/reference/@wxt-dev/storage/interfaces/WxtStorage) for types and examples of how to use all the bulk APIs.

packages/storage/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# WXT Storage
2+
3+
[Changelog](https://github.com/wxt-dev/wxt/blob/main/packages/storage/CHANGELOG.md) &bull; [Docs](https://wxt.dev/storage.html)
4+
5+
A simplified wrapper around the extension storage APIs.
6+
7+
## Installation
8+
9+
### With WXT
10+
11+
This module is built-in to WXT, so you don't need to install anything.
12+
13+
```ts
14+
import { storage } from 'wxt/storage';
15+
```
16+
17+
If you use auto-imports, `storage` is auto-imported for you, so you don't even need to import it!
18+
19+
### Without WXT
20+
21+
Install the NPM package:
22+
23+
```sh
24+
npm i @wxt-dev/storage
25+
pnpm add @wxt-dev/storage
26+
yarn add @wxt-dev/storage
27+
bun add @wxt-dev/storage
28+
```
29+
30+
```ts
31+
import { storage } from '@wxt-dev/storage';
32+
```
33+
34+
## Usage
35+
36+
Read full docs on the [documentation website](https://wxt.dev/storage.html).

packages/storage/package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "@wxt-dev/storage",
3+
"description": "Web extension storage API provided by WXT, supports all browsers.",
4+
"version": "1.0.0",
5+
"type": "module",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/wxt-dev/wxt.git",
9+
"directory": "packages/storage"
10+
},
11+
"homepage": "https://wxt.dev/storage.html",
12+
"keywords": [
13+
"wxt",
14+
"storage",
15+
"extension",
16+
"addon",
17+
"chrome",
18+
"firefox",
19+
"edge"
20+
],
21+
"author": {
22+
"name": "Aaron Klinker",
23+
"email": "[email protected]"
24+
},
25+
"license": "MIT",
26+
"scripts": {
27+
"build": "buildc -- unbuild",
28+
"check": "buildc --deps-only -- check",
29+
"test": "buildc --deps-only -- vitest"
30+
},
31+
"dependencies": {
32+
"async-mutex": "^0.5.0",
33+
"dequal": "^2.0.3"
34+
},
35+
"devDependencies": {
36+
"@aklinker1/check": "^1.4.5",
37+
"@types/chrome": "^0.0.268",
38+
"@webext-core/fake-browser": "^1.3.1",
39+
"oxlint": "^0.9.9",
40+
"publint": "^0.2.11",
41+
"typescript": "^5.6.2",
42+
"unbuild": "^2.0.0",
43+
"vitest": "^2.0.0"
44+
},
45+
"module": "./dist/index.mjs",
46+
"types": "./dist/index.d.ts",
47+
"exports": {
48+
".": {
49+
"types": "./dist/index.d.mts",
50+
"import": "./dist/index.mjs"
51+
}
52+
},
53+
"files": [
54+
"dist"
55+
]
56+
}

packages/wxt/src/__tests__/storage.test.ts renamed to packages/storage/src/__tests__/index.test.ts

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

65
/**
76
* This works because fakeBrowser is synchronous, and is will finish any number of chained
@@ -223,7 +222,7 @@ describe('Storage Utils', () => {
223222
describe('setMeta', () => {
224223
it('should set metadata at key+$', async () => {
225224
const existing = { v: 1 };
226-
await browser.storage[storageArea].set({ count$: existing });
225+
await chrome.storage[storageArea].set({ count$: existing });
227226
const newValues = {
228227
date: Date.now(),
229228
};
@@ -239,7 +238,7 @@ describe('Storage Utils', () => {
239238
'should remove any properties set to %s',
240239
async (version) => {
241240
const existing = { v: 1 };
242-
await browser.storage[storageArea].set({ count$: existing });
241+
await chrome.storage[storageArea].set({ count$: existing });
243242
const expected = {};
244243

245244
await storage.setMeta(`${storageArea}:count`, { v: version });
@@ -1163,7 +1162,7 @@ describe('Storage Utils', () => {
11631162

11641163
await item.removeValue();
11651164
// Make sure it's actually blank before running the test
1166-
expect(await browser.storage.local.get()).toEqual({});
1165+
expect(await chrome.storage.local.get()).toEqual({});
11671166
init.mockClear();
11681167

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

0 commit comments

Comments
 (0)