forked from peterbe/minimalcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimalcss.js
More file actions
executable file
·156 lines (144 loc) · 4.59 KB
/
minimalcss.js
File metadata and controls
executable file
·156 lines (144 loc) · 4.59 KB
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
#!/usr/bin/env node
'use strict';
const { URL } = require('url');
const fs = require('fs');
const minimist = require('minimist');
const minimalcss = eval('require')('../index');
const filesize = require('filesize');
const args = process.argv.slice(2);
const argv = minimist(args, {
boolean: [
'help',
'version',
'verbose',
'debug',
'loadimages',
'styletags',
'withoutjavascript',
'nosandbox',
'enableserviceworkers',
],
string: ['output', 'skip', 'viewport'],
default: {
// color: true,
},
alias: {
debug: 'd',
help: 'h',
version: 'v',
output: 'o',
},
unknown: (param) => {
if (param.startsWith('-')) {
console.warn('Ignored unknown option: ' + param + '\n');
return false;
}
},
});
if (argv['version']) {
console.log(minimalcss.version);
process.exit(0);
}
if (argv['help']) {
console.log(
'Usage: minimalcss [opts] url [url2 ...]\n\n' +
'Available options:\n' +
' --output <path> or -o <path> Path to write the final CSS to.\n' +
' --verbose Include a comment about the options and the date it was generated.\n' +
' --debug or -d Print all console logging during page rendering to stdout.\n' +
' --loadimages By default, all images are NOT downloaded. This reverses that.\n' +
' --styletags By default, all <style> tags are ignored. This will include them.\n' +
' --enableserviceworkers By default, use of Service Workers is disable. This flag enables them.\n' +
' --withoutjavascript The CSS is evaluated against the DOM twice, first with no JavaScript, ' +
'then with. This disables the load without JavaScript.\n' +
' --skip String to match in URL to ignore download. Repeatable. E.g. --skip google-analyics.com\n' +
' --viewport JSON string that gets converted into valid parameter to `page.setViewport()`\n' +
" --nosandbox Adds `['--no-sandbox', '--disable-setuid-sandbox']` to puppeteer launch.\n" +
' --version or -v Print minimalcss version.\n' +
''
);
process.exit(0);
}
const urls = argv['_'];
urls.forEach((url) => {
try {
const parsed = new URL(url);
} catch (ex) {
console.error(`${url} is not a valid URL`);
process.exit(1);
}
});
const parseViewport = (asString) => {
if (!asString) {
return null;
}
try {
return JSON.parse(asString);
} catch (ex) {
console.error(`Unable to parse 'viewport' (${ex.toString()})`);
process.exit(2);
}
};
const options = {
urls: urls,
debug: argv['debug'],
loadimages: argv['loadimages'],
styletags: argv['styletags'],
withoutjavascript: argv['withoutjavascript'],
skippable: (request) => {
let skips = argv['skip'];
if (!skips) {
return false;
}
if (!Array.isArray(skips)) {
skips = [skips];
}
return skips.some((skip) => !!request.url().match(skip));
},
viewport: parseViewport(argv['viewport']),
puppeteerArgs: argv['nosandbox']
? ['--no-sandbox', '--disable-setuid-sandbox']
: [],
enableServiceWorkers: argv['enableserviceworkers'],
};
const start = Date.now();
minimalcss
.minimize(options)
.then((result) => {
let output = result.finalCss;
const end = Date.now();
if (argv['verbose']) {
const now = new Date().toISOString();
let comment = `/*\nGenerated ${now} by minimalcss.\n`;
const seconds = ((end - start) / 1000).toFixed(2);
const bytesHuman = filesize(output.length);
const stylesheetContents = result.stylesheetContents;
const stylesheets = Object.keys(stylesheetContents);
const totalSizeBefore = stylesheets.reduce(
(acc, key) => acc + stylesheetContents[key].length,
0
);
const totalSizeBeforeHuman = filesize(totalSizeBefore);
comment += `Took ${seconds} seconds to generate ${bytesHuman} of CSS.\n`;
comment += `Based on ${stylesheets.length} stylesheets `;
comment += `totalling ${totalSizeBeforeHuman}.\n`;
comment += 'Options: ' + JSON.stringify(options, undefined, 2) + '\n';
comment += '*/';
output = `${comment}\n${output}`;
}
if (argv['output']) {
const filename = argv['output'];
try {
fs.writeFileSync(filename, output + '\n', 'utf8');
} catch (err) {
console.error('Unable to write file: ' + filename + '\n' + err);
process.exit(2);
}
} else {
console.log(output);
}
})
.catch((error) => {
console.error(error);
process.exit(3);
});