Skip to content

Skip content script loader during build if it has no imports #996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jaskp opened this issue Mar 10, 2025 · 4 comments · May be fixed by #997
Open

Skip content script loader during build if it has no imports #996

jaskp opened this issue Mar 10, 2025 · 4 comments · May be fixed by #997
Labels
enhancement New feature or request

Comments

@jaskp
Copy link

jaskp commented Mar 10, 2025

Describe the problem

Content script loader prevents the content script from running synchronously in production even if there are no imports.

Describe the proposed solution

Title pretty much says it all, I've seen samrum/vite-plugin-web-extension do this. If the content script doesn't import anything, then I think the loader should be unnecessary in build. In dev, I believe it's still needed for HMR.

Alternatives considered

Do this manually in userland, i.e. write a CrxPlugin that finds the path to the actual content script (which the content script loader imports) and rewrite the manifest to point to it directly

Importance

would make my life easier

@jaskp jaskp added the enhancement New feature or request label Mar 10, 2025
Copy link
Contributor

⚠️ Important Notice: CRXJS is seeking new maintainers.

  • New issues and PRs may not receive immediate attention
  • A new maintenance team must establish itself by March 31, 2025 or this repository will be archived on June 1, 2025
  • Learn more about the transition

This is an automated message. Please do not reply to this comment.

@Toumash
Copy link
Collaborator

Toumash commented Mar 15, 2025

related: #946 (comment)

@jaskp
Copy link
Author

jaskp commented Mar 15, 2025

@Toumash Can you elaborate? I mean, if what you're suggesting is that implementing this feature goes against some of the following

Some things to keep in mind:

  1. Content scripts should behave the same in dev and build modes.
  2. Vite relies heavily on ESM features like import.meta. Non-module scripts would miss out on some Vite features. We'd need to document this somehow.

then I don't really think that's the case, because

  1. This feature doesn't change the behavior of the content script. If you consider timing relative to the outside world to be part of behavior, then I'd argue the content script loader makes the timing undeterministic. So the only thing that comes to mind that is true with the loader and not true without it is that the body of the content script gets executed in a microtask in the former case. However, this is an implementation detail that no one would know without reading crxjs source code. And frankly I cannot think of a good reason to want to run a content script in a randomly scheduled microtask when you have the options not to.

  2. The docs could simply say "if you don't use any ESM features in the content script, it will be executed synchronously"

Therefore I believe this feature aligns with @jacksteamdev's view. How do you see it Toumash?

@Toumash
Copy link
Collaborator

Toumash commented Mar 15, 2025

I agree you you, i just wanted to have the issue linked with similiar effor that i've already seen.
The config will behave the same way in dev and build modes so it is fine IMO👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants