Skip to content
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

Add entrypoints:beforeResolve hook #1072

Open
breadgrocery opened this issue Oct 17, 2024 · 2 comments
Open

Add entrypoints:beforeResolve hook #1072

breadgrocery opened this issue Oct 17, 2024 · 2 comments
Labels

Comments

@breadgrocery
Copy link
Contributor

breadgrocery commented Oct 17, 2024

Feature Request

Is your feature request related to a bug?

The current configuration property filterEntrypoints lacks flexibility. It requires explicitly listing all entrypoints that need to be built, which becomes cumbersome when you only need to exclude specific entrypoints in certain scenarios.

For example, I want to exclude the offscreen entrypoint when the browser is firefox.
The only solution I've found is as follows. Other approaches either fail to retrieve all entrypoint names for exclusion or fail to configure.

import { defineWxtModule } from "wxt/modules";

const entrypoints = {
  common: ["background", "options", "popup"],
  chrome: ["offscreen"],
  firefox: []
};

export default defineWxtModule(wxt => {
  const { browser } = wxt.config;
  const filterEntrypoints = [...entrypoints.common, ...entrypoints[browser]].filter(Boolean);
  wxt.config.filterEntrypoints = new Set(filterEntrypoints);
});

The skipped and exclude properties in Entrypoint have no effect because the filtering logic is applied before the entrypoints:resolved hook is triggered.

import { defineWxtModule } from "wxt/modules";

export default defineWxtModule(wxt => {
  const { browser } = wxt.config;
  wxt.hooks.hook("entrypoints:resolved", (wxt, entrypoints) => {
    if (browser === "firefox") {
      for (const entrypoint of entrypoints) {
        if (entrypoint.name === "offscreen") {
          entrypoint.skipped = true;
          entrypoint.options.exclude = ["firefox"];
        }
      }
    }
  });
});

What are the alternatives?

Introduce an entrypoints:beforeResolve hook.

Although HookResult returns void or Promise, modifications to the skipped or exclude properties can achieve the goal.

@aklinker1
Copy link
Collaborator

const entrypoints = {
  common: ["background", "options", "popup"],
  chrome: ["offscreen"],
  firefox: []
};

export default defineWxtModule(wxt => {
  const { browser } = wxt.config;
  const filterEntrypoints = [...entrypoints.common, ...entrypoints[browser]].filter(Boolean);
  wxt.config.filterEntrypoints = new Set(filterEntrypoints);
});

Interesting, I guess that's works... But it's not the intentional way of filtering entrypoints. Use include/exclude in your entrypoints instead, then you don't have to list everything. Those fields are what generates wxt.config.filterEntrypoints.

https://wxt.dev/guide/essentials/target-different-browsers.html#filtering-entrypoints

Regardless, I can add this hook, would definitely be useful!

@breadgrocery
Copy link
Contributor Author

Thanks for the tip. I am not familiar with how to define the attributes of entrypoints of type html.
This works for me.

<meta name="manifest.include" content="['chrome']" />

Regardless, I can add this hook, would definitely be useful!

Yeah. Coding offers more flexibility than configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants