@nuxt/vite-builder-nightly 4.6.0-29776167.796cdae2 → 4.6.0-29778602.bfddf2f0

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 +106 -32
  2. package/package.json +4 -4
package/dist/index.mjs CHANGED
@@ -1795,7 +1795,8 @@ function wrapStringGenerateScopedName(pattern, hashPrefix) {
1795
1795
  function SSRStylesPlugin(nuxt) {
1796
1796
  if (nuxt.options.dev) return;
1797
1797
  const chunksWithInlinedCSS = /* @__PURE__ */ new Set();
1798
- const cssSourcesByChunkSrc = /* @__PURE__ */ new Map();
1798
+ const clientImporters = /* @__PURE__ */ new Map();
1799
+ const cssSourcesByCSSFile = /* @__PURE__ */ new Map();
1799
1800
  const clientCSSMap = {};
1800
1801
  const stripQuery = (id) => id.replace(QUERY_RE, "");
1801
1802
  const withInlineQuery = (id) => {
@@ -1803,38 +1804,101 @@ function SSRStylesPlugin(nuxt) {
1803
1804
  if (match) return id.slice(0, match.index) + match[1] + "inline&used&" + id.slice(match.index + 1);
1804
1805
  return id + (id.includes("?") ? "&" : "?") + "inline&used";
1805
1806
  };
1806
- const inlinedCSSModuleIds = /* @__PURE__ */ new Set();
1807
+ const inlinedCSSConsumers = /* @__PURE__ */ new Map();
1808
+ const isInlinedForEveryImporter = (cssId) => {
1809
+ const consumers = inlinedCSSConsumers.get(cssId);
1810
+ if (!consumers) return false;
1811
+ const seen = /* @__PURE__ */ new Set();
1812
+ const queue = [cssId];
1813
+ while (queue.length) {
1814
+ const importer = queue.shift();
1815
+ if (seen.has(importer)) continue;
1816
+ seen.add(importer);
1817
+ if (consumers.has(relativeToSrcDir(importer))) continue;
1818
+ const parents = clientImporters.get(importer);
1819
+ if (!parents?.size) return false;
1820
+ queue.push(...parents);
1821
+ }
1822
+ return true;
1823
+ };
1824
+ const isDroppableCSSFile = (file) => {
1825
+ const sources = cssSourcesByCSSFile.get(basename(file));
1826
+ if (!sources?.size) return false;
1827
+ for (const cssId of sources) if (!isInlinedForEveryImporter(cssId)) return false;
1828
+ return true;
1829
+ };
1830
+ const inlinedCSSConditions = /* @__PURE__ */ new Map();
1831
+ const serializeInlinedCSSConditions = () => JSON.stringify(Object.fromEntries(inlinedCSSConditions));
1832
+ /**
1833
+ * Decide how a droppable CSS file should be handled, recording a render-time
1834
+ * condition when some of its sources are only inlined for components that may
1835
+ * not be server-rendered on a given request (for example a component used
1836
+ * inside `<ClientOnly>` on one route and server-rendered on another).
1837
+ *
1838
+ * Returns `true` when the link can be removed from the manifest outright,
1839
+ * i.e. every CSS source is inlined for an entry module, which is always in
1840
+ * `ssrContext.modules`.
1841
+ */
1842
+ const dropCSSFile = (file, entryIds) => {
1843
+ const cssFile = basename(file);
1844
+ const sources = cssSourcesByCSSFile.get(cssFile);
1845
+ const conditions = [];
1846
+ for (const cssId of sources) {
1847
+ const consumers = inlinedCSSConsumers.get(cssId);
1848
+ if (!consumers?.size) continue;
1849
+ let alwaysRendered = false;
1850
+ for (const consumer of consumers) if (entryIds.has(consumer)) {
1851
+ alwaysRendered = true;
1852
+ break;
1853
+ }
1854
+ if (!alwaysRendered) conditions.push(Array.from(consumers));
1855
+ }
1856
+ if (!conditions.length) return true;
1857
+ inlinedCSSConditions.set(cssFile, conditions);
1858
+ return false;
1859
+ };
1860
+ /**
1861
+ * Record a render-time condition for CSS files attributed to a single
1862
+ * component, used for rolldown-generated chunks whose CSS is matched to the
1863
+ * component by filename rather than through the module graph.
1864
+ */
1865
+ const dropComponentCSSFile = (file, componentId, entryIds) => {
1866
+ if (entryIds.has(componentId)) return true;
1867
+ inlinedCSSConditions.set(basename(file), [[componentId]]);
1868
+ return false;
1869
+ };
1807
1870
  nuxt.hook("build:manifest", (manifest) => {
1808
1871
  const entryIds = /* @__PURE__ */ new Set();
1809
- for (const { cssIds, files, inBundle } of Object.values(cssMap)) {
1810
- if (!cssIds || !inBundle || !files.length) continue;
1811
- for (const cssId of cssIds) inlinedCSSModuleIds.add(cssId);
1872
+ for (const [id, { cssIds, files, inBundle }] of Object.entries(cssMap)) {
1873
+ if (!inBundle || !files.length) continue;
1874
+ chunksWithInlinedCSS.add(id);
1875
+ if (!cssIds) continue;
1876
+ for (const cssId of cssIds) {
1877
+ const consumers = inlinedCSSConsumers.get(cssId) ?? /* @__PURE__ */ new Set();
1878
+ inlinedCSSConsumers.set(cssId, consumers);
1879
+ consumers.add(id);
1880
+ }
1881
+ }
1882
+ for (const id of chunksWithInlinedCSS) {
1883
+ const chunk = manifest[id];
1884
+ if (chunk?.isEntry && chunk.src) entryIds.add(chunk.src);
1812
1885
  }
1813
1886
  for (const id of chunksWithInlinedCSS) {
1814
1887
  const chunk = manifest[id];
1815
1888
  if (!chunk) continue;
1816
- if (chunk.isEntry && chunk.src) entryIds.add(chunk.src);
1817
- else chunk.css &&= [];
1818
1889
  if (chunk.imports && chunk.src) {
1819
1890
  const componentBaseName = filename(chunk.src);
1820
1891
  for (const imp of chunk.imports) {
1821
1892
  const imported = manifest[imp];
1822
1893
  if (imported?.css?.length && !imported.isEntry && !imported.src) {
1823
- if (imported.css.every((css) => css.startsWith(componentBaseName + "."))) imported.css = [];
1894
+ if (imported.css.every((css) => css.startsWith(componentBaseName + "."))) imported.css = imported.css.filter((file) => !dropComponentCSSFile(file, id, entryIds));
1824
1895
  }
1825
1896
  }
1826
1897
  }
1827
1898
  }
1828
1899
  for (const chunk of Object.values(manifest)) {
1829
- if (!chunk.css?.length || !chunk.src) continue;
1830
- const cssSources = cssSourcesByChunkSrc.get(chunk.src);
1831
- if (!cssSources?.size) continue;
1832
- let allInlined = true;
1833
- for (const cssId of cssSources) if (!inlinedCSSModuleIds.has(cssId)) {
1834
- allInlined = false;
1835
- break;
1836
- }
1837
- if (allInlined) chunk.css = [];
1900
+ if (!chunk.css?.length) continue;
1901
+ chunk.css = chunk.css.filter((file) => !(isDroppableCSSFile(file) && dropCSSFile(file, entryIds)));
1838
1902
  }
1839
1903
  setBuildOutput("entryIds", () => `export default ${JSON.stringify(Array.from(entryIds))}`, nuxt);
1840
1904
  });
@@ -1882,7 +1946,7 @@ function SSRStylesPlugin(nuxt) {
1882
1946
  buildStart() {
1883
1947
  if (this.environment.name === "ssr") {
1884
1948
  const stylesPath = resolve(this.environment.config.build.outDir, "styles.mjs");
1885
- setBuildOutput("ssrStyles", () => `export { default } from ${JSON.stringify(stylesPath)}`, nuxt);
1949
+ setBuildOutput("ssrStyles", () => [`export { default } from ${JSON.stringify(stylesPath)}`, `export const inlinedCSS = ${serializeInlinedCSSConditions()}`].join("\n"), nuxt);
1886
1950
  }
1887
1951
  },
1888
1952
  resolveId: {
@@ -1904,8 +1968,30 @@ function SSRStylesPlugin(nuxt) {
1904
1968
  };
1905
1969
  }
1906
1970
  },
1907
- generateBundle(outputOptions) {
1908
- if (environment.name === "client") return;
1971
+ generateBundle(outputOptions, bundle) {
1972
+ if (environment.name === "client") {
1973
+ for (const chunk of Object.values(bundle)) {
1974
+ if (chunk.type !== "chunk") continue;
1975
+ for (const moduleId of chunk.moduleIds) {
1976
+ const id = stripQuery(moduleId);
1977
+ const importers = clientImporters.get(id) ?? /* @__PURE__ */ new Set();
1978
+ clientImporters.set(id, importers);
1979
+ for (const importer of this.getModuleInfo(moduleId)?.importers ?? []) {
1980
+ const importerId = stripQuery(importer);
1981
+ if (importerId !== id) importers.add(importerId);
1982
+ }
1983
+ }
1984
+ const cssSources = /* @__PURE__ */ new Set();
1985
+ for (const moduleId of chunk.moduleIds) if (isCSS(moduleId)) cssSources.add(stripQuery(moduleId));
1986
+ if (cssSources.size) for (const file of chunk.viteMetadata?.importedCss ?? []) {
1987
+ const cssFile = basename(file);
1988
+ const sources = cssSourcesByCSSFile.get(cssFile) ?? /* @__PURE__ */ new Set();
1989
+ cssSourcesByCSSFile.set(cssFile, sources);
1990
+ for (const cssId of cssSources) sources.add(cssId);
1991
+ }
1992
+ }
1993
+ return;
1994
+ }
1909
1995
  const emitted = {};
1910
1996
  const usedNames = /* @__PURE__ */ new Set();
1911
1997
  for (const [file, { files, inBundle }] of Object.entries(cssMap)) {
@@ -1952,22 +2038,10 @@ function SSRStylesPlugin(nuxt) {
1952
2038
  renderChunk(_code, chunk) {
1953
2039
  const isEntry = chunk.facadeModuleId === entry;
1954
2040
  if (isEntry) clientCSSMap[chunk.facadeModuleId] ||= /* @__PURE__ */ new Set();
1955
- let chunkCSSSources;
1956
- if (environment.name === "client" && chunk.facadeModuleId) {
1957
- const chunkSrc = relativeToSrcDir(chunk.facadeModuleId);
1958
- if (chunkSrc) {
1959
- chunkCSSSources = cssSourcesByChunkSrc.get(chunkSrc);
1960
- if (!chunkCSSSources) {
1961
- chunkCSSSources = /* @__PURE__ */ new Set();
1962
- cssSourcesByChunkSrc.set(chunkSrc, chunkCSSSources);
1963
- }
1964
- }
1965
- }
1966
2041
  for (const moduleId of [chunk.facadeModuleId, ...chunk.moduleIds].filter(Boolean)) {
1967
2042
  if (environment.name === "client") {
1968
2043
  const moduleMap = clientCSSMap[moduleId] ||= /* @__PURE__ */ new Set();
1969
2044
  if (isCSS(moduleId)) {
1970
- chunkCSSSources?.add(stripQuery(moduleId));
1971
2045
  if (isVue(moduleId)) {
1972
2046
  moduleMap.add(moduleId);
1973
2047
  const parent = moduleId.replace(/\?.+$/, "");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/vite-builder-nightly",
3
- "version": "4.6.0-29776167.796cdae2",
3
+ "version": "4.6.0-29778602.bfddf2f0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/nuxt.git",
@@ -24,7 +24,7 @@
24
24
  "dist"
25
25
  ],
26
26
  "dependencies": {
27
- "@nuxt/kit": "npm:@nuxt/kit-nightly@4.6.0-29776167.796cdae2",
27
+ "@nuxt/kit": "npm:@nuxt/kit-nightly@4.6.0-29778602.bfddf2f0",
28
28
  "@vitejs/plugin-vue": "^6.0.8",
29
29
  "@vitejs/plugin-vue-jsx": "^5.1.6",
30
30
  "autoprefixer": "^10.5.4",
@@ -55,7 +55,7 @@
55
55
  "devDependencies": {
56
56
  "@babel/plugin-proposal-decorators": "8.0.2",
57
57
  "@babel/plugin-syntax-jsx": "8.0.1",
58
- "@nuxt/schema": "npm:@nuxt/schema-nightly@4.6.0-29776167.796cdae2",
58
+ "@nuxt/schema": "npm:@nuxt/schema-nightly@4.6.0-29778602.bfddf2f0",
59
59
  "h3": "1.15.11",
60
60
  "h3-next": "npm:h3@2.0.1-rc.26",
61
61
  "nitropack": "2.13.4",
@@ -68,7 +68,7 @@
68
68
  "peerDependencies": {
69
69
  "@babel/plugin-proposal-decorators": "^7.25.0 || ^8.0.0",
70
70
  "@babel/plugin-syntax-jsx": "^7.25.0 || ^8.0.0",
71
- "nuxt": "npm:nuxt-nightly@4.6.0-29776167.796cdae2",
71
+ "nuxt": "npm:nuxt-nightly@4.6.0-29778602.bfddf2f0",
72
72
  "rolldown": "^1.0.0",
73
73
  "rollup-plugin-visualizer": "^6.0.0 || ^7.0.1",
74
74
  "vue": "^3.3.4"