-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.test.js
324 lines (279 loc) · 10.2 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const rimraf = require('rimraf')
const expect = require('chai').expect
const webpack = require('webpack')
const Plugin = require('../lib')
const DIST_FOLDER = path.join(__dirname, 'fixtures/dist/')
const FONT_AWESOME_FOLDER = path.join(__dirname, '../node_modules/font-awesome')
const CUSTOM_ICON_FOLDER = path.join(__dirname, 'fixtures/custom-icon')
function getFilenameWithoutQueryString(filename) {
return filename.split('?', 2)[0]
}
describe('FontminPlugin', () => {
let fontStats
const baseConfig = require('./fixtures/webpack.config')
const baseExtractConfig = require('./fixtures/webpack.extract-text.config')
const baseUnicodeConfig = require('./fixtures/webpack.unicode.config')
const customIconConfig = require('./fixtures/webpack.custom-icon.config')
const originalStats = collectFontStats(FONT_AWESOME_FOLDER + '/fonts', {
'fontawesome-webfont.eot': true,
'fontawesome-webfont.ttf': true,
'fontawesome-webfont.svg': true,
'fontawesome-webfont.woff': true,
'fontawesome-webfont.woff2': true,
})
const customIconOriginalStats = collectFontStats(CUSTOM_ICON_FOLDER, {
'custom-icon.eot': true,
'custom-icon.ttf': true,
'custom-icon.svg': true,
'custom-icon.woff': true,
})
function collectFontStats(directory, files) {
return _.keys(files)
.map(name => {
const filename = getFilenameWithoutQueryString(name)
const filePath = `${directory}/${filename}`
return {
filename,
filePath,
extension: path.extname(filename),
stats: fs.statSync(filePath),
}
})
.filter(item => item.extension !== '.js')
.filter(item => item.filename !== 'OpenSans-Bold.ttf')
}
function getGlyphs() {
const svg = _.find(fontStats, {extension: '.svg'})
const contents = fs.readFileSync(svg.filePath, 'utf8')
const matchedContents = contents.match(/glyph-name="(.*?)"/g)
const getGlyphName = s => s.slice('glyph-name="'.length, s.length - 1)
return matchedContents.map(getGlyphName)
}
function testWithConfig(config, done) {
webpack(config, (err, stats) => {
try {
if (err) {
done(err)
} else {
fontStats = collectFontStats(DIST_FOLDER, stats.compilation.assets)
done()
}
} catch (err) {
done(err)
}
})
}
describe('FontAwesome micro', () => {
it('should run successfully', function (done) {
this.timeout(10000)
const plugin = new Plugin({autodetect: false, glyphs: '\uF0C7'})
const config = _.cloneDeep(baseConfig)
testWithConfig(_.assign(config, {plugins: [plugin]}), done)
})
before(done => rimraf(DIST_FOLDER, done))
after(done => rimraf(DIST_FOLDER, done))
it('should minify eot', () => {
const eot = _.find(fontStats, {extension: '.eot'})
expect(eot.stats.size)
.to.be.greaterThan(500)
.lessThan(2400)
})
it('should minify svg', () => {
const glyphs = getGlyphs()
expect(glyphs).to.eql(['save'])
const svg = _.find(fontStats, {extension: '.svg'})
expect(svg.stats.size)
.to.be.greaterThan(500)
.lessThan(2000)
})
it('should minify tff', () => {
const ttf = _.find(fontStats, {extension: '.ttf'})
expect(ttf.stats.size)
.to.be.greaterThan(500)
.lessThan(2200)
})
it('should minify woff', () => {
const woff = _.find(fontStats, {extension: '.woff'})
expect(woff.stats.size)
.to.be.greaterThan(500)
.lessThan(2300)
})
it('should minify woff2', () => {
const woff2 = _.find(fontStats, {extension: '.woff2'})
expect(woff2.stats.size)
.to.be.greaterThan(500)
.lessThan(1000)
})
})
describe('FontAwesome inferred', () => {
it('should run successfully', function (done) {
this.timeout(60000)
testWithConfig(baseConfig, done)
})
after(done => rimraf(DIST_FOLDER, done))
it('should contain the right glyphs', () => {
const glyphs = getGlyphs()
expect(glyphs).to.not.contain('heart')
expect(glyphs).to.contain('table')
expect(glyphs).to.contain('film')
expect(glyphs).to.contain('ok')
expect(glyphs).to.contain('remove')
})
})
describe('FontAwesome full', () => {
it('should run successfully', function (done) {
this.timeout(60000)
const plugin = new Plugin({autodetect: false})
const config = _.cloneDeep(baseConfig)
testWithConfig(_.assign(config, {plugins: [plugin]}), done)
})
after(done => rimraf(DIST_FOLDER, done))
it('should not replace with a larger version', () => {
const svg = _.find(fontStats, {extension: '.svg'})
const svgOriginal = _.find(originalStats, {extension: '.svg'})
expect(svg.stats.size).to.equal(svgOriginal.stats.size)
})
})
describe('FontAwesome with ExtractTextPlugin', () => {
it('should run successfully', function (done) {
this.timeout(60000)
testWithConfig(baseExtractConfig, done)
})
after(done => rimraf(DIST_FOLDER, done))
it('should minify eot', () => {
const eot = _.find(fontStats, {extension: '.eot'})
expect(eot.stats.size)
.to.be.greaterThan(500)
.lessThan(7000)
})
it('should minify svg', () => {
const svg = _.find(fontStats, {extension: '.svg'})
expect(svg.stats.size)
.to.be.greaterThan(500)
.lessThan(7000)
})
it('should minify tff', () => {
const ttf = _.find(fontStats, {extension: '.ttf'})
expect(ttf.stats.size)
.to.be.greaterThan(500)
.lessThan(7000)
})
it('should minify woff', () => {
const woff = _.find(fontStats, {extension: '.woff'})
expect(woff.stats.size)
.to.be.greaterThan(500)
.lessThan(7000)
})
it('should minify woff2', () => {
const woff2 = _.find(fontStats, {extension: '.woff2'})
expect(woff2.stats.size)
.to.be.greaterThan(500)
.lessThan(7000)
})
it('should contain the right glyphs', () => {
const glyphs = getGlyphs()
expect(glyphs).to.not.contain('heart')
expect(glyphs).to.contain('table')
expect(glyphs).to.contain('film')
expect(glyphs).to.contain('ok')
expect(glyphs).to.contain('remove')
})
})
describe('FontAwesome with multi-byte unicode characters', () => {
it('should run successfully', function (done) {
this.timeout(60000)
testWithConfig(baseUnicodeConfig, done)
})
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)
})
})
describe('FontAwesome with appendHash option', () => {
it('should run successfully', function (done) {
this.timeout(60000)
const plugin = new Plugin({appendHash: true})
const config = _.cloneDeep(baseConfig)
testWithConfig(_.assign(config, {plugins: [plugin]}), done)
})
it('should append the hash to the ends of all refrences in all assets', () => {
const out = fs.readFileSync(DIST_FOLDER + '/out.js').toString()
fontStats.forEach(file => expect(out.match(file.filename)).to.be.ok)
fontStats.forEach(file => expect(out.match(file.filename.replace(/-([a-z]|[0-9])+\./, '.'))).to.not.be.ok)
})
})
describe('Custom Icon pack', () => {
it('should run successfully', function (done) {
this.timeout(10000)
const plugin = new Plugin({autodetect: true})
const config = _.cloneDeep(customIconConfig)
testWithConfig(_.assign(config, {plugins: [plugin]}), done)
})
before(done => rimraf(DIST_FOLDER, done))
after(done => rimraf(DIST_FOLDER, done))
it('should contain the right glyphs', () => {
const glyphs = getGlyphs()
expect(glyphs).to.not.contain('cloud-lightning')
expect(glyphs).to.contain('coffee')
expect(glyphs).to.contain('plus')
})
it('should minify eot', () => {
const original = _.find(customIconOriginalStats, {extension: '.eot'})
const eot = _.find(fontStats, {extension: '.eot'})
expect(eot.stats.size)
.to.be.greaterThan(500)
.lessThan(original.stats.size)
})
it('should minify svg', () => {
const glyphs = getGlyphs()
expect(glyphs).to.eql(['plus', 'coffee'])
const original = _.find(customIconOriginalStats, {extension: '.svg'})
const svg = _.find(fontStats, {extension: '.svg'})
expect(svg.stats.size)
.to.be.greaterThan(500)
.lessThan(original.stats.size)
})
it('should minify tff', () => {
const original = _.find(customIconOriginalStats, {extension: '.ttf'})
const ttf = _.find(fontStats, {extension: '.ttf'})
expect(ttf.stats.size)
.to.be.greaterThan(500)
.lessThan(original.stats.size)
})
it('should minify woff', () => {
const original = _.find(customIconOriginalStats, {extension: '.woff'})
const woff = _.find(fontStats, {extension: '.woff'})
expect(woff.stats.size)
.to.be.greaterThan(500)
.lessThan(original.stats.size)
})
})
})