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 @import other CSS files and reference image and font files with url() 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.
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.css file 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/fonts vs assets/css/fonts vs assets/fonts, etc?
Could my problem have something to do with the fact that I’m telling ESBuild to use a file loader, but in my app.css file I have specified a URL?
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 resolveor No loader is configured for ".ttf" files, if it is Could not resolve it 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');
Yes, if I visit http://localhost:4000/fonts/American_Typewriter_Regular.ttf in 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.server it 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.
in my lib/use_local_font_web/templates/layout/root.html.heex file:
<div style="margin:16px; padding:16px;outline:4px solid red;font-size:28px;text-align:center;background-color:lightyellow;">
<span>
This text should be in the default font. Q
</span>
<br />
<span class="use-local-font">
This text should be in American Typewriter Regular font. Q
</span>
</div>
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.
It turns out I don’t need to adjust my mix.exs file or my config/dev.exs file for the local font to work in my toy example app. I can just drop the font in my fonts folder, reference it in my CSS file, and I’m done. So I can get rid of the stuff having to do with the file loader for .ttf files.
Ok, so it turns out that I can entirely delete the priv/static/font directory and the assets/font directory and my toy app still has the American Typewriter font showing up. Maybe it is cached somewhere?
It seems like in my real app there is nothing I can do to get the American Typewriter font to work, and in my toy app there is nothing I can do to get it to stop working.
ok, so now I’ve commented out the @font-face rule in my CSS file and I’ve also deleted the assets/fonts directory and the static/fonts directory, which contained the American_Typewriter_Regular.ttf file.
But the font is showing up fine in my real app (not my toy app).
Before there was nothing I could do to make it work and now there is nothing I can do to break it. So I guess the font got cached somewhere.
But I want to make sure that the font works even if someone’s computer has never seen or heard of this font before, which is why I want to have a copy of the font bundled in my CSS. This appears to be hard to test because it is difficult to tell where a font is coming from.
I guess the lesson from all this is that playing around with fonts is just a stupid waste of time because there is no way to tell what is going on.
I can’t help with the esbuild setup but from this it sounds like you installed the font on your computer. I’d recommend uninstalling until you verify that you have your site working.
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.
I recently converted to esbuild (after making an npm update that sent me back down the error rabbit hole), and I was getting these “mark the file as external” errors from esbuild and wondering how I mark the files as external, very excited to try this.