@nuxt/kit-nightly 4.6.0-29776095.3d108708 → 4.6.0-29776185.65fb0aaa

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +25 -60
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -2663,31 +2663,20 @@ function addVitePlugin(pluginOrGetter, options = {}) {
2663
2663
  if (options.dev === false && nuxt.options.dev) return;
2664
2664
  if (options.build === false && nuxt.options.build) return;
2665
2665
  let needsEnvInjection = false;
2666
+ const isIsomorphic = options.server !== false && options.client !== false;
2666
2667
  nuxt.hook("vite:extend", async ({ config }) => {
2667
2668
  config.plugins ||= [];
2668
2669
  const plugin = toArray(typeof pluginOrGetter === "function" ? await pluginOrGetter() : pluginOrGetter);
2669
- if (options.server !== false && options.client !== false) {
2670
- const method = options?.prepend ? "unshift" : "push";
2671
- config.plugins[method](...plugin);
2672
- return;
2673
- }
2670
+ const method = options?.prepend ? "unshift" : "push";
2674
2671
  if (!config.environments?.ssr || !config.environments.client) {
2675
- needsEnvInjection = true;
2672
+ if (isIsomorphic) config.plugins[method](...plugin);
2673
+ else needsEnvInjection = true;
2676
2674
  return;
2677
2675
  }
2678
2676
  const environmentName = options.server === false ? "client" : "ssr";
2679
- const defaultEnforce = options?.prepend ? "pre" : "post";
2680
- const method = options?.prepend ? "unshift" : "push";
2681
- for (const [enforce, plugins] of groupByEnforce(plugin, defaultEnforce)) {
2682
- const pluginName = plugins.map((p) => p.name).join("|");
2683
- config.plugins[method]({
2684
- name: `${pluginName}:wrapper`,
2685
- enforce,
2686
- applyToEnvironment(environment) {
2687
- if (environment.name === environmentName) return resolveNestedPlugins(plugins, config, environment, nuxt);
2688
- }
2689
- });
2690
- }
2677
+ const isAllowed = isIsomorphic ? (name) => name === "client" || name === "ssr" : (name) => name === environmentName;
2678
+ const defaultEnforce = isIsomorphic ? options?.prepend ? "pre" : void 0 : options?.prepend ? "pre" : "post";
2679
+ config.plugins[method](...plugin.map((p) => scopeToEnvironments(p, defaultEnforce, isAllowed)));
2691
2680
  });
2692
2681
  nuxt.hook("vite:extendConfig", async (config, env) => {
2693
2682
  if (!needsEnvInjection) return;
@@ -2697,53 +2686,29 @@ function addVitePlugin(pluginOrGetter, options = {}) {
2697
2686
  if (env.isServer && options.client === false) config.plugins[method](...plugin);
2698
2687
  });
2699
2688
  }
2700
- function groupByEnforce(plugins, defaultEnforce) {
2701
- const groups = /* @__PURE__ */ new Map();
2702
- for (const plugin of plugins) {
2703
- const enforce = plugin.enforce ?? defaultEnforce;
2704
- const group = groups.get(enforce);
2705
- if (group) group.push(plugin);
2706
- else groups.set(enforce, [plugin]);
2707
- }
2708
- return groups;
2709
- }
2710
- /**
2711
- * Vite only honours `apply` and `applyToEnvironment` for plugins it finds in the
2712
- * top-level plugin array, so plugins nested behind a wrapper have to be resolved
2713
- * by hand or (for example) dev-only plugins would leak into production builds.
2714
- */
2715
- async function resolveNestedPlugins(plugins, config, environment, nuxt) {
2716
- const configEnv = resolveConfigEnv(environment, config, nuxt);
2717
- const resolved = [];
2718
- for (const plugin of plugins) {
2719
- const { apply, applyToEnvironment } = plugin;
2720
- if (apply && (typeof apply === "function" ? !apply(config, configEnv) : apply !== configEnv.command)) continue;
2721
- const applied = applyToEnvironment ? await applyToEnvironment(environment) : true;
2722
- if (applied === true) {
2723
- resolved.push(plugin);
2724
- continue;
2725
- }
2726
- resolved.push(...await flattenPlugins(applied));
2727
- }
2728
- return resolved;
2729
- }
2730
2689
  /**
2731
- * `getTopLevelConfig` is only available from Vite 6, and kit is used with older
2732
- * versions of nuxt (and therefore vite), so fall back to what we can infer.
2690
+ * `addVitePlugin` has always scoped plugins to the app builds. Builders can contribute
2691
+ * further environments (Nitro adds `nitro`, and one per service), so a plugin registered
2692
+ * here has to opt out of any environment it wasn't registered for. The check runs per
2693
+ * environment as Vite resolves them, so environments added after the plugin was
2694
+ * registered are still excluded.
2695
+ *
2696
+ * The plugin is returned as a top-level plugin rather than nested behind a wrapper
2697
+ * so that Vite still applies `apply`, sorts by `enforce`, calls server-level hooks
2698
+ * like `configureServer`, and exposes any extra properties the plugin declares
2699
+ * (`api`, and whatever tooling scanning `config.plugins` looks for) as usual.
2733
2700
  */
2734
- function resolveConfigEnv(environment, config, nuxt) {
2735
- const topLevelConfig = environment.getTopLevelConfig?.() ?? environment.config;
2701
+ function scopeToEnvironments(plugin, defaultEnforce, isAllowed) {
2702
+ const { applyToEnvironment } = plugin;
2736
2703
  return {
2737
- command: topLevelConfig?.command ?? (nuxt.options.dev ? "serve" : "build"),
2738
- mode: topLevelConfig?.mode ?? config.mode ?? nuxt.options.vite?.mode ?? (nuxt.options.dev ? "development" : "production")
2704
+ ...plugin,
2705
+ enforce: plugin.enforce ?? defaultEnforce,
2706
+ applyToEnvironment(environment) {
2707
+ if (!isAllowed(environment.name)) return false;
2708
+ return applyToEnvironment ? applyToEnvironment(environment) : true;
2709
+ }
2739
2710
  };
2740
2711
  }
2741
- async function flattenPlugins(option) {
2742
- const resolved = await option;
2743
- if (!resolved) return [];
2744
- if (Array.isArray(resolved)) return (await Promise.all(resolved.map(flattenPlugins))).flat();
2745
- return [resolved];
2746
- }
2747
2712
  function addBuildPlugin(pluginFactory, options) {
2748
2713
  if (pluginFactory.vite) addVitePlugin(pluginFactory.vite, options);
2749
2714
  if (pluginFactory.webpack) addWebpackPlugin(pluginFactory.webpack, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/kit-nightly",
3
- "version": "4.6.0-29776095.3d108708",
3
+ "version": "4.6.0-29776185.65fb0aaa",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/nuxt.git",
@@ -69,7 +69,7 @@
69
69
  }
70
70
  },
71
71
  "devDependencies": {
72
- "@nuxt/schema": "npm:@nuxt/schema-nightly@4.6.0-29776095.3d108708",
72
+ "@nuxt/schema": "npm:@nuxt/schema-nightly@4.6.0-29776185.65fb0aaa",
73
73
  "c12": "4.0.0-beta.5",
74
74
  "chokidar": "^5.0.0",
75
75
  "dotenv": "^17.4.2",