-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
289 lines (229 loc) · 9.92 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
281
282
283
284
285
286
287
288
289
// @ts-check
const debug = require('debug')('metalsmith-css-packer')
const Bluebird = require('bluebird');
const cheerio = require('cheerio');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const csso = require('csso');
const rp = require('request-promise');
module.exports = options => {
if (typeof options !== 'object' || options === null) {
options = {};
}
let inline = options.inline || false;
let siteRootPath = options.siteRootPath || '/';
let outputPath = options.outputPath || 'assets/stylesheets/';
let cssoEnabled = options.csso || true;
let cssoOptions = options.cssoOptions || {};
let defaultMedia = options.defaultMedia || 'screen';
let removeLocalSrc = options.removeLocalSrc || false;
let assetsSource = options.assetsSource || false;
let hashContent = options.hashContent || false;
return (files, metalsmith, done) => {
let styles = {};
let packedStyles = {};
let remoteStylesPromises = [];
let packedStylesUsage = {};
for (let file in files) {
// parse only builded html files
if (!file.endsWith('.html')) {
continue;
}
let $ = cheerio.load(files[file].contents.toString());
let $links = $('link');
let $styles = $('style');
let pageStyles = {};
let pageStylesHash;
debug(`processing ${$links.length} linked stylesheets in "${file}" file`);
$links.each((_, element) => {
let $element = $(element);
// here if a <style> element has a "src" attribute
// -> external style content
if ($element.attr('href') && $element.attr('rel') === 'stylesheet' && $element.data('packer') !== 'exclude') {
let href = $element.attr('href');
let styleHash = crypto.createHash('sha1').update(href).digest("hex");
let media = defaultMedia;
if ($element.attr('media') !== undefined) {
media = $element.attr('media');
}
if (styles[media] === undefined) {
styles[media] = {};
}
if (pageStyles[media] === undefined) {
pageStyles[media] = [];
}
// handle remote styles (uniqness: href)
// - add remote style to pending styles
// - fetch remote style in memory
// - insert it in a cache to avoid to re download it
// - remove original <style> tag
if (href.startsWith('//') || href.startsWith('http')) {
debug(`+ remote stylesheet located at "${href}"`);
if (styles[media][styleHash] === undefined) {
debug(`+--> processing remote style located at "${href}"`);
if (href.startsWith('//')) {
href = 'http:' + href
}
// allocate style to prevent multiple processing
styles[media][styleHash] = ''
// add remote style to pending styles
remoteStylesPromises.push(rp(href).then(content => {
styles[media][styleHash] = content;
}))
}
pageStyles[media].push(styleHash);
}
// handle local styles (uniqness: href)
// - load local style in memory
// - insert it in a cache to avoid to re read it from fs
// - remove original <style> tag
else {
debug(`+ local stylesheet located at "${href}"`);
if (styles[media][styleHash] === undefined) {
debug(`+--> processing local stylesheet located at "${href}"`);
// TODO: check with generated css with sass / less
let stylePath = path.join(metalsmith._directory, assetsSource ? assetsSource : metalsmith._source, href)
if (files[href.substring(1)] === undefined) {
if (!fs.existsSync(stylePath)) {
console.warn(`File missing: ${stylePath}`);
return;
}
else {
debug(`+----> reading ${stylePath} from filesystem`);
styles[media][styleHash] = fs.readFileSync(stylePath, "utf8");
}
}
else {
styles[media][styleHash] = files[href.substring(1)].contents.toString();
if (removeLocalSrc) {
debug(`+----> removing local stylesheet file ${href.substring(1)}`);
delete files[href.substring(1)];
}
}
}
pageStyles[media].push(styleHash);
}
$element.remove();
return;
}
else {
if ($element.data('packer') === 'exclude') {
$element.removeAttr('data-packer');
debug(`- skipping excluded stylesheet <link> tag`);
}
else {
debug(`- skipping unknown stylesheet <link> tag in file "${file}"\n${$element.toString()}`);
}
}
});
debug(`processing ${$styles.length} inline stylesheets in "${file}" file`);
$styles.each((_, element) => {
let $element = $(element);
let media = defaultMedia;
if ($element.attr('media') !== undefined) {
media = $element.attr('media');
}
if (styles[media] === undefined) {
styles[media] = {};
}
if (pageStyles[media] === undefined) {
pageStyles[media] = [];
}
// -> internal style content (uniqness: content hash + media attribute if any, sha1 might be enougth)
// - load tag content in memory
// - remove original <style> tag
if (($element.attr('type') === 'text/css' || $element.attr('type') === undefined) && $element.data('packer') !== 'exclude') {
let styleIdentifier = $element.html();
let styleHash = crypto.createHash('sha1').update($element.html()).digest("hex");
debug(`+ inline stylesheet identified by "${styleHash}"`);
if (styles[media][styleHash] === undefined) {
debug(`+--> processing inline stylesheet identified by "${styleHash}"`);
styles[media][styleHash] = $element.html();
return;
}
pageStyles[media].push(styleHash);
$element.remove();
return;
}
else {
if ($element.data('packer') === 'exclude') {
$element.removeAttr('data-packer');
debug(`- skipping excluded stylesheet <link> tag`);
}
else {
debug(`- skipping unknown stylesheet <link> tag in file "${file}"\n${$element.toString()}`);
}
}
})
// - if current page contains styles, create a hash of style names this page needs
// we will distinguish same grouped style usage with this
for (let media in pageStyles) {
if (pageStyles[media].length > 0) {
pageStylesHash = crypto.createHash('sha1').update(pageStyles[media].join('.')).digest("hex");
packedStyles[pageStylesHash] = {
media,
ressources: pageStyles[media]
};
packedStylesUsage[pageStylesHash] = packedStylesUsage[pageStylesHash] || [];
packedStylesUsage[pageStylesHash].push(file);
debug(`register usage of packed style "${pageStylesHash}" (media: "${media}") for file "${file}"`);
}
}
files[file].contents = Buffer.from($.html(), 'utf-8');
}
if (remoteStylesPromises.length === 0) {
remoteStylesPromises.push(Bluebird.resolve());
}
// we can pack all styles togethers once all pending remotes styles are fetched
Bluebird.all(remoteStylesPromises)
.then(() => {
for (let pageStylesHash in packedStyles) {
debug(`create packed stylesheet "${pageStylesHash}", used by ${packedStylesUsage[pageStylesHash].length} files`);
let packedStyle = '';
for (let i = 0; i < packedStyles[pageStylesHash].ressources.length; i++) {
packedStyle += styles[packedStyles[pageStylesHash].media][packedStyles[pageStylesHash].ressources[i]] + '\n'
}
if (cssoEnabled) {
packedStyle = csso.minify(packedStyle, cssoOptions).css;
}
let finalHash;
if (hashContent) {
finalHash = crypto.createHash('sha1').update(packedStyle).digest('hex');
debug(`mapping hash "${pageStylesHash}" to content hash "${finalHash}"`);
} else {
finalHash = pageStylesHash;
}
// include style reference only when inline mode is enabled
// else, add new packed file to metalsmith file list and
// link it
if (!inline) {
debug(`write packed stylesheet "${pageStylesHash}" in "${outputPath + finalHash + '.min.css'}" file`);
files[outputPath + finalHash + '.min.css'] = {
contents: Buffer.from(packedStyle, 'utf-8')
};
for (let file of packedStylesUsage[pageStylesHash]) {
debug(`link packed stylesheet "${finalHash}" in "${file}" file`);
let $ = cheerio.load(files[file].contents.toString());
let $head = $('head').first();
$('<link>').attr('media', packedStyles[pageStylesHash].media).attr('rel', 'stylesheet').attr('href', siteRootPath + outputPath + finalHash + '.min.css').appendTo($head);
files[file].contents = Buffer.from($.html(), 'utf-8');
}
}
else {
for (let file of packedStylesUsage[pageStylesHash]) {
debug(`include packed stylesheet "${pageStylesHash}" in "${file}" file`);
let $ = cheerio.load(files[file].contents.toString());
let $head = $('head').first();
$('<style>').attr('media', packedStyles[pageStylesHash].media).html(packedStyle).appendTo($head);
files[file].contents = Buffer.from($.html(), 'utf-8');
}
}
}
})
.then(() => done())
.catch(err => {
throw new Error(err)
})
}
}