-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
280 lines (253 loc) · 9.03 KB
/
index.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
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const log = require('debug')('fontmin-webpack')
const _ = require('lodash')
const Fontmin = require('fontmin')
const RawSource = require('webpack-sources').RawSource
const FONT_REGEX = /\.(eot|ttf|svg|woff|woff2)(\?.+)?$/
const TEXT_REGEX = /\.(js|css|html)$/
const GLYPH_REGEX = /content\s*:[^};]*?('|")(.*?)\s*('|"|;)/g
const UNICODE_REGEX = /\\([0-9a-f]{2,6})/i
const FONTMIN_EXTENSIONS = ['eot', 'woff', 'woff2', 'svg']
const WEBPACK_COMPILATION = 'thisCompilation'
function getSurrogatePair(astralCodePoint) {
const highSurrogate = Math.floor((astralCodePoint - 0x10000) / 0x400) + 0xD800
const lowSurrogate = ((astralCodePoint - 0x10000) % 0x400) + 0xDC00
return [highSurrogate, lowSurrogate]
}
function getFilenameWithoutQueryString(filename) {
return filename.split('?', 2)[0]
}
class FontminPlugin {
constructor(options) {
this._options = _.assign(
{
glyphs: [],
autodetect: true,
allowedFilesRegex: null,
skippedFilesRegex: null,
appendHash: false,
textRegex: TEXT_REGEX,
webpackCompilationHook: WEBPACK_COMPILATION,
},
options,
)
}
computeFinalGlyphs(foundGlyphs) {
let glyphs = _.clone(this._options.glyphs)
if (this._options.autodetect) {
glyphs = glyphs.concat(foundGlyphs)
}
return glyphs
}
hasFontAsset(assets) {
return _.find(assets, (val, key) => FONT_REGEX.test(key))
}
findFontFiles(compilation) {
const regular = this.findRegularFontFiles(compilation)
const extract = this.findExtractTextFontFiles(compilation)
// Prefer the ExtractTextPlugin version of any assets we find
const regularWithExtractRemoved = _.differenceBy(regular, extract, 'asset')
return _.uniqBy(regularWithExtractRemoved.concat(extract), 'asset')
}
findRegularFontFiles(compilation) {
return _(Array.from(compilation.modules))
.filter(module => this.hasFontAsset(module.buildInfo.assets))
.map(module => {
const filename = Array.from(module.buildInfo.assetsInfo.values())[0].sourceFilename
const font = path.basename(filename, path.extname(filename))
return _.keys(module.buildInfo.assets).map(asset => {
const buffer = module.buildInfo.assets[asset].source()
const extension = path.extname(getFilenameWithoutQueryString(asset))
return {asset, extension, font, buffer}
})
})
.flatten()
.value()
}
findExtractTextFontFiles(compilation) {
const fileDependencies = _(Array.from(compilation.modules))
.map(module =>
Array.from((module.buildInfo.assetsInfo && module.buildInfo.assetsInfo.values()) || []).map(
file => file.sourceFilename,
),
)
.flatten()
.filter(filename => FONT_REGEX.test(filename))
.map(getFilenameWithoutQueryString)
.map(filename => {
return {
filename,
stats: fs.statSync(filename),
}
})
.value()
return _(compilation.assets)
.keys()
.filter(name => FONT_REGEX.test(name))
.map(asset => {
const assetFilename = getFilenameWithoutQueryString(asset)
const buffer = compilation.assets[asset].source()
const extension = path.extname(assetFilename)
const dependency = fileDependencies.find(dependency => {
return (
path.extname(dependency.filename) === extension &&
buffer.length === dependency.stats.size
)
})
const filename = (dependency && dependency.filename) || assetFilename
const font = path.basename(filename, extension)
return {asset, extension, font, buffer}
})
.value()
}
findUnicodeGlyphs(compilation) {
return _(compilation.assets)
.map((asset, name) => ({asset, name}))
.filter(item => this._options.textRegex.test(item.name))
.map(item => {
const content = item.asset.source()
const matches = content.match(GLYPH_REGEX) || []
return matches
.map(match => {
const unicodeMatch = match.match(UNICODE_REGEX)
if (!unicodeMatch) {
return false
}
const unicodeHex = unicodeMatch[1]
const numericValue = parseInt(unicodeHex, 16)
const character = String.fromCharCode(numericValue)
if (unicodeHex.length === 2 || unicodeHex.length === 4) {
return character
}
const multiCharacter = getSurrogatePair(numericValue)
.map(v => String.fromCharCode(v))
.join('')
return multiCharacter
})
.filter(Boolean)
})
.flatten()
.value()
}
setupFontmin(extensions, usedGlyphs = []) {
usedGlyphs = _.isArray(usedGlyphs) ? usedGlyphs : [usedGlyphs]
let fontmin = new Fontmin().use(Fontmin.glyph({text: usedGlyphs.join(' ')}))
FONTMIN_EXTENSIONS.forEach(ext => {
if (extensions.includes('.' + ext)) {
fontmin = fontmin.use(Fontmin[`ttf2${ext}`]())
}
})
return fontmin
}
mergeAssetsAndFiles(group, files) {
const byExtension = _.keyBy(files, 'extname')
return group
.map(item => {
const extension = item.extension
const buffer = byExtension[extension] && byExtension[extension].contents
if (!buffer) {
return undefined
}
const minified = buffer
return _.assign(item, {minified})
})
.filter(Boolean)
}
minifyFontGroup(group, usedGlyphs = []) {
log('analyzing font group:', _.get(group, '0.font'))
const ttfInfo = _.find(group, {extension: '.ttf'})
if (!ttfInfo) {
log('font group has no TTF file, skipping...')
return Promise.resolve([])
}
const extensions = _.map(group, 'extension')
const fontmin = this.setupFontmin(extensions, usedGlyphs)
return new Promise((resolve, reject) => {
fontmin.src(ttfInfo.buffer).run((err, files) => {
if (err) {
reject(err)
} else {
resolve(this.mergeAssetsAndFiles(group, files))
}
})
})
}
onAdditionalAssets(compilation) {
const allowedFiles = this._options.allowedFilesRegex
const skippedFiles = this._options.skippedFilesRegex
const appendHash = this._options.appendHash
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)
return minifiableFonts.reduce((prev, group) => {
return prev
.then(() => this.minifyFontGroup(group, glyphs))
.then(files => {
return files.forEach(file => {
if (file.buffer.length > file.minified.length) {
if (appendHash) {
const newAssetName = this.appendMinifiedFileHash(file)
compilation.assets[newAssetName] = new RawSource(file.minified)
this.hashifyFontReferences(file.asset, newAssetName, compilation.assets)
delete compilation.assets[file.asset]
} else {
compilation.assets[file.asset] = new RawSource(file.minified)
}
}
})
})
}, Promise.resolve())
}
appendMinifiedFileHash(file) {
const fileHash = crypto.createHash('md5').update(file.minified).digest('hex')
return file.asset.split('.').join(`-${fileHash}.`)
}
hashifyFontReferences(oldAssetName, newAssetName, assets) {
Object.keys(assets).forEach(
asset => {
const oldAssetNameRegex = new RegExp(
oldAssetName.replace('.', '\\.').replace('?', '\\?'),
'g'
)
const assetSource = assets[asset].source().toString()
if (assetSource.match(oldAssetNameRegex)) {
assets[asset] = new RawSource(assetSource.replace(oldAssetNameRegex, newAssetName))
}
}
)
}
apply(compiler) {
compiler.hooks[this._options.webpackCompilationHook].tap('FontminPlugin', compilation => {
compilation.hooks.additionalAssets.tapPromise('FontminPlugin', () => {
if (!compilation.modules || !compilation.assets) {
// eslint-disable-next-line no-console
console.warn(`[fontmin-webpack] Failed to detect modules. Check your webpack version!`)
return Promise.resolve()
}
return this.onAdditionalAssets(compilation)
})
})
}
}
module.exports = FontminPlugin