diff --git a/README.md b/README.md index 46f5fd9..bedac5a 100644 --- a/README.md +++ b/README.md @@ -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 }), ], } diff --git a/lib/index.js b/lib/index.js index ca4c54d..7c67449 100644 --- a/lib/index.js +++ b/lib/index.js @@ -25,6 +25,8 @@ class FontminPlugin { { glyphs: [], autodetect: true, + allowedFilesRegex: null, + skippedFilesRegex: null, }, options, ) @@ -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) diff --git a/test/index.test.js b/test/index.test.js index 2d8323b..d2b5cc7 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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) + }) + }) })