wxt: Failed to load extension: It isn't UTF-8 encoded

Describe the bug

after pnpm dev, I got this error from chrome:

image

image

image

To Reproduce

branch: https://github.com/qiweiii/markdown-sticky-notes/tree/migrate-wxt Steps to reproduce the bug using the reproduction:

  1. Install dependencies: pnpm i
  2. 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)

Most upvoted comments

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.

Yes, I used a custom vite plugin as suggested above

In my code, it’s here

Can confirm it is working for react-markdown, rehype-katex, remark-math setup.

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 generateBundle hook.

On my phone right now, will add a full example if that’s not enough to get you started

For large extensions, I’d rather we don’t iterate through megabytes of strings in JS

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-katex specific solution (such as encoding only this file) that doesn’t affect other files, or if we should ask the rehype-katex author 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-codemirror module. 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
diff --git a/entrypoints/content/Editor.tsx b/entrypoints/content/Editor.tsx
index d5e7ec5..5b28859 100644
--- a/entrypoints/content/Editor.tsx
+++ b/entrypoints/content/Editor.tsx
@@ -1,5 +1,5 @@
 import { useRef } from "react";
-import CodeMirror, { ViewUpdate } from "@uiw/react-codemirror";
+// import CodeMirror, { ViewUpdate } from "@uiw/react-codemirror";
 import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
 import { languages } from "@codemirror/language-data";
 import * as themes from "@uiw/codemirror-themes-all";
@@ -19,7 +19,7 @@ const Editor = (props: Props) => {
   return (
     <div>
       {/* https://github.com/uiwjs/react-codemirror */}
-      <CodeMirror
+      {/* <CodeMirror
         value={props.value}
         extensions={[
           markdown({ base: markdownLanguage, codeLanguages: languages }),
@@ -30,7 +30,7 @@ const Editor = (props: Props) => {
         autoFocus={props.autofocus}
         style={{ fontSize: props.fontSize, fontFamily: props.fontFamily }}
         // TODO: check CodeMirror component has any more stuff need to move here or not
-      />
+      /> */}
     </div>
   );
 };
diff --git a/entrypoints/content/Note.tsx b/entrypoints/content/Note.tsx
index 6e5698a..1a7e9e7 100644
--- a/entrypoints/content/Note.tsx
+++ b/entrypoints/content/Note.tsx
@@ -23,8 +23,8 @@ import { useOutsideClickRef } from "rooks";
 import nightOwl from "react-syntax-highlighter/dist/cjs/styles/prism/night-owl";
 
 import Editor from "./Editor";
-import themes from "../themes";
-import fonts from "../fonts";
+import themes from "@/utils/themes";
+import fonts from "@/utils/fonts";
 import type { Note } from "./storage";
 
 const ITEM_HEIGHT = 20;
diff --git a/entrypoints/options/Preference.tsx b/entrypoints/options/Preference.tsx
index 8a1e663..3af059f 100644
--- a/entrypoints/options/Preference.tsx
+++ b/entrypoints/options/Preference.tsx
@@ -8,8 +8,8 @@ import Select, { SelectChangeEvent } from "@mui/material/Select";
 import Input from "@mui/material/Input";
 import { styled, withStyles } from "@mui/material/styles";
 
-import themes from "../themes";
-import fonts from "../fonts";
+import themes from "@/utils/themes";
+import fonts from "@/utils/fonts";
 
 const ITEM_HEIGHT = 20;
 const ITEM_PADDING_TOP = 5;
diff --git a/entrypoints/fonts.ts b/utils/fonts.ts
similarity index 100%
rename from entrypoints/fonts.ts
rename to utils/fonts.ts
diff --git a/entrypoints/themes.ts b/utils/themes.ts
similarity index 100%
rename from entrypoints/themes.ts
rename to utils/themes.ts
diff --git a/wxt.config.ts b/wxt.config.ts
index 03dae2e..6fa6100 100644
--- a/wxt.config.ts
+++ b/wxt.config.ts
@@ -10,10 +10,10 @@ export default defineConfig({
     short_name: "markdown-sticky-notes",
     name: "Markdown Sticky Notes",
     icons: {
-      "16": "notes16.png",
-      "32": "notes32.png",
-      "64": "notes64.png",
-      "128": "notes128.png",
+      "16": "icon/notes16.png",
+      "32": "icon/notes32.png",
+      "64": "icon/notes64.png",
+      "128": "icon/notes128.png",
     },
     permissions: ["storage", "tabs"],
     web_accessible_resources: [

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-codemirror inside a content script. Maybe that would work-around the error with your dependency.

See WXT’s createIframeUi helper: https://wxt.dev/guide/content-script-ui.html#iframe