fix: Add getAppConfig as an alias to useAppConfig (#2144)
Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
@@ -23,12 +23,12 @@ export default defineAppConfig({
|
||||
This file is committed to the repo, so don't put any secrets here. Instead, use [Environment Variables](/guide/essentials/config/environment-variables)
|
||||
:::
|
||||
|
||||
To access runtime config, WXT provides the `useAppConfig` function:
|
||||
To access runtime config, WXT provides the `getAppConfig` function:
|
||||
|
||||
```ts
|
||||
import { useAppConfig } from '#imports';
|
||||
import { getAppConfig } from '#imports';
|
||||
|
||||
console.log(useAppConfig()); // { theme: "dark" }
|
||||
console.log(getAppConfig()); // { theme: "dark" }
|
||||
```
|
||||
|
||||
## Environment Variables in App Config
|
||||
|
||||
@@ -131,7 +131,7 @@ declare module 'wxt/utils/define-app-config' {
|
||||
Runtime options are returned when calling
|
||||
|
||||
```ts
|
||||
const config = useAppConfig();
|
||||
const config = getAppConfig();
|
||||
console.log(config.myModule);
|
||||
```
|
||||
|
||||
@@ -220,7 +220,7 @@ export default defineWxtModule({
|
||||
const analyticsModuleCode = `
|
||||
import { createAnalytics } from 'some-module';
|
||||
|
||||
export const analytics = createAnalytics(useAppConfig().analytics);
|
||||
export const analytics = createAnalytics(getAppConfig().analytics);
|
||||
export const { reportEvent, reportPageView } = analytics;
|
||||
`;
|
||||
|
||||
|
||||
@@ -177,7 +177,7 @@ import { defineContentScript } from 'wxt/sandbox'; // [!code --]
|
||||
import { ContentScriptContext, useAppConfig } from 'wxt/client'; // [!code --]
|
||||
import { storage } from '#imports'; // [!code ++]
|
||||
import { defineContentScript } from '#imports'; // [!code ++]
|
||||
import { ContentScriptContext, useAppConfig } from '#imports'; // [!code ++]
|
||||
import { ContentScriptContext, getAppConfig } from '#imports'; // [!code ++]
|
||||
```
|
||||
|
||||
You can combine the imports into a single import statement, but it's easier to just find/replace each statement.
|
||||
@@ -191,7 +191,7 @@ import {
|
||||
storage, // [!code ++]
|
||||
defineContentScript, // [!code ++]
|
||||
ContentScriptContext, // [!code ++]
|
||||
useAppConfig, // [!code ++]
|
||||
getAppConfig, // [!code ++]
|
||||
} from '#imports'; // [!code ++]
|
||||
```
|
||||
|
||||
|
||||
@@ -44,9 +44,9 @@ export default defineWxtModule({
|
||||
? clientModuleId
|
||||
: normalizePath(relative(wxtAnalyticsFolder, clientModuleId))
|
||||
}';
|
||||
import { useAppConfig } from '#imports';
|
||||
import { getAppConfig } from '#imports';
|
||||
|
||||
export const analytics = createAnalytics(useAppConfig().analytics);
|
||||
export const analytics = createAnalytics(getAppConfig().analytics);
|
||||
`;
|
||||
addAlias(wxt, '#analytics', wxtAnalyticsIndex);
|
||||
wxt.hook('prepare:types', async (_, entries) => {
|
||||
|
||||
@@ -12,7 +12,7 @@ export default defineBackground({
|
||||
manifestVersion: import.meta.env.MANIFEST_VERSION,
|
||||
});
|
||||
|
||||
console.log(useAppConfig());
|
||||
console.log(getAppConfig());
|
||||
|
||||
browser.runtime.getURL('/');
|
||||
browser.runtime.getURL('/background.js');
|
||||
|
||||
@@ -47,6 +47,7 @@ exports[`Auto Imports > eslintrc > "enabled: 8" should output a JSON config file
|
||||
"defineUnlistedScript": true,
|
||||
"defineWxtPlugin": true,
|
||||
"fakeBrowser": true,
|
||||
"getAppConfig": true,
|
||||
"injectScript": true,
|
||||
"storage": true,
|
||||
"useAppConfig": true
|
||||
@@ -101,6 +102,7 @@ const globals = {
|
||||
"defineUnlistedScript": true,
|
||||
"defineWxtPlugin": true,
|
||||
"fakeBrowser": true,
|
||||
"getAppConfig": true,
|
||||
"injectScript": true,
|
||||
"storage": true,
|
||||
"useAppConfig": true
|
||||
@@ -164,6 +166,7 @@ exports[`Auto Imports > eslintrc > "enabled: true" should output a JSON config f
|
||||
"defineUnlistedScript": true,
|
||||
"defineWxtPlugin": true,
|
||||
"fakeBrowser": true,
|
||||
"getAppConfig": true,
|
||||
"injectScript": true,
|
||||
"storage": true,
|
||||
"useAppConfig": true
|
||||
@@ -219,6 +222,7 @@ exports[`Auto Imports > eslintrc > should allow customizing the output 1`] = `
|
||||
"defineUnlistedScript": "readonly",
|
||||
"defineWxtPlugin": "readonly",
|
||||
"fakeBrowser": "readonly",
|
||||
"getAppConfig": "readonly",
|
||||
"injectScript": "readonly",
|
||||
"storage": "readonly",
|
||||
"useAppConfig": "readonly"
|
||||
|
||||
@@ -30,6 +30,7 @@ describe('Auto Imports', () => {
|
||||
const defineUnlistedScript: typeof import('wxt/utils/define-unlisted-script').defineUnlistedScript
|
||||
const defineWxtPlugin: typeof import('wxt/utils/define-wxt-plugin').defineWxtPlugin
|
||||
const fakeBrowser: typeof import('wxt/testing').fakeBrowser
|
||||
const getAppConfig: typeof import('wxt/utils/app-config').getAppConfig
|
||||
const injectScript: typeof import('wxt/utils/inject-script').injectScript
|
||||
const storage: typeof import('wxt/utils/storage').storage
|
||||
const useAppConfig: typeof import('wxt/utils/app-config').useAppConfig
|
||||
@@ -111,7 +112,7 @@ describe('Auto Imports', () => {
|
||||
declare module '#imports' {
|
||||
export { browser, Browser } from 'wxt/browser';
|
||||
export { storage, StorageArea, WxtStorage, WxtStorageItem, StorageItemKey, StorageAreaChanges, MigrationError } from 'wxt/utils/storage';
|
||||
export { useAppConfig } from 'wxt/utils/app-config';
|
||||
export { getAppConfig, useAppConfig } from 'wxt/utils/app-config';
|
||||
export { ContentScriptContext, WxtWindowEventMap } from 'wxt/utils/content-script-context';
|
||||
export { createIframeUi, IframeContentScriptUi, IframeContentScriptUiOptions } from 'wxt/utils/content-script-ui/iframe';
|
||||
export { createIntegratedUi, IntegratedContentScriptUi, IntegratedContentScriptUiOptions } from 'wxt/utils/content-script-ui/integrated';
|
||||
@@ -196,7 +197,7 @@ describe('Auto Imports', () => {
|
||||
declare module '#imports' {
|
||||
export { browser, Browser } from 'wxt/browser';
|
||||
export { storage, StorageArea, WxtStorage, WxtStorageItem, StorageItemKey, StorageAreaChanges, MigrationError } from 'wxt/utils/storage';
|
||||
export { useAppConfig } from 'wxt/utils/app-config';
|
||||
export { getAppConfig, useAppConfig } from 'wxt/utils/app-config';
|
||||
export { ContentScriptContext, WxtWindowEventMap } from 'wxt/utils/content-script-context';
|
||||
export { createIframeUi, IframeContentScriptUi, IframeContentScriptUiOptions } from 'wxt/utils/content-script-ui/iframe';
|
||||
export { createIntegratedUi, IntegratedContentScriptUi, IntegratedContentScriptUiOptions } from 'wxt/utils/content-script-ui/integrated';
|
||||
|
||||
@@ -387,7 +387,7 @@ async function getUnimportOptions(
|
||||
},
|
||||
{
|
||||
from: 'wxt/utils/app-config',
|
||||
imports: defineImportsAndTypes(['useAppConfig'], []),
|
||||
imports: defineImportsAndTypes(['getAppConfig', 'useAppConfig'], []),
|
||||
},
|
||||
{
|
||||
from: 'wxt/utils/content-script-context',
|
||||
|
||||
@@ -3,6 +3,20 @@
|
||||
import appConfig from 'virtual:app-config';
|
||||
import type { WxtAppConfig } from './define-app-config';
|
||||
|
||||
export function useAppConfig(): WxtAppConfig {
|
||||
/**
|
||||
* Get runtime config defined in `<srcDir>/app.config.ts`
|
||||
*
|
||||
* @see https://wxt.dev/guide/essentials/config/runtime.html
|
||||
*/
|
||||
export function getAppConfig(): WxtAppConfig {
|
||||
return appConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for {@link getAppConfig}.
|
||||
*
|
||||
* @see https://wxt.dev/guide/essentials/config/runtime.html
|
||||
*/
|
||||
export function useAppConfig(): WxtAppConfig {
|
||||
return getAppConfig();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user