Skip to content

Generated script constants #2020

Open

Description

@nickbar01234

Feature Request

A common pattern is to define a content script and dynamically injecting script at runtime via

// script.ts
export default defineUnlistedScript(() => { console.log("Hello" });

// background.ts
browser.scripting.executeScript({ file: ["/script.js"] })

If the filename were to change, the user would also need to update the path used in executeScript. Curious on whether it's feasible to generate a constant enum specifying the filename. The API can look something like

export default defineUnlistedScript({
  name: "MyDynamicScript"
  main: () => { ... }
}

// background.ts
browser.scripting.executeScript({ file: [Scripts["MyDynamicScript"]] });

the value would map to the actual path.

Is your feature request related to a bug?

N/A

Activity

  1. aklinker1 commented on Feb 11, 2026

    @aklinker1
    Member

    I like this! We don't need a separate name option in defineUnlistedScript or other entrypoints definition functions, WXT already has a name it assigned to entrypoints internally based on the file name.

    If someone wants to contribute this feature, that would be great! This can be accomplished via an "built-in WXT module" that generates a file in the `.wxt directory and adds an import alias and adds the constants to auto-imports as well.

    Otherwise I'll prioritize working on it myself after v1.0 is out.

  2. added
    good first issue
    on Feb 11, 2026
  3. added this to the v1.1 milestone on Feb 11, 2026
  4. nickbar01234 commented on Feb 11, 2026

    @nickbar01234
    ContributorAuthor

    nice! I wasn't sure if it was accomplishable at first. Happy to take a stab at it!

  5. aklinker1 commented on Feb 11, 2026

    @aklinker1
    Member

    Sounds good, @ me if you have a question or get stuck. I can provide more direction as well if you don't know where to start.

  6. nickbar01234 commented on Feb 15, 2026

    @nickbar01234
    ContributorAuthor

    Hey @aklinker1, started taking a look at this.

    We don't need a separate name option in defineUnlistedScript or other entrypoints definition functions, WXT already has a name it assigned to entrypoints internally based on the file name.

    Can you clarify why the internal naming is helpful in this case? If I'm reading the code correctly, the internal name is dependent on the file name, so even if we add auto-import, subsequent file name changes would still break the build.

    export interface BaseEntrypoint {
      /**
       * The entrypoint's name. This is the filename or dirname without the type suffix.
       *
       * Examples:
       * - `popup.html` → `popup`
       * - `options/index.html` → `options`
       * - `named.sandbox.html` → `named`
       * - `named.sandbox/index.html` → `named`
       * - `sandbox.html` → `sandbox`
       * - `sandbox/index.html` → `sandbox`
       * - `overlay.content.ts` → `overlay`
       * - `overlay.content/index.ts` → `overlay`
       *
       * The name is used when generating an output file:
       * `<entrypoint.outputDir>/<entrypoint.name>.<ext>`
       */
        [...]
    }
  7. aklinker1 commented on Feb 19, 2026

    @aklinker1
    Member

    I mention the internal name because I'm hesitant about introducing yet another "name" concept for entrypoints. In another PR, we're adding a way to customize the name for the IIFE global in content scripts. So this would make 3 different ways to "name" an entrypoint.

    If we use the internal name, projects will at least get type-safety: when renaming an entrypoint, you'll get a type error because the variable no longer exists. One find and replace, and you're done.

    I recognize your point though, that adding an option could make it easier to rename a file without having to update the variable references... But wouldn't you want to rename the variable to match the new name anyways? How often would this happen?

    Or heck, just make your own variables today using the PublicPath type - are you aware of this type? WXT already generates a union of string literals for all entrypoints and other predictable paths:

    // This example might be wrong, I'm on mobile
    import { Browser } from 'wxt/browser'
    
    const INJECT_SCRIPT_PATH: Browser.PublicPath = '/inection-script.js';

    Thoughts? I don't want to shoot down this feature completely, but thinking about this more, I'm not convinced this feature should go in the core package since as there are other ways to be type-safe today.

  8. nickbar01234 commented on Feb 20, 2026

    @nickbar01234
    ContributorAuthor

    yep, I'm aware of the type safety from PublicPath, which is how I figured out the naming needed to change. I understand your concern with multiple naming concepts; I think there are merits to generating variables using internal names, which we can use to add extra typesafety to browser.scripting.executeScript (not sure if there are other places where this can be helpful)?

    One problem I see is if you start with injectScript("A.js") and want to update to B.js, then the build will fail until you comment out the injection and rebuild. Maybe there is a more first-class typescript way to solve this problem?

  9. aklinker1 commented on Feb 20, 2026

    @aklinker1
    Member

    Maybe there is a more first-class typescript way to solve this problem?

    I'm not sure what you mean by this. If there's already a type... what's more first-class than that?

  10. nickbar01234 commented on Feb 20, 2026

    @nickbar01234
    ContributorAuthor

    Maybe there is a more first-class typescript way to solve this problem?

    I'm not sure what you mean by this. If there's already a type... what's more first-class than that?

    Sorry, I just meant maybe there's a way to build without commenting out when filenames are changed. Does that make sense?

  11. aklinker1 commented on Feb 21, 2026

    @aklinker1
    Member

    How about this, why don't we create an official WXT module for this? Say @wxt-dev/constants or @wxt-dev/path-constants? Because there is value in defining constants that don't break when changed.

    I'm fine with adding as much config as you want to it at that point as well, since it wouldn't be in the core package. We already have an example of this in the @wxt-dev/auto-icons package. Similar convo happened around it's features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

contribution welcomegood first issue

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions