tadasajon
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
thomas.fortes
I answered (almost) the same question here:
Just change the extensions or follow the links
tadasajon
Wow, thanks much. Following you suggestion I have adjusted my
config/dev.exsfile and it now reads as follows:I have also adjusted my
mix.exsfile and it now shows the following:But I’m still not getting the font to work on my website.\
Could my problem have something to do with the fact that I’m telling ESBuild to use a file loader, but in my
app.cssfile I have specified a URL?Also, it is not clear to me how the ESBuild file loader is supposed to know where to find the file. How does it know whether to look in
priv/static/assets/fontsvsassets/css/fontsvsassets/fonts, etc?thomas.fortes
Not really, what this loader does is copy the file to the output folder, if you used the dataURL it would convert the font to base64 and paste it at the start of the generated css.
Can you access the font file going straight to its url in the browser?
What exactly the error says?
Could not resolveorNo loader is configured for ".ttf" files, if it isCould not resolveit means that the loader isn’t finding the file, in my referenced post the person put the font in priv and used a relative path like:background-image: url('../../priv/static/images/background.jpg');tadasajon
Yes, if I visit
http://localhost:4000/fonts/American_Typewriter_Regular.ttfin my browser, then I get a popup saying “Do you want to allow downloads on “localhost”?” – which suggests to me that the browser can find the file and is prompting me to download it because displaying it is not an option.I’m not getting any error, though – when I run
mix phx.serverit just runs normally. I can use my website as expected. The only problem is that the font is not showing up so I don’t get the cool American Typewriter look.thomas.fortes
Can you try @import instead of @font-face?
tadasajon
ok, I’ve adjusted my
assets/css/app.cssfile and it now has the following:This causes my VSCode editor to give me some red underlines and warnings, though. The font is still not showing up.
I have also tried different paths, such as:
I have the
.ttffile stored in two places:priv/static/fonts/American_Typewriter_Regular.ttfassets/fonts/American_Typewriter_Regular.ttfthomas.fortes
Forget about the @import, I was thinking of other stuff…
My last guess is to use
../../priv/static/fonts/American_Typewriter_Regular.ttfand hope for the best…tadasajon
Ok, so I’ve made a repo here: GitHub - tadasajon/use_local_font: use a local font in Phoenix 1.6.2 using ESBuild · GitHub
This was generated with
mix phx.new use_local_font --no-ectoand it tries to do just one thing: use a local font.I’ll obviously be delighted to receive a pull request from anyone.
tadasajon
Ok, well, the font works fine for me in that repo that I just made. I used the following settings:
In my
assets/css/app.css:in my
lib/use_local_font_web/templates/layout/root.html.heexfile:in my
mix.exsfile:in my
config/dev.exsfile:tadasajon
So I guess my problem has something to do with the way I have tailwind configured in my app, since it isn’t working in the setup I have in my real app, but it is working in this toy setup.
Wow, what a time suck this has been for me.