forked from filamentgroup/criticalCSS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcritical.js
More file actions
156 lines (128 loc) · 3.48 KB
/
critical.js
File metadata and controls
156 lines (128 loc) · 3.48 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
/*global require:true*/
/*global console:true*/
/*global __dirname:true*/
(function( exports ){
"use strict";
var phantomJsPath = require("phantomjs").path;
var execFile = require("child_process").execFile;
var path = require( "path" );
var fs = require( "fs" );
var os = require( "os" );
var DEFAULT_BUFFER_SIZE = 800*1024; //had this as the set val before, don't want to break things
exports.getRules = function( cssfile, opts, cb ){
var defaultCb = function( err, output ){
if( err ){
throw new Error( err );
} else {
console.log( output );
}
};
if( typeof cssfile !== "string" ){
throw new TypeError( "The CSS filename must be a string" );
}
if( !fs.existsSync( cssfile ) ){
throw new Error( "CSS file must exist" );
}
if( typeof opts === "undefined" && typeof cb === "undefined" ){
opts = {};
cb = defaultCb;
}
if( typeof opts === "function" ){
cb = opts;
opts = {};
}
var bufferSize = opts.buffer || DEFAULT_BUFFER_SIZE;
execFile( phantomJsPath,
[
path.resolve( path.join( __dirname, "lib", "rules.js" ) ),
cssfile
],
{
maxBuffer: bufferSize
},
function(err, stdout, stderr){
if( err ){
console.log("\nSomething went wrong with phantomjs...");
if( stderr ){
err.message = stderr;
}
cb( err, null );
} else {
stdout = stdout.replace("Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL ", "");
stdout = stdout.replace(/file:\/\/.*rules.js\./, "");
stdout = stdout.replace(" Domains, protocols and ports must match.\n\n", "");
stdout = stdout.replace(" Domains, protocols and ports must match.\r\n\r\n", ""); //windows
cb( null, stdout );
}
}
);
};
exports.findCritical = function( url, opts, cb ){
var defaultCb = function( err, output ){
if( err ){
throw new Error( err );
} else {
console.log( output );
}
};
if( typeof url !== "string" ){
throw new TypeError( "URL must be a string" );
}
if( typeof opts === "undefined" && typeof cb === "undefined" ){
opts = {};
cb = defaultCb;
}
if( typeof opts === "function" ){
cb = opts;
opts = {};
}
var width = opts.width || 1200;
var height = opts.height || 900;
var forceInclude = opts.forceInclude || [];
var rules = opts.rules || [];
var tmpfile;
var bufferSize = opts.buffer || DEFAULT_BUFFER_SIZE;
if( !Array.isArray( forceInclude ) ){
throw new Error( "forceInclude must be an array of selectors" );
}
var rulesString = JSON.stringify( rules );
//var MAX_ARG_STRLEN = 131072; // on unix machines, the longest string an argument can be
tmpfile = path.join( os.tmpdir(), "criticalcss-findcritical-rules" + (new Date()).getTime() );
try {
fs.writeFileSync( tmpfile, rulesString );
} catch( e ){
throw e;
}
var execArgs = [
path.resolve( path.join( __dirname, "lib", "criticalrunner.js" ) ),
url,
width,
height,
JSON.stringify( forceInclude ),
tmpfile
];
if( opts.ignoreConsole ){
execArgs.push( "--ignoreConsole" );
}
execFile( phantomJsPath,
execArgs,
{
maxBuffer: bufferSize
},
function(err, stdout, stderr){
if( err ){
console.log("\nSomething went wrong with phantomjs...");
if( stderr ){
err.message = stderr;
}
cb( err, null );
} else {
cb( null, stdout );
}
if( fs.existsSync(tmpfile) ){
fs.unlinkSync(tmpfile);
}
}
);
};
}(typeof exports === "object" && exports || this));