Skip to content

Commit 35865ad

Browse files
committed
fix(storage): Throw better error message when importing outside a extension environment
This closes #371
1 parent 3b63a51 commit 35865ad

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/storage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,20 @@ function createDriver(
308308
storageArea: 'local' | 'session' | 'sync' | 'managed',
309309
): WxtStorageDriver {
310310
const getStorageArea = () => {
311-
if (browser.storage == null)
311+
if (browser.runtime == null) {
312+
throw Error(
313+
[
314+
"'wxt/storage' must be loaded in a web extension environment",
315+
'\n - If thrown during a build, see https://github.com/wxt-dev/wxt/issues/371',
316+
" - If thrown during tests, mock 'wxt/browser' correctly. See https://wxt.dev/guide/testing.html\n",
317+
].join('\n'),
318+
);
319+
}
320+
if (browser.storage == null) {
312321
throw Error(
313322
"You must add the 'storage' permission to your manifest to use 'wxt/storage'",
314323
);
324+
}
315325

316326
return browser.storage[storageArea];
317327
};

0 commit comments

Comments
 (0)