Skip to content

Commit

Permalink
feat: filter optimized fonts with an allowlist/skiplist (#48)
Browse files Browse the repository at this point in the history
Add two options: allowedFilesRegex & skippedFilesRegex. Both can be null or RegExp, they're mutually exclusive and allowedFilesRegex has priority over skippedFilesRegex.
  • Loading branch information
Moustachos authored Sep 14, 2021
1 parent 9a27b9e commit 4c72d42
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ module.exports = {
new FontminPlugin({
autodetect: true, // automatically pull unicode characters from CSS
glyphs: ['\uf0c8' /* extra glyphs to include */],
// note: these settings are mutually exclusive and allowedFilesRegex has priority over skippedFilesRegex
allowedFilesRegex: null, // RegExp to only target specific fonts by their names
skippedFilesRegex: null, // RegExp to skip specific fonts by their names
}),
],
}
Expand Down
20 changes: 20 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class FontminPlugin {
{
glyphs: [],
autodetect: true,
allowedFilesRegex: null,
skippedFilesRegex: null,
},
options,
)
Expand Down Expand Up @@ -185,11 +187,29 @@ class FontminPlugin {
}

onAdditionalAssets(compilation) {
const allowedFiles = this._options.allowedFilesRegex
const skippedFiles = this._options.skippedFilesRegex
const fontFiles = this.findFontFiles(compilation)
const glyphsInCss = this.findUnicodeGlyphs(compilation)
log(`found ${glyphsInCss.length} glyphs in CSS`)
const minifiableFonts = _(fontFiles)
.groupBy('font')
.filter(font => {
const fontName = font[0].font
if (allowedFiles instanceof RegExp) {
if (!fontName.match(allowedFiles)) {
log(`Font "${fontName}" not allowed by pattern: ${allowedFiles}.`)
return false
}
} else if (skippedFiles instanceof RegExp) {
if (fontName.match(skippedFiles)) {
log(`Font "${fontName}" skipped by pattern ${skippedFiles}.`)
return false
}
}

return true
})
.values()

const glyphs = this.computeFinalGlyphs(glyphsInCss)
Expand Down
34 changes: 34 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,38 @@ describe('FontminPlugin', () => {

after(done => rimraf(DIST_FOLDER, done))
})

describe('FontAwesome in allowed fonts list', () => {
it('should run successfully', function (done) {
this.timeout(60000)
const plugin = new Plugin({allowedFilesRegex: /^fontawesome/})
const config = _.cloneDeep(baseConfig)
testWithConfig(_.assign(config, {plugins: [plugin]}), done)
})

after(done => rimraf(DIST_FOLDER, done))

it('should minify font', () => {
const svg = _.find(fontStats, {extension: '.svg'})
const svgOriginal = _.find(originalStats, {extension: '.svg'})
expect(svg.stats.size).to.be.lessThan(svgOriginal.stats.size)
})
})

describe('FontAwesome in skipped fonts list', () => {
it('should run successfully', function (done) {
this.timeout(60000)
const plugin = new Plugin({skippedFilesRegex: /^fontawesome/})
const config = _.cloneDeep(baseConfig)
testWithConfig(_.assign(config, {plugins: [plugin]}), done)
})

after(done => rimraf(DIST_FOLDER, done))

it('should not minify font', () => {
const svg = _.find(fontStats, {extension: '.svg'})
const svgOriginal = _.find(originalStats, {extension: '.svg'})
expect(svg.stats.size).to.be.equal(svgOriginal.stats.size)
})
})
})

0 comments on commit 4c72d42

Please sign in to comment.