-
-
Notifications
You must be signed in to change notification settings - Fork 412
feat: Add iifeName option #1897
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
f084548 to
106d137
Compare
106d137 to
97a4fb9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this idea, I am willing to merge this as-is.
Before that, I want to run an alternative by you: would it make more sense to add this to defineContentScript (as an option like cssInjectionMode, matches, etc) instead of wxt.config.ts?
export default defineContentScript({
iffeName: "example",
// OR `globalName: string | boolean`, letting people disable the iffe name in the output as well as give it a different name.
main() {
// ...
}
})In this version, it would default to true and the name would be generated based on the content script name, but in the next major version would could change the default to false so a name isn't even generated in the first place.
Great - thank you. Yes that idea could work too, and does offer more flexibility. My only hesitation is that on a team it's easy for someone to forget to add this option, so having a way to globally ensure the IIFE name is unique (or unnamed) would still be valuable. Our extension inserts world=MAIN content scripts and we need a strong guarantee that we're not going to interfere with a variable on the site. |
|
If we go with that approach, forgetting to override the name would be resolved in the next major version by changing the default value to I don't really want to introduce a feature now that would only be removed in the next major version. I know that sucks for you now, but an alternative would be to patch WXT and add a prefix to all IIFE global names by default. |
Sounds fine to me - we can add something to our build tests that ensure the value has been set to false. |
|
You could validate it via a WXT module: // modules/iife-global-name-validation.ts
import { defineWxtModule } from 'wxt/modules';
export default defineWxtModules({
name: "iife-global-name-validation",
setup(wxt) {
wxt.hook('entrypoints:resolved', (entrypoints) => {
for (const entrypoint of entrpoints) {
// I believe entrypoint.options contains the fully resolved options, with defaults applied.
if (entrypoint.type === "content" && entrypoint.options.globalName !== false) {
throw Error("ERROR: All content scripts must have `globalName: false` set in the options")
// or log a warning with wxt.logger.warn,
// or look at wxt.config.command and decide to throw an error or warn based on the command being ran,
// or build a list of invalid content scripts and do either of the above for all content scripts that are invalid
}
}
}
}
})It would run on |
We decided to change where the option was defined.
Great - that's even better. Shall I do a PR to add this new option? |
Overview
The IIFE name is not currently configurable, and can be important when inserting content scripts with world=MAIN to ensure there is no conflict with the page where the script is being inserted.
Manual Testing
When specifying an
iifeNameoption in the config, the return value of this function is used as the IIFE name in the built JS files.