Skip to content

Css font imports in manifest.json #18392

Open

Description

@indykoning

Description

Currently fonts are properly added to the manifest.json but you cannot see which e.g. css/vue files require this font.
This makes automatic preloading difficult as we cannot determine which file requires the font, and IF we need to require the font for the initial page load.

https://stackblitz.com/edit/github-citlf9?file=package.json,src%2Findex.css&terminal=build

Suggested solution

I suggest adding an imports key to css files within the manifest.json just like is done with JS.
This may contain imported css (which is not relevant as everything currently gets compiled to a single css file)
and fonts required by the current css or vue file.

This way when (pre)loading the css or vue file, we know we should also (pre)load those font files

Alternative

Preloading of the fonts either needs to be done manually, or ALL fonts in the manifest.json must be loaded. Which isn't ideal since likely not all fonts are actually used on the current page.

Additional context

No response

Validations

Activity

  1. royduin commented on Oct 18, 2024

    @royduin

    An example, currently the Vite just adds all the fonts with no relation to the css file where the font was discovered in the manifest:

      "resources/css/app.css": {
        "file": "assets/app-DE7dJou-.css",
        "src": "resources/css/app.css",
        "isEntry": true
      },
      "resources/css/fonts/Nunito-Bold.woff": {
        "file": "assets/Nunito-Bold-C6dW0HYt.woff",
        "src": "resources/css/fonts/Nunito-Bold.woff"
      },

    Where Javascript files do have a reference with dynamic imports:

      "resources/js/app.js": {
        "file": "assets/app-CNshmeXx.js",
        "name": "app",
        "src": "resources/js/app.js",
        "isEntry": true,
        "dynamicImports": [
          "_Listing-DnGV7WH4.js"
         ],
      },
      "_Listing-DnGV7WH4.js": {
        "file": "assets/Listing-DnGV7WH4.js",
        "name": "Listing",
        "isDynamicEntry": true,
        "imports": [
          "resources/js/app.js"
        ]
      },

    It would be nice to have something like:

      "resources/css/app.css": {
        "file": "assets/app-DE7dJou-.css",
        "src": "resources/css/app.css",
        "isEntry": true,
        "fonts": [
            "resources/css/fonts/Nunito-Bold.woff"
        ]
      },
      "resources/css/fonts/Nunito-Bold.woff": {
        "file": "assets/Nunito-Bold-C6dW0HYt.woff",
        "src": "resources/css/fonts/Nunito-Bold.woff",
        "foundIn": [
            "resources/css/app.css"
        ]
      },

    With that we can use the new fonts array to add preload tags in the html and only get fonts from that CSS file instead of all fonts from all CSS files which aren't always used. For example multisite with a CSS per store.

  2. bolt-new-by-stackblitz commented on Oct 19, 2024

    @bolt-new-by-stackblitz
  3. added
    enhancement
    p2-nice-to-have
    and removed on Oct 19, 2024
  4. indykoning commented on Dec 3, 2024

    @indykoning
    Author

    I'm guessing somewhere here we need to have access to the bundle so we can set the bundle.imports to the files we find here

    const resolveAssetUrlsInCss = (

  5. daun commented on Jun 16, 2025

    @daun

    I've published a vite plugin that adds information about font imports to the manifest: vite-plugin-font-manifest. Working great in our use case, but would appreciate feedback if anybody wants to give it a try!

    import { defineConfig } from 'vite'
    import fontManifest from 'vite-plugin-font-manifest'
    
    export default defineConfig({
        plugins: [
            fontManifest(),
        ]
    })
    {
      "src/fonts/Inter-Regular.woff2": {
        "file": "assets/Inter-Regular-CnZ_CWUo.woff2",
        "src": "src/fonts/Inter-Regular.woff2",
        "fontFace": {
          "family": "Inter",
          "weight": "400",
          "style": "normal",
          "mime": "font/woff2",
          "css": "@font-face { /* */ }",
          "definedIn": ["src/css/display.css"]
        }
      },
      "src/css/display.css": {
        "fonts": ["src/fonts/Inter-Regular.woff2"]
      }
    }
  6. adamkoppede commented on Oct 27, 2025

    @adamkoppede
    Contributor

    Automatic <link rel=preload> for fonts via the manifest.json Backend Integration already works in Vite v7.1.12, as long as there is some JavaScript entry point that includes the CSS entry point via import statement.

    I think it should be a bug. The "assets" manifest.json property should not behave differently depending if there happens to be some other JavaScript entry point or not.

    For the specific use case where I stumbled over this issue, I was able to just combine the CSS and JavaScript entry points as they were always used together.


    In details, the following works in v7.1.12:
    // vite.config.ts
    export default defineConfig({
      base: "./",
      build: {
        manifest: "manifest.json",
        rollupOptions: {
          input: ["test.css", "test.js"],
        },
      },
    });
    // test.js
    import "./test.css"
    /* test.css */
    @font-face {
      font-family: "my-font";
      src: url("./font.woff2") format("woff2");
    }
    The resulting `manifest.json` contains the "assets" key for the CSS entry point:
    {
      "font.woff2": {
        "file": "assets/font-BRpGEcXM.woff2",
        "src": "font.woff2"
      },
      "test.css": {
        "file": "assets/test-Bz9E2gFy.js",
        "name": "test",
        "src": "test.css",
        "isEntry": true,
        "css": [
          "assets/test-BQohd8LT.css"
        ],
        "assets": [
          "assets/font-BRpGEcXM.woff2"
        ]
      },
      "test.js": {
        "file": "assets/test-C4i5TFBK.js",
        "name": "test",
        "src": "test.js",
        "isEntry": true,
        "imports": [
          "test.css"
        ]
      }
    }

    If the JavaScript entry point is missing:
    // vite.config.ts
    import { defineConfig } from "vite";
    
    export default defineConfig({
      base: "./",
      build: {
        manifest: "manifest.json",
        rollupOptions: {
          input: ["test.css"],
        },
      },
    });
    The "asset" key will not be set on the CSS entry point in the generated `manifest.json`:
    {
      "font.woff2": {
        "file": "assets/font-BRpGEcXM.woff2",
        "src": "font.woff2"
      },
      "test.css": {
        "file": "assets/test-BQohd8LT.css",
        "src": "test.css",
        "isEntry": true,
        "names": [
          "test.css"
        ]
      }
    }

    If only the JavaScript entry point is specified,
    // vite.config.ts
    import { defineConfig } from "vite";
    
    export default defineConfig({
      base: "./",
      build: {
        manifest: "manifest.json",
        rollupOptions: {
          input: ["test.js"],
        },
      },
    });
    no empty JavaScript dist file will be generated for the CSS file:
    {
      "font.woff2": {
        "file": "assets/font-BRpGEcXM.woff2",
        "src": "font.woff2"
      },
      "test.js": {
        "file": "assets/test-Bz9E2gFy.js",
        "name": "test",
        "src": "test.js",
        "isEntry": true,
        "css": [
          "assets/test-BQohd8LT.css"
        ],
        "assets": [
          "assets/font-BRpGEcXM.woff2"
        ]
      }
    }
  7. aparajitaks commented on Nov 29, 2025

    @aparajitaks

    Hi! I’d like to work on this issue.
    I’ll reproduce the problem with a minimal setup and explore how we can expose CSS → font dependencies in manifest.json.
    If maintainers have any guidance on where this metadata should live, please let me know. Thanks!

  8. RudraKo commented on Nov 29, 2025

    @RudraKo

    Hi! I’m interested in taking this issue.
    I’ll create a small repro and check how fonts are attached to CSS assets during the build. Then I’ll experiment with adding an imports-like field to CSS entries in the manifest.
    Any suggestions before I start are welcome.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfeat: buildp2-nice-to-have

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions