wxt: Failed to load extension: It isn't UTF-8 encoded
Describe the bug
after pnpm dev, I got this error from chrome:
To Reproduce
branch: https://github.com/qiweiii/markdown-sticky-notes/tree/migrate-wxt Steps to reproduce the bug using the reproduction:
- Install dependencies:
pnpm i - Start dev mode:
pnpm dev
Expected behavior
Expected to open in chrome
Screenshots
Environment
System:
OS: macOS 14.2.1
CPU: (12) arm64 Apple M2 Max
Memory: 640.95 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
Yarn: 1.22.21 - ~/.nvm/versions/node/v20.9.0/bin/yarn
npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
pnpm: 8.13.1 - ~/.nvm/versions/node/v20.9.0/bin/pnpm
bun: 1.0.11 - ~/.bun/bin/bun
Browsers:
Chrome: 120.0.6099.216
Safari: 17.2.1
npmPackages:
wxt: ^0.14.3 => 0.14.3
Additional context
I am trying to migrate this old extension to wxt, the branch is wip so may have many problems, any help would be appreciated!
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 2
- Comments: 15 (8 by maintainers)
Yes, I used a custom vite plugin as suggested above
In my code, it’s here
Yeah, I’m looking for a more general solution.
I guess for now there’s a workaround (https://github.com/wxt-dev/wxt/issues/353#issuecomment-2475525361), and this seems to happen rarely enough that I don’t want to add this to all projects for performance reasons. Perhaps Vite 6 and rolldown will use the esbuild approach and default to writing files as ascii instead of utf-8. Then the problem will fix itself.
Until then, we could create a wxt module that adds this workaround’s vite plugin and tell people to install it if they run into this error.
Can confirm it is working for
react-markdown,rehype-katex,remark-mathsetup.If one of you could try this function out, that’d be helpful. I’m a little concerned about iterating over every character of the bundle to fix a rare case that doesn’t happen for everyone.
You can test this out by adding a custom vite plugin, and calling the function @lionelhorn mentioned on the bundle during the
generateBundlehook.On my phone right now, will add a full example if that’s not enough to get you started
That’s my concern as well. Peaking at @qiweiii’s extension code, it seems the culprit is rehype-katex, which is the same for us! I wonder if there’s a
rehype-katexspecific solution (such as encoding only this file) that doesn’t affect other files, or if we should ask therehype-katexauthor to support UTF-8. cc: @wooorm 👀@aklinker1
Depending on the wxt.config.ts and vite build options (terser used as minifier or not), I also get these errors.
Would something as simple as this in a vite plugin that can be activated or not solve the issue?
https://github.com/PlasmoHQ/plasmo/blob/6c559127f70d356e3325712e70502b082579f13f/packages/parcel-optimizer-terser/src/to-utf8.ts
Did some googling, I found a few other references to this UTF8 issue.
There’s probably some non-encoded UTF characters being output that makes Chrome think your file isn’t valid. The file was too big to look through myself, so I switched to trying to find the code/modules causing the problem.
After a binary search of commenting things out, I found this has something to do with the
@uiw/react-codemirrormodule. I was able to get it building by commenting it out in the editor component. Then I had to make a few other changes to actually get a working build:Full patch to get it working
Obviously removing the editor isn’t a solution lol. Instead, I’d recommend you load the editor inside an iframe, and just have the content script insert the iframe. That way you don’t have to include
@uiw/react-codemirrorinside a content script. Maybe that would work-around the error with your dependency.See WXT’s
createIframeUihelper: https://wxt.dev/guide/content-script-ui.html#iframe