-
-
Notifications
You must be signed in to change notification settings - Fork 461
feat: Add globalName entrypoint option.
#2017
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
jviney
wants to merge
1
commit into
wxt-dev:main
Choose a base branch
from
jviney:add-global-name-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/wxt/src/core/builders/vite/plugins/iifeAnonymous.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { Plugin } from 'vite'; | ||
|
|
||
| /** | ||
| * Remove the `var name = ` prefix from the generated code so it becomes an anonymous IIFE. | ||
| * Handle both minified and non-minified cases. | ||
| */ | ||
| export function iifeAnonymous(iifeReturnValueName: string): Plugin { | ||
| return { | ||
| name: 'wxt:iife-anonymous', | ||
| generateBundle(_, bundle) { | ||
| for (const chunk of Object.values(bundle)) { | ||
| if (chunk.type === 'chunk' && chunk.isEntry) { | ||
| const namedIIFEPrefix = new RegExp( | ||
| `^var ${iifeReturnValueName}\\s*=\\s*(\\(function)`, | ||
| ); | ||
| chunk.code = chunk.code.replace(namedIIFEPrefix, '$1'); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -551,6 +551,25 @@ export interface BaseEntrypointOptions { | |
| * @default undefined | ||
| */ | ||
| exclude?: TargetBrowser[]; | ||
| /** | ||
| * The variable name for the IIFE in the JS output bundle. | ||
| * | ||
| * This option is for content scripts with world=MAIN, and unlisted scripts. | ||
| * It's relevant for scripts that are inserted into the page context where the default IIFE | ||
| * variable name may conflict with an existing variable on the target page. This applies to content | ||
| * scripts with world=MAIN, and others, such as unlisted scripts, that could be dynamically injected | ||
| * into the page with a <script> tag. | ||
| * | ||
| * Available options: | ||
| * - `true`: automatically generate a name for the IIFE based on the entrypoint name | ||
| * - `false`: Output the IIFE without a variable name, making it anonymous. This is the safest option | ||
| * to avoid conflicts with existing variables on the page. This will become the default in a future version of WXT. | ||
| * - `string`: Use the provided string as the global variable name. | ||
| * - `function`: A function that receives the entrypoint and returns a string to use as the variable name. | ||
| * | ||
| * @default true | ||
| */ | ||
| globalName?: string | boolean | ((entrypoint: Entrypoint) => string); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this needs to be moved to the content script and unlisted scripts types. I think it's fine if you duplicate this or feel free to create a shared base type. |
||
| } | ||
|
|
||
| export interface BackgroundEntrypointOptions extends BaseEntrypointOptions { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 don't think we need a full plugin to do this... If I remember correctly, vite will already output a anonymous IIFE if we don't specify a global name?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 am wrong. We need a plugin like you created!