PostCSS Font Grabber
3.x
is under active development.
A postcss
plugin, it grabs remote font files and update your CSS, just like that.
postcss-font-grabber
v3.x
only works with postcss
v8
,for postcss
v7
, please take a look at the v2.x
.
Motivation
You may not want to use remote fonts, because:
- it may expose your internal project
- font service may be slow for your users
- you can do more things with local font files
- GDPR compliance
Features
- Support custom download function (the
download
option) - Written in TypeScript
- Standalone without any dependency
- Download font files concurrently
- Infer font file extension from HTTP response header (Thanks to @FTWinston)
Installation
Requires: Node >= 10.0
, postcss 8.*
npm install postcss postcss-font-grabber --save-dev
Options
Function postcssFontGrabber
takes an object of options as parameter, like:
import { postcssFontGrabber } from 'postcss-font-grabber';
postcssFontGrabber({
cssSrc: 'src/css/',
cssDest: 'dist/',
fontDest: 'dist/fonts/',
});
Name | Type | Default | Description |
---|
cssSrc | string | opts.from from PostCSS 's setting | The root directory path of all CSS files |
cssDest | string | opts.to from PostCSS 's setting | The directory where the transpiled CSS files are in |
fontDest | string | the same as cssDest | The directory where the downloaded fonts stored |
download | (fontSpec: FontSpec) => Promise<{ data: Readable, mimeType?: string }> | - | Custom function to download font files |
You can import types as shown above:
import { FontSpec, Downloader, DownloadResult } from 'postcss-font-grabber';
Usages
With Gulp
gulp.task('css', () => {
const postcss = require('gulp-postcss');
const { postcssFontGrabber } = require('postcss-font-grabber');
return gulp
.src('src/css/**/*.css')
.pipe(
postcss([
postcssFontGrabber({
cssDest: 'dist/',
fontDest: 'dist/fonts/',
}),
]),
)
.pipe(gulp.dest('dist/'));
});
With Webpack
This example is using Webpack 5
with these packages:
webpack.config.js
:
import path from 'path';
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
use: ['file-loader'],
},
],
},
};
postcss.config.js
:
import { postcssFontGrabber } from 'postcss-font-grabber';
module.exports = {
plugins: [
postcssFontGrabber({
cssSrc: 'src/css/',
cssDest: 'src/css/',
fontDest: 'tmp/css/fonts/',
}),
],
};
With Only PostCSS
PostCSS-Font-Grabber
will use from
and to
options of PostCSS
setting as the default options of cssSrc
(from
), cssDest
and fontDest
(to
).
Advanced Usages
TODO
License
Licensed under the APACHE LISENCE 2.0.
Credits
PostCSS
PostCSS Copy Assets
Issue Reporters