forked from addyosmani/critical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
232 lines (201 loc) · 6.38 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
'use strict';
const path = require('path');
const fs = require('fs-extra');
const assign = require('lodash/assign');
const defaults = require('lodash/defaults');
const isFunction = require('lodash/isFunction');
const isObject = require('lodash/isObject');
const intersection = require('lodash/intersection');
const keys = require('lodash/keys');
const chalk = require('chalk');
const sourceInliner = require('inline-critical');
const Bluebird = require('bluebird');
const through2 = require('through2');
const PluginError = require('gulp-util').PluginError;
const replaceExtension = require('gulp-util').replaceExtension;
const core = require('./lib/core');
const file = require('./lib/file-helper');
const inliner = require('./lib/inline-styles');
Bluebird.promisifyAll(fs);
/**
* Normalize options
*
* @param opts
*/
function prepareOptions(opts) {
if (!opts) {
opts = {};
}
const options = defaults(opts, {
base: file.guessBasePath(opts),
dimensions: [{
height: opts.height || 900,
width: opts.width || 1300
}]
});
// Set dest relative to base if isn't specivied absolute
if (options.dest && !path.isAbsolute(options.dest)) {
options.dest = path.join(options.base, options.dest);
}
// Set dest relative to base if isn't specivied absolute
if (options.destFolder && !path.isAbsolute(options.destFolder)) {
options.destFolder = path.join(options.base, options.destFolder);
}
// Set options for inline-critical
options.inline = Boolean(options.inline) && assign({
minify: opts.minify || false,
extract: opts.extract || false,
basePath: opts.base || process.cwd()
}, (isObject(options.inline) && options.inline) || {});
// Set penthouse options
options.penthouse = assign({}, {
forceInclude: opts.include || [],
timeout: opts.timeout || 30000,
maxEmbeddedBase64Length: opts.maxImageFileSize || 10240
}, options.penthouse || {});
// Show overwrite warning if penthouse params url, css, witdh or height are present
const checkOpts = intersection(keys(options.penthouse), ['url', 'css', 'width', 'height']);
if (checkOpts.length > 0) {
console.warn(chalk.yellow('Detected presence of penthouse options:'), checkOpts.join(', '));
console.warn(chalk.yellow('These options will be overwritten by critical during the process.'));
}
return options;
}
/**
* Critical path CSS generation
* @param {object} opts Options
* @param {function} cb Callback
* @accepts src, base, width, height, dimensions, dest
* @return {Promise}|undefined
*/
exports.generate = function (opts, cb) {
opts = prepareOptions(opts);
// Generate critical css
let corePromise = core.generate(opts);
// @deprecated
// should be removed in next major release
if (opts.styleTarget) {
corePromise.then(output => {
const file = path.resolve(opts.styleTarget);
const dir = path.dirname(file);
return fs.ensureDirAsync(dir).then(() => {
return fs.writeFileAsync(path.resolve(opts.styleTarget), output);
});
});
}
// Inline
if (opts.inline) {
corePromise = Bluebird.props({
file: file.getVinylPromise(opts),
css: corePromise
}).then(result => {
return sourceInliner(result.file.contents.toString(), result.css, opts.inline);
});
}
// Save to file
if (opts.dest) {
corePromise = corePromise.then(output => {
const file = path.resolve(opts.dest);
const dir = path.dirname(file);
return fs.ensureDirAsync(dir).then(() => {
return fs.writeFileAsync(path.resolve(opts.dest), output);
}).then(() => {
return output;
});
});
}
// Return promise if callback is not defined
if (isFunction(cb)) {
corePromise.catch(err => {
cb(err);
throw new Bluebird.CancellationError();
}).then(output => {
cb(null, output.toString());
}).catch(Bluebird.CancellationError, () => {
}).done();
} else {
return corePromise;
}
};
/**
* Deprecated will be removed in the next version
* @param opts
* @param cb
* @returns {Promise}|undefined
*/
exports.generateInline = function (opts, cb) {
if (!opts.inline) {
opts.inline = true;
}
if (opts.htmlTarget) {
opts.dest = opts.htmlTarget;
} else if (opts.styleTarget) {
// Return error
}
return exports.generate(opts, cb);
};
/**
* Critical path CSS inlining
* @param {object} opts Options
* @param {function} cb Callback
* @accepts src, base, dest
* @deprecated
*/
exports.inline = function (opts, cb) {
opts = opts || {};
cb = cb || function () {};
if (!opts.src || !opts.base) {
throw new Error('A valid source and base path are required.');
}
// Inline the critical path CSS
fs.readFile(path.join(opts.base, opts.src), (err, data) => {
if (err) {
cb(err);
return;
}
const out = inliner(data, opts);
if (opts.dest) {
// Write HTML with inlined CSS to dest
fs.writeFile(path.resolve(opts.dest), out, err => {
if (err) {
cb(err);
return;
}
cb(null, out.toString());
});
} else {
cb(null, out.toString());
}
});
};
/**
* Streams wrapper for critical
*
* @param {object} opts
* @returns {*}
*/
exports.stream = function (opts) {
// Return stream
return through2.obj(function (file, enc, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return this.emit('error', new PluginError('critical', 'Streaming not supported'));
}
const options = assign(opts || {}, {
src: file
});
exports.generate(options, (err, data) => {
if (err) {
return cb(new PluginError('critical', err.message));
}
// Rename file if not inlined
if (!opts.inline) {
file.path = replaceExtension(file.path, '.css');
}
file.contents = Buffer.from(data);
cb(err, file);
});
});
};