wxt-dev / wxt

⚡ Next-gen Web Extension Framework
https://wxt.dev
MIT License
4.68k stars 202 forks source link

Add `entrypoints:beforeResolve` hook #1072

Open breadgrocery opened 1 month ago

breadgrocery commented 1 month ago

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 commented 1 month ago
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 commented 1 month ago

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.