From 35865adcb256a685761a99df2684bdf8712c7486 Mon Sep 17 00:00:00 2001 From: Aaron Klinker Date: Sat, 27 Jan 2024 00:26:35 -0600 Subject: [PATCH] fix(storage): Throw better error message when importing outside a extension environment This closes #371 --- src/storage.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/storage.ts b/src/storage.ts index 5dc9390c..f701f4f8 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -308,10 +308,20 @@ function createDriver( storageArea: 'local' | 'session' | 'sync' | 'managed', ): WxtStorageDriver { const getStorageArea = () => { - if (browser.storage == null) + if (browser.runtime == null) { + throw Error( + [ + "'wxt/storage' must be loaded in a web extension environment", + '\n - If thrown during a build, see https://github.com/wxt-dev/wxt/issues/371', + " - If thrown during tests, mock 'wxt/browser' correctly. See https://wxt.dev/guide/testing.html\n", + ].join('\n'), + ); + } + if (browser.storage == null) { throw Error( "You must add the 'storage' permission to your manifest to use 'wxt/storage'", ); + } return browser.storage[storageArea]; };