Vite 构建文件中的字体路径错误

发布于 2025-01-10 12:05:46 字数 2080 浏览 0 评论 0原文

我在 css 文件夹中有一个 stylesheet ,在 fonts 文件夹中有一些字体。 fontscss 文件夹位于同一目录中。

文件夹结构

|-fonts
|   |--- a.woff
|   |--  b.woff2      
|
|-css
    |--- typography.css
    

我在 typography.css 中使用了 a.woffb.woff2,如下所示:

@font-face {
  font-family: "basis-pro";
  src: url("../fonts/a.woff2") format("woff2"),
    url("../fonts/b.woff") format("woff");
  font-style: normal;
  font-weight: bold;
  font-display: swap;
}

我已经更改了 base >、vite.config.jsrollupOptions.outPutoutDir

export default defineConfig({
  base: "./",
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        assetFileNames: (assetInfo) => {
          var info = assetInfo.name.split(".");
          var extType = info[info.length - 1];
          if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
            extType = "img";
          } else if (/woff|woff2/.test(extType)) {
            extType = "fonts";
          }
          return `static/${extType}/[name]-[hash][extname]`;
        },
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
      },
    },
    outDir: "./../backend/src/main/resources/static/app/",
  },
});

构建应用程序时,除字体之外的所有资源都具有正确的路径。

fontsfonts 中生成,css 文件在 css 文件夹中生成。但是css文件中的两种字体路径错误。

@font-face {
  font-family: "basis-pro";
  src: url("./a.woff2") format("woff2"),
    url("./b.woff") format("woff");
  font-style: normal;
  font-weight: bold;
  font-display: swap;
}

它必须如下所示,因为我的资产具有与上面相同的文件夹结构(文件夹结构)。

@font-face {
  font-family: "basis-pro";
  src: url("../fonts/a.woff2") format("woff2"),
    url("../fonts/b.woff") format("woff");
  font-style: normal;
  font-weight: bold;
  font-display: swap;
}

如何解决这个问题?

I have a stylesheet in css folder and a few fonts in fonts folder. fonts and css folders are in the same directory.

folder structure

|-fonts
|   |--- a.woff
|   |--  b.woff2      
|
|-css
    |--- typography.css
    

I have used a.woff and b.woff2 in typography.css like this:

@font-face {
  font-family: "basis-pro";
  src: url("../fonts/a.woff2") format("woff2"),
    url("../fonts/b.woff") format("woff");
  font-style: normal;
  font-weight: bold;
  font-display: swap;
}

I have changed base, rollupOptions.outPut and outDir of vite.config.js.

export default defineConfig({
  base: "./",
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        assetFileNames: (assetInfo) => {
          var info = assetInfo.name.split(".");
          var extType = info[info.length - 1];
          if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
            extType = "img";
          } else if (/woff|woff2/.test(extType)) {
            extType = "fonts";
          }
          return `static/${extType}/[name]-[hash][extname]`;
        },
        chunkFileNames: "static/js/[name]-[hash].js",
        entryFileNames: "static/js/[name]-[hash].js",
      },
    },
    outDir: "./../backend/src/main/resources/static/app/",
  },
});

All of the assets have correct path excepts fonts when building the app.

fonts are generated in fonts and css files are genereted in css folder. But the two fonts in a css file has wrong path.

@font-face {
  font-family: "basis-pro";
  src: url("./a.woff2") format("woff2"),
    url("./b.woff") format("woff");
  font-style: normal;
  font-weight: bold;
  font-display: swap;
}

It must be like the following as my assets has the same folder structure as the above (folder structure).

@font-face {
  font-family: "basis-pro";
  src: url("../fonts/a.woff2") format("woff2"),
    url("../fonts/b.woff") format("woff");
  font-style: normal;
  font-weight: bold;
  font-display: swap;
}

How to fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文