Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when "url" is a function in vite, the path string become a base64 code. I means it use 'inline', but I want 'rebase'. #183

Open
l-outsider opened this issue Nov 15, 2024 · 1 comment

Comments

@l-outsider
Copy link

I follow this doc in vite: https://vite.dev/config/shared-options.html#css-postcss

And I want to change the font file path in css after build.

before:

@font-face {
  font-family: iconfont;
  src:
    url("./assets/fonts/iconfont.woff2?t=1722856322648") format("woff2"),
    url("./assets/fonts/iconfont.woff?t=1722856322648") format("woff"),
    url("./assets/fonts/iconfont.ttf?t=1722856322648") format("truetype");
}

after, I want this

@font-face {
  font-family: iconfont;
  src:
    url("../assets/fonts/iconfont.woff2?t=1722856322648") format("woff2"),
    url("../assets/fonts/iconfont.woff?t=1722856322648") format("woff"),
    url("../assets/fonts/iconfont.ttf?t=1722856322648") format("truetype");
}

this is my config:

css: {
    postcss: {
      plugins: [
        postcssUrl({
          url: (asset) => {
            if (!/\.(woff|woff2|ttf)$/.test(asset.pathname || '')) {
              return asset.url;
            }
            // ensure paths are correct in npm publish
            return `../assets/fonts/${asset.url.split('/').pop()}`;
          },
        }),
      ],
    },

But the result is

@font-face{font-family:iconfont;src:url(data:font/woff2;base64,d09GMgABAAAAAAQw

If I understand correctly, it uses inline mode internally, and I want to use a relative path string, so how can I make it use rebase mode?

By the way, I did something similar in rollup, and I did find that it defaulted to rebase mode, which was different from the result of vite.

@l-outsider
Copy link
Author

Or should I not do this in the first place? Is there any other way I can achieve my idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant