Closed vimcaw closed 1 year ago
Good catch. I think I know how to solve this. Rather than transforming HTML file myself, I should fetch it from the dev server. That way all plugins apply properly.
The main problem with that is that the dev server starts after we transform the HTML files.... I'll have to figure out a way to change up the order. Hmmm...
Thanks for sharing, I'll try and get this fixed today
I try to inject the missing script to dist/src/popup.html
as a temp workaround, but chrome throws some errors:
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' http://localhost:* http://127.0.0.1:*". Either the 'unsafe-inline' keyword, a hash ('sha256-ziklEcVoMM12lYuZURXft0fBUfwBk0XufOImFQWUAXk='), or a nonce ('nonce-...') is required to enable inline execution.
Seems like an inline script is disallowed in a browser extension, maybe it only allows a script file link, which will make it tricky to solve (maybe place this to a separate file and link it).
Yeah, the problem is this content is only included when using the react plugin, and I don't want to make any react-only changes to the codebase if I don't need to.
I can add unsafe-inline
to the CSV for dev mode. If that doesn't work, I can process the HTML after the server has returned it and put inline scripts in their own virtual module.
Yup, Chrome is not OK with us using unsafe-inline
. I'll have to take the virtual module approach... Which will be a bit harder.
Alright, I got something working. I didn't have to deal with the inline script nonsense, Vite took care of it automatically for me.
I am getting two warnings. I don't have much experience working with Vite and React, so I'll debug later today.
Good progress though, this should be fixed soon.
Nevermind, one last change before I get off. I fixed the top warning.
The bottom warning appears to be a problem with calling ReactDOM.createRoot
inside the file that I'm saving to HMR. I need to separate it from the component. I'll update the template with a better file layout.
So this is pretty much good to go. #85 will be merged later today soon and I'll get a release out.
After testing, seems like still has an error here, I think in HTML file, import "/@react-refresh"
should be import "http://localhost:5173/@react-refresh"
(see https://github.com/vitejs/vite/issues/1984#issuecomment-778420441)
vite-plugin-web-extension-bug-reproduction on main [!] via v19.7.0 took 8s
❯ pnpm dev
> test@1.0.0 dev /Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction
> vite dev
Build Steps
1. Building src/popup.html indvidually
2. Building src/background.ts indvidually
Building src/popup.html (1/2)
VITE v4.1.4 ready in 566 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h to show help
vite v4.1.4 building for production...
✓ 3 modules transformed.
[vite]: Rollup failed to resolve import "/@react-refresh" from "/Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/src/popup.html?html-proxy&index=0.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
file:///Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/node_modules/.pnpm/vite@4.1.4/node_modules/vite/dist/node/chunks/dep-ca21228b.js:44792
throw new Error(`[vite]: Rollup failed to resolve import "${exporter}" from "${id}".\n` +
^
Error: [vite]: Rollup failed to resolve import "/@react-refresh" from "/Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/src/popup.html?html-proxy&index=0.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
at onRollupWarning (file:///Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/node_modules/.pnpm/vite@4.1.4/node_modules/vite/dist/node/chunks/dep-ca21228b.js:44792:19)
at onwarn (file:///Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/node_modules/.pnpm/vite@4.1.4/node_modules/vite/dist/node/chunks/dep-ca21228b.js:44562:13)
at Object.onwarn (file:///Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/node_modules/.pnpm/rollup@3.19.1/node_modules/rollup/dist/es/shared/node-entry.js:25097:13)
at ModuleLoader.handleInvalidResolvedId (file:///Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/node_modules/.pnpm/rollup@3.19.1/node_modules/rollup/dist/es/shared/node-entry.js:23779:26)
at file:///Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/node_modules/.pnpm/rollup@3.19.1/node_modules/rollup/dist/es/shared/node-entry.js:23739:26 {
watchFiles: [
'/Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/src/popup.html',
'\x00vite/modulepreload-polyfill',
'/Users/vimcaw/Downloads/vite-plugin-web-extension-bug-reproduction/src/popup.html?html-proxy&index=0.js'
]
}
Node.js v19.7.0
ELIFECYCLE Command failed with exit code 1.
@vimcaw Oh sorry, I ran into that error when looking into this and fixed it as well. I forgot to mention there is a change you have to make on your side as well:
This change is already in both the templates.
After adding it, anything works well, thanks!
Summary
I created a simple project from
pnpm create vite-plugin-web-extension
, and chosereact-ts
template, and the popup page doesn't work, after right-clicking to inspect the popup, the console shows an error:In
Popup.tsx:3:33
:That mean
window.$RefreshReg$
is missing.When I open
http://localhost:5173/src/popup.html
directly in the browser, anything works well.After diving deep, I found their HTML file has some differences: In
dist/src/popup.html
:In
http://localhost:5173/src/popup.html
:Obviously, this part is missing:
That's why it not working in the browser extension.
Reproduction
https://github.com/vimcaw/vite-plugin-web-extension-bug-reproduction
Environment