Closed rbhalla closed 1 year ago
Hi @rbhalla, thanks for taking the time to submit this issue! There's a lot of good information here, but I need a little more to identify the issue. Can you provide a link to a minimal reproduction repo?
I'm guessing rollup is involved somewhere in the background?
That's right! Vite uses Rollup under the hood for production builds.
Hey @jacksteamdev!
Apologies, I should have done this in the initial post. Here is an example repo: https://github.com/rbhalla/crxjs-bug-repro/tree/pages-js-problems
This is from a vite starter with the @crxjs/vite-plugin
installed. You can compare the branch to master to see what it took to introduce.
When you do npm run dev
and access the options page (right click on the icon and press options). Everything works fine. When you do npm run build
, the javascript stops loading (it is not found). If you inspect the file at dist/src/pages/options.index.js
you'll also notice the environment variable isn't swapped out. Likely because now we're using rollup, not vite(?).
The fix to the index.js
404 is to actually change the reference in src/pages/options/index.html
to load the js from index.js
. This fixes npm run build
, but then breaks npm run dev
.
@rbhalla Awesome, thanks for the example! It works if you use type="module"
and add a slash to the front of the path.
<script src="/src/pages/options/index.js" type="module"></script>
Vite only supports absolute or relative paths (paths that start with ./
or /
). 🤷
Oh wow! Thank you so much!
Looking at the dist
folder, this also seems to mean I don't need to reference the javascript file as an accessible resource in my manifest file.
Build tool
Vite
Where do you see the problem?
Describe the bug
Sorry to bundle these errors into a single issue, let me know if you prefer them split.
I'm seeing some odd errors with web accessible resources. This is my current manifest:
I can then access these resources by going to
chrome-extension://<my-extension-id>/src/pages/options/index.html
, for example.The issues I'm seeing: 1) I can't reference javascript files that resolve correctly in dev and build In dev, the index.html files need to reference the full relative path, i.e.
src/pages/options/index.js
. In the build, index.html needs to reference just the file,index.js
. Ideally the script tag should reference the full url, but the chrome extension id isn't necessarily known here to be able to do that.I think this is because in dev, the javascript file is evaluated using
@crx/inline-script/src/pages/options/index
. In the final build, the script tag is not touched and the browser resolves the file relative to the currently loaded html file. To put it in another way:src/pages/options/index.js
resolved correctly by@crx/inline-script/src/pages/options/index
in dev, but the browser in the actual build resolves this tochrome-extension://<my-extension-id>/src/pages/options/src/pages/options/index.js
index.js
is not resolved correctly by@crx/inline-script/src/pages/options/index
in dev, but the browser resolves correctly in the final build to the addresschrome-extension://<my-extension-id>/src/pages/options/index.js
2) Environment variables aren't resolving in build I am using
import.meta.env
to accessVITE_
prefixed environment variables.I can understand why this would work in dev (since it goes through
@crx/inline-script
), I don't understand why it doesn't work in build. I am getting a warning in the console when I try and do a build:If I add
type="module"
to the script tag, I get a rollup error, even though I'm using vite. I'm guessing rollup is involved somewhere in the background?I am not sure how to begin address this error.
System Info
Severity
blocking all usage of RPCE