Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
postcss-font-magician
Advanced tools
PostCSS plugin that magically generates all the @font-face rules
Font Magician is a PostCSS plugin that magically generates all of your @font-face
rules. Never write a @font-face
rule again.
Just use the font and font-family properties as if they were magic.
/* before */
body {
font-family: "Alice";
}
/* after */
@font-face {
font-family: "Alice";
font-style: normal;
font-weight: 400;
src: local("Alice"), local("Alice-Regular"),
url("http://fonts.gstatic.com/s/alice/v7/sZyKh5NKrCk1xkCk_F1S8A.eot?#") format("eot"),
url("http://fonts.gstatic.com/s/alice/v7/l5RFQT5MQiajQkFxjDLySg.woff2") format("woff2"),
url("http://fonts.gstatic.com/s/alice/v7/_H4kMcdhHr0B8RDaQcqpTA.woff") format("woff"),
url("http://fonts.gstatic.com/s/alice/v7/acf9XsUhgp1k2j79ATk2cw.ttf") format("truetype")
}
body {
font-family: "Alice";
}
body {
font-family: "Material Icons";
}
Seriously, never write a @font-face
rule again.
Need more? Request additional magic by creating an issue.
Add Font Magician to your build tool.
npm install postcss postcss-font-magician --save-dev
or
yarn add postcss postcss-font-magician --dev
Use Font Magician in your script:
js const fs = require('fs'); const postcss = require('postcss'); const pfm = require('postcss-font-magician'); postcss([ pfm({ /* options */ }) ]).process( fs.readFileSync('./css/src/style.css', 'utf8') ).then(function (result) { fs.writeFileSync('./css/style.css', result.css); });
Use Font Magician in your webpack.config.js:
Webpack 1.x
postcss: function () {
return [
...
fontMagician({
variants: {
'Roboto Condensed': {
'300': [],
'400': [],
'700': []
}
},
foundries: ['google']
})
...
];
}
Webpack 2.x
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
...
postcss: [
...
fontMagician({
variants: {
'Roboto Condensed': {
'300': [],
'400': [],
'700': []
}
},
foundries: ['google']
})
]
},
}),
...
]
Webpack 3+
postcss.config.js
module.exports = {
plugins: {
'postcss-font-magician': {
variants: {
'Roboto Condensed': {
'300': [],
'400': [],
'700': []
}
},
foundries: ['google']
}
}
}
webpack.config.js
{
loader: 'postcss-loader',
options: {
config: {
path: 'path/to/postcss.config.js'
}
}
}
Use Font Magician in your Gruntfile:
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
processors: [
require('postcss-font-magician')({ /* options */ })
]
},
src: './css/src/*.css',
dest: './css'
}
});
Use Font Magician in your Gulpfile:
var postcss = require('gulp-postcss');
gulp.task('css', function () {
return gulp.src('./css/src/*.css').pipe(
postcss([
require('postcss-font-magician')({ /* options */ })
])
).pipe(
gulp.dest('./css')
);
});
Want to download only specific font weights, styles and formats?
require('postcss-font-magician')({
variants: {
'Open Sans': {
'300': ['woff, eot, woff2'],
'400 italic': ['woff2']
}
}
});
The plugin will download the font only selected weights, styles and formats.
Need to support unicode-range? You can use both the names of the subsets of Google fonts (e.g. 'latin-ext' or 'cyrillic') and specific unicode ranges values. You can use it as a second element in an option array.
require('postcss-font-magician')({
variants: {
'Open Sans': {
'300': ['woff2', 'cyrillic-ext, greek'],
'400 italic': ['woff2', 'U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF']
}
}
});
Need to support font-stretch? You can use it as a 2nd or 3rd params in an option key.
require('postcss-font-magician')({
variants: {
'Open Sans': {
'300 ultra-condensed': ['woff, eot, woff2'],
'400 italic': ['woff2'],
'400 italic semi-expanded': ['woff2']
}
}
});
Need to control how custom fonts display when they loading? You can use font-display css rule.
require('postcss-font-magician')({
display: 'swap'
});
Have a directory of self-hosted fonts?
require('postcss-font-magician')({
hosted: ['./src/fonts', /custom/path/to/fonts/on/site]
});
The directory will be scanned for font files, which will be read and automatically added if they are used in the document.
Please note that at the moment the plugin doesn't detect woff2 fonts
The first argument of hosted
array is required and it should be a path that related to the project root. i.e. ./src/fonts
In this case, the path to the fonts folder structure must match the structure of the path to the fonts folder on the server, because the path will be relative css file.
The second argument of hosted
array is optional and it can be an arbitrary path which should match the fonts directory on the site.
i.e. /assets/fonts
Prefer another name for particular fonts?
require('postcss-font-magician')({
aliases: {
'sans-serif': 'Source Sans Pro'
}
});
The @font-face
rule will be updated so that your alias key is the new name of the font family.
You can also use the 'aliases' option with other options, i.g with the 'variants'
require('postcss-font-magician')({
aliases: {
'sans-serif': 'Source Sans Pro'
},
variants: {
'sans-serif': {
'400': ['woff']
}
}
});
Need to load the fonts asynchronously?
require('postcss-font-magician')({
async: 'path/to/your-fonts-async-loader.js'
});
Any @font-face
rules are moved out of CSS and into a JavaScript file may be added to a page separately. It will automatically load the fonts before using them.
Want to control which font formats are used?
require('postcss-font-magician')({
formats: 'woff2 woff'
});
By default, local
, woff2
, woff
, and eot
are enabled.
Supported formats include local
, woff2
, woff
, ttf
, eot
, svg
, and otf
.
Want to enable specific foundries? Provide a space-separated list or array:
require('postcss-font-magician')({
foundries: 'bootstrap google'
});
By default, all foundries are enabled.
Supported foundries include custom
, hosted
, bootstrap
, and google
.
Need something very specific? I can do that, too.
require('postcss-font-magician')({
custom: {
'My Special Font': {
variants: {
normal: {
400: {
url: {
woff2: 'path/to/my-body-font-normal-400.woff2',
woff: 'path/to/my-body-font-normal-400.woff'
}
},
700: {
url: {
woff2: 'path/to/my-body-font-normal-700.woff2'
}
}
},
italic: {
400: {
url: {
woff2: 'path/to/my-body-font-italic-400.woff2'
}
}
}
}
}
}
});
Do you want to control the font URL-address protocol?
require('postcss-font-magician')({
protocol: 'https:'
});
By default, 'http/https' protocol is removed from the font url. Supports any string values, eg - '', 'http:' or 'https:'
If you don't need Font Magician in some part of your CSS, you can use control comments to disable it.
body {
/* font-magician: ignore-next */
font-family: "Alice"; /* will not generate font-face */
}
Look forward to more magic, like:
Enjoy!
4.0.0 (2024-02-25)
postcss-load-config
#102async
option, this is now relative to the process. (BREAKING!)@font-face
will be inserted after @charset
, @import
and similar statements. #107FAQs
PostCSS plugin that magically generates all the @font-face rules
The npm package postcss-font-magician receives a total of 2,320 weekly downloads. As such, postcss-font-magician popularity was classified as popular.
We found that postcss-font-magician demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.