Skip to content

Commit bdb49c0

Browse files
committed
docs: Final changes to 0.20 upgrade guide
1 parent cc3c43d commit bdb49c0

File tree

1 file changed

+86
-45
lines changed

1 file changed

+86
-45
lines changed

docs/guide/resources/upgrading.md

+86-45
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ Read through all the changes once before making any code changes.
2626

2727
### `webextension-polyfill` Removed
2828

29-
WXT no longer uses the `webextension-polyfill`!
29+
WXT's `browser` no longer uses the `webextension-polyfill`!
3030

31-
Why? The polyfill doesn't add any value anymore. WXT's new `browser` supports the promise-based APIs on all browsers/manifest versions and all shared APIs behave the same way.
31+
:::details Why?
32+
See https://github.com/wxt-dev/wxt/issues/784
33+
:::
3234

3335
To upgrade, you have two options:
3436

3537
1. **Stop using the polyfill** - No changes necessary, though you may want to do some manual testing to make sure everything continues to work. None of the early testers of this feature reported any runtime issues once they stopped using the polyfill.
38+
- If you're already using `extensionApi: "chrome"`, then you don't need to test anything! You're already using the same `browser` object v0.20 provides by default.
3639
2. **Continue using the polyfill** - If you want to keep using the polyfill, you can! One less thing to worry about during this upgrade.
3740
- Install `webextension-polyfill` and WXT's [new polyfill module](https://www.npmjs.com/package/@wxt-dev/webextension-polyfill):
3841
```sh
@@ -45,17 +48,27 @@ To upgrade, you have two options:
4548
});
4649
```
4750

48-
Regardless of your choice, the `extensionApi` config has been removed. Remove it from your `wxt.config.ts` file if present:
51+
The new `browser` object (and types) is backed by WXT's new package: [`@wxt-dev/browser`](https://www.npmjs.com/package/@wxt-dev/browser). This package continues WXT's mission of providing useful packages for the whole community. Just like [`@wxt-dev/storage`](https://www.npmjs.com/package/@wxt-dev/storage), [`@wxt-dev/i18n`](https://www.npmjs.com/package/@wxt-dev/i18n), [`@wxt-dev/analytics`](https://www.npmjs.com/package/@wxt-dev/analytics), it is designed to be easy to use in any web extension project, not just those using WXT, and provides a consistent API across all browsers and manifest versions.
52+
53+
### `extensionApi` Config Removed
54+
55+
The `extensionApi` config has been removed. Before, this config provided a way to opt into using the new `browser` object prior to v0.20.0.
56+
57+
Remove it from your `wxt.config.ts` file if present:
4958

5059
```ts [wxt.config.ts]
5160
export default defineConfig({
5261
extensionApi: 'chrome', // [!code --]
5362
});
5463
```
5564

56-
Additionally, how you consume extension API types have changed. `wxt/browser` now uses types based on `@types/chrome` instead of `@types/webextension-polyfill`.
65+
### Extension API Type Changes
66+
67+
With the new `browser` introduced in v0.20, how you access types has changed. WXT now provides types based on `@types/chrome` instead of `@types/webextension-polyfill`.
5768

58-
Types are now accessed off the `Browser` namespace:
69+
These types are more up-to-date with MV3 APIs, contain less bugs, are better organized, and don't have any auto-generated names.
70+
71+
To access types, use the new `Browser` namespace from `wxt/browser`:
5972
6073
<!-- prettier-ignore -->
6174
```ts
@@ -68,23 +81,17 @@ function getMessageSenderUrl(sender: Browser.runtime.MessageSender): string { //
6881
}
6982
```
7083
71-
These new types contain up-to-date MV3 APIs, contain less bugs, and don't have any auto-generated names.
84+
> If you use auto-imports, `Browser` will be available without manually importing it.
85+
86+
Not all type names will be the same as what `@types/webextension-polyfill` provides. You'll have to find the new type names by looking at the types of the `browser.*` API's you use.
7287
7388
### `public/` and `modules/` Directories Moved
7489
75-
The default location for the `public/` and `modules/` directories have changed to better align with standards set by other frameworks (Nuxt, Next, Astro, etc). Now, each path is relative to the project's root directory, not the src directory.
90+
The default location for the `public/` and `modules/` directories have changed to better align with standards set by other frameworks (Nuxt, Next, Astro, etc). Now, each path is relative to the project's **root directory**, not the src directory.
7691

7792
- If you follow the default folder structure, you don't need to make any changes.
7893
- If you set a custom `srcDir`, you have two options:
79-
1. Keep the folders in the same place and update your project config:
80-
```ts [wxt.config.ts]
81-
export default defineConfig({
82-
srcDir: 'src',
83-
publicDir: 'src/public', // [!code ++]
84-
modulesDir: 'src/modules', // [!code ++]
85-
});
86-
```
87-
2. Move the your `public/` and `modules/` directories to the project root:
94+
1. Move the your `public/` and `modules/` directories to the project root:
8895
```diff
8996
<root>/
9097
+ modules/
@@ -97,12 +104,32 @@ The default location for the `public/` and `modules/` directories have changed t
97104
utils/
98105
wxt.config.ts
99106
```
107+
2. Keep the folders in the same place and update your project config:
108+
```ts [wxt.config.ts]
109+
export default defineConfig({
110+
srcDir: 'src',
111+
publicDir: 'src/public', // [!code ++]
112+
modulesDir: 'src/modules', // [!code ++]
113+
});
114+
```
115+
116+
### Import Path Changes and `#imports`
117+
118+
The APIs exported by `wxt/sandbox`, `wxt/client`, or `wxt/storage` have moved to individual exports under the `wxt/utils/*` path.
119+
120+
:::details Why?
121+
As WXT grows and more utilities are added, any helper with side-effects will not be tree-shaken out of your final bundle.
100122
101-
### Import Path Changes
123+
This can cause problems because not every API used by these side-effects is available in every type of entrypoint. Some APIs can only be used in the background, sandboxed pages can't use any extension API, etc. This was leading to JS throwing errors in the top-level scope, preventing your code from running.
102124

103-
The APIs exported by `wxt/sandbox`, `wxt/client`, or `wxt/storage` have moved to `wxt/utils/*`.
125+
Splitting each util into it's own module solves this problem, making sure you're only importing APIs and side-effects into entrypoints they can run in.
126+
:::
127+
128+
Refer to the updated [API Reference](/api/reference/) to see the list of new import paths.
129+
130+
However, you don't need to memorize or learn the new import paths! v0.20 introduces a new virtual module, `#imports`, that abstracts all this away from developers. See the [blog post](/blog/2024-12-06-using-imports-module) for more details about how this module works.
104131
105-
To upgrade, replace these imports with the new `#imports` module:
132+
So to upgrade, just replace any imports from `wxt/storage`, `wxt/client`, and `wxt/sandbox` with an import to the new `#imports` module:
106133
107134
```ts
108135
import { storage } from 'wxt/storage'; // [!code --]
@@ -115,20 +142,34 @@ import { ContentScriptContext, useAppConfig } from '#imports'; // [!code ++]
115142
116143
You can combine the imports into a single import statement, but it's easier to just find/replace each statement.
117144
145+
```ts
146+
import { storage } from 'wxt/storage'; // [!code --]
147+
import { defineContentScript } from 'wxt/sandbox'; // [!code --]
148+
import { ContentScriptContext, useAppConfig } from 'wxt/client'; // [!code --]
149+
import {
150+
// [!code ++]
151+
storage, // [!code ++]
152+
defineContentScript, // [!code ++]
153+
ContentScriptContext, // [!code ++]
154+
useAppConfig, // [!code ++]
155+
} from '#imports'; // [!code ++]
156+
```
157+
118158
:::tip
119159
Before types will work, you'll need to run `wxt prepare` after installing v0.20 to generate the new TypeScript declarations.
120160
:::
121161
122-
Read more about the new `#imports` module in the [blog post](/blog/2024-12-06-using-imports-module).
123-
124162
### `createShadowRootUi` CSS Changes
125163
126164
WXT now resets styles inherited from the webpage (`visibility`, `color`, `font-size`, etc.) by setting `all: initial` inside the shadow root.
127165
166+
:::warning
167+
This doesn't effect `rem` units. You should continue using `postcss-rem-to-px` or an equivalent library if the webpage sets the HTML element's `font-size`.
168+
:::
169+
128170
If you use `createShadowRootUi`:
129171
130-
1. Double check that your UI looks the same as before.
131-
2. If you have any manual CSS resets to these styles, you can remove them. For example:
172+
1. Remove any manual CSS overrides that reset the style of specific websites. For example:
132173
133174
<!-- prettier-ignore -->
134175
```css [entrypoints/reddit.content/style.css]
@@ -138,9 +179,7 @@ If you use `createShadowRootUi`:
138179
} /* [!code --] */
139180
```
140181
141-
:::warning
142-
This doesn't effect `rem` units. You should continue using `postcss-rem-to-px` or an equivalent library if the webpage sets the HTML element's `font-size`.
143-
:::
182+
2. Double check that your UI looks the same as before.
144183
145184
If you run into problems with the new behavior, you can disable it and continue using your current CSS:
146185
@@ -159,7 +198,7 @@ The default value for the [`outDirTemplate`](/api/reference/wxt/interfaces/Inlin
159198
- `--mode development` &rarr; `.output/chrome-mv3-dev`: Dev mode now has a `-dev` suffix so it doesn't overwrite production builds
160199
- `--mode custom` &rarr; `.output/chrome-mv3-custom`: Other custom modes end with a `-[mode]` suffix
161200
162-
To use the old behavior, writing all output to the same directory, set `outDirTemplate` option:
201+
To use the old behavior, writing all output to the same directory, set the `outDirTemplate` option:
163202
164203
```ts [wxt.config.ts]
165204
export default defineConfig({
@@ -171,7 +210,26 @@ export default defineConfig({
171210
If you've previously loaded the extension into your browser manually for development, you'll need to uninstall and re-install it from the new dev output directory.
172211
:::
173212
174-
### `runner` APIs Renamed
213+
### Deprecated APIs Removed
214+
215+
- `entrypointLoader` option: WXT now uses `vite-node` for importing entrypoints during the build process.
216+
> This was deprecated in v0.19.0, see the [v0.19 section](#v0-18-5-rarr-v0-19-0) for migration steps.
217+
- `transformManifest` option: Use the `build:manifestGenerated` hook to transform the manifest instead:
218+
<!-- prettier-ignore -->
219+
```ts [wxt.config.ts]
220+
export default defineConfig({
221+
transformManifest(manifest) { // [!code --]
222+
hooks: { // [!code ++]
223+
'build:manifestGenerated': (_, manifest) => { // [!code ++]
224+
// ...
225+
}, // [!code ++]
226+
},
227+
});
228+
```
229+
230+
### New Deprecations
231+
232+
#### `runner` APIs Renamed
175233
176234
To improve consistency with the `web-ext.config.ts` filename, the "runner" API and config options have been renamed. You can continue using the old names, but they have been deprecated and will be removed in a future version:
177235
@@ -195,23 +253,6 @@ To improve consistency with the `web-ext.config.ts` filename, the "runner" API a
195253
import type { WebExtConfig } from 'wxt'; // [!code ++]
196254
```
197255
198-
### Deprecated APIs Removed
199-
200-
- `entrypointLoader` option: WXT now uses `vite-node` for importing entrypoints during the build process.
201-
> This was deprecated in v0.19.0, see the [v0.19 section](#v0-18-5-rarr-v0-19-0) for migration steps.
202-
- `transformManifest` option: Use the `build:manifestGenerated` hook to transform the manifest instead:
203-
<!-- prettier-ignore -->
204-
```ts [wxt.config.ts]
205-
export default defineConfig({
206-
transformManifest(manifest) { // [!code --]
207-
hooks: { // [!code ++]
208-
'build:manifestGenerated': (_, manifest) => { // [!code ++]
209-
// ...
210-
}, // [!code ++]
211-
},
212-
});
213-
```
214-
215256
## v0.18.5 &rarr; v0.19.0
216257
217258
### `vite-node` Entrypoint Loader

0 commit comments

Comments
 (0)