Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to filter optimized fonts with an allowlist and a skiplist #48

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})