tadasajon
How do I use a custom font with Phoenix 1.6 and ESBuild?
I downloaded a font called “American Typewriter” and I now have a file named American_Typewriter_Regular.ttf in my priv/static/fonts directory.
But I can’t figure out how to get this file to work with my CSS.
At the top of my assets/css/app.css file I have the following lines:
@font-face {
font-family: 'American Typewriter Regular';
src: url('/fonts/American_Typewriter_Regular.ttf') format('ttf');
font-weight: normal;
font-style: normal
}
.my-logo {
font-family: 'American Typewriter Regular';
font-size: 28px;
}
What do I have to do to get ESBuild to use my font properly?
I’ve been reading ESBiuld docs and found this info:
You can
@importother CSS files and reference image and font files withurl()and esbuild will bundle everything together. Note that you will have to configure a loader for image and font files, since esbuild doesn’t have any pre-configured. Usually this is either the data URLloader or the external file loader.
So I guess my problem is that I’m not using a loader – but I don’t know exactly how to specify to ESBuild how to use a loader.
The docs tell me to set some configuration similar to this:
require('esbuild').buildSync({
entryPoints: ['app.js'],
bundle: true,
loader: { '.png': 'dataurl' },
outfile: 'out.js',
})
but I don’t know where to set this configuration.
TLDR: how do I use a font that I’ve downloaded and have saved in my fonts/ directory?
Marked As Solved
tadasajon
The solution I have come up with is to just skip all the ESBuild config stuff and manually convert the .ttf files to .woff2 and then just manually convert the .woff2 files to base64-encoded datauri strings that I manually include in my CSS file.
First, I use this tool to convert .ttf to .woff2: TTF to WOFF2 | CloudConvert
Then I use this tool to covert .woff2 to a base64-encoded datauri string: Woff2Base: Convert .woff2 to accurate Base64 css.
Then I create a /assets/css/fonts.css file and drop the @font-face rules in there, with the entire base64 string included, as follows:
@font-face {
font-family: "QuizDrill";
src: url(data:application/octet-stream;base64,d09GMgABAAAAAHXIABIAAAAiNV/D/f09uyBBZB9W+3ovLSgYkiIYUScoOOMkzfm2/7LYcNpb5clKparZy9oQnYKYSgPwoISWRgs3iJBhX/+ib/4nZm3KxhqQXEcUACUh7T/8s/398//BtL3vL8qXMex/yq3LNP8q8SQUvyVQQT4d/DHVfj/7spNOX/8fzl6irz/pbkO/gdR55B/E35yUwY=);
}
Note that I have shortened the base64 string in this example because it is thousands (or tens of thousands) of characters long.
Then at the top of my /assets/css/app.css file I have the line:
@import "fonts";
This allows me to use these local fonts bundled with my CSS.
Also Liked
BrooklinJazz
We had to remove the format for our code to work. So our CSS looks like this:
@font-face {
font-family: 'Sigmar';
src: url('../fonts/Sigmar-Regular.ttf');
}
Instead of:
@font-face {
font-family: 'Sigmar';
src: url('../fonts/Sigmar-Regular.ttf') format('ttf');
}
For some reason adding format to the url broke our font source.
I hope that helps anyone who might experience a similar issue.
outlog
thomas.fortes
I answered (almost) the same question here:
Just change the extensions or follow the links ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









