Integrate IBM Plex with TailwindCSS and autogenerate font rules
How to install IBM Plex as an NPM package that Renovate or Dependabot can update, and generate its @font-face rules for TailwindCSS with a script instead of copying them by hand.
IBM Plex is available as an NPM package and updated regularly. I want to automate these updates so I always have the latest font version. Manual steps (like adjusting the @font-face rules) would be a deal breaker for that.
The easy way is to download the fonts you need, copy the stylesheet into your project and adjust the font paths. Or you use google-webfonts-helper to build a package with everything in it. Both leave you updating the files by hand.
Installing the package
First, add the @ibm/plex NPM package to your project.
$ npm install @ibm/plex
The font files need to be somewhere the webserver can serve them. copy-files-from-to copies them there automatically after the packages are installed. I’m running a Phoenix project (adjust the folder structure to your liking).
{
"private": true,
"dependencies": {},
"devDependencies": {
"@ibm/plex": "^6.4.0",
"copy-files-from-to": "^3.9.1",
...
},
"scripts": {
"install": "copy-files-from-to",
...
},
"copyFiles": [
{
"from": "node_modules/@ibm/plex/IBM-Plex-{Mono,Sans,Serif}/**/*.woff2",
"to": "priv/static/fonts/"
}
]
}
The glob pattern only copies the woff2 fonts of the mono, sans and serif variants. (There are a lot of other variants I’m not interested in.) That keeps the release as clean and small as possible.
Generate the font-face rules
If you’re using Sass, you’re in luck. IBM Plex generates its @font-face rules with Sass. Just import all the variants you want to support and set $font-prefix before that.
I’m using TailwindCSS via the official CLI, so there are some extra steps. After going through a few options, I decided to use Sass as well, but only for a single fonts.scss file:
$font-prefix: '/fonts';
@import '../../node_modules/@ibm/plex/scss/mono/index';
@import '../../node_modules/@ibm/plex/scss/sans/index';
@import '../../node_modules/@ibm/plex/scss/serif/index';
Here I can @import exactly what I need, and $font-prefix adjusts all paths in the @font-face rules. The file is transpiled to a CSS file (added to .gitignore) right after the NPM packages are installed:
{
"private": true,
"dependencies": {},
"devDependencies": {
"@ibm/plex": "^6.4.0",
"copy-files-from-to": "^3.9.1",
"npm-run-all2": "6.2.0",
"sass": "^1.70.0",
...
},
"scripts": {
"install": "run-s install:*",
"install:copy-files": "copy-files-from-to",
"install:ibm-plex-css": "sass assets/scss/fonts.scss assets/css/fonts.css --no-source-map",
...
},
"copyFiles": [
{
"from": "node_modules/@ibm/plex/IBM-Plex-{Mono,Sans,Serif}/**/*.woff2",
"to": "priv/static/fonts/"
}
]
}
run-s from npm-run-all2 runs all scripts matching install:* on npm install, one after another. (run-p runs them in parallel instead.)
Usage with TailwindCSS
Now import the generated fonts.css file in the main TailwindCSS bundle:
@import "fonts";
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
Then change the font families in tailwind.config.js:
module.exports = {
theme: {
fontFamily: {
sans: ['IBM Plex Sans', 'Helvetica Neue', 'Arial', 'sans-serif'],
serif: ['IBM Plex Serif', 'Georgia', 'Times', 'serif'],
mono: [
'IBM Plex Mono', 'Menlo', 'DejaVu Sans Mono',
'Bitstream Vera Sans Mono', 'Courier', 'monospace'
]
},
...
After that, the familiar utility classes (font-sans, font-serif, font-mono) change the font family. The next time you update the font, everything should be generated again automatically.