-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (52 loc) · 2.47 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
const through = require('through2');
const PluginError = require('plugin-error');
const critical = require('critical');
const fs = require('fs');
const File = require('vinyl');
// consts
const PLUGIN_NAME = 'gulp-shopify-critical';
const generateCriticalHead = critical => {
return critical.reduce((data, page, index) => {
return data += (index === 0) ? `{% if template contains '${page.template}' %}{% include 'critical.${page.template}' %}` : `{% elsif template contains '${page.template}' %}{% include 'critical.${page.template}' %}`;
}, '').concat(`{% else %}<link rel="stylesheet" type="text/css" href="{{ 'bvaccel.css' | asset_url }}">{% endif %}`);
};
const generateCriticalFooter = critical => {
return critical.reduce((data, page, index, array) => {
return data += `template contains '${page.template}'${(index === array.length - 1) ? '' : ' or '}`;
}, '{% if ').concat(`%}<link rel="stylesheet" type="text/css" href="{{ 'bvaccel.css' | asset_url }}">{% endif %}`);
};
const gulpShopifyCritical = function (options) {
if (!options) throw new PluginError(PLUGIN_NAME, 'Missing critical options!');
const stream = through.obj(function (file, enc, cb) {
const option = options.config.critical.find(option => file.relative === `${option.template}.liquid`);
if (option) {
const criticalLiquidHead = generateCriticalHead(options.config.critical);
const criticalLiquidFooter = generateCriticalFooter(options.config.critical);
const criticalFileHead = new File({ path: 'critical.head.liquid', contents: new Buffer(criticalLiquidHead) });
const criticalFileFooter = new File({ path: 'critical.footer.liquid', contents: new Buffer(criticalLiquidFooter) });
this.push(criticalFileHead);
this.push(criticalFileFooter);
const styles = new File({ path: 'styles.css', contents: fs.readFileSync(options.css) });
critical.generate({
src: `http://${options.url}${option.url}?preview_theme_id=${options.theme}`,
css: styles,
minify: true,
width: 1440,
height: 1200
}).then(output => {
const criticalFile = new File({ path: `critical.${option.template}.liquid`, contents: new Buffer(`<style>${output}</style>`) });
this.push(criticalFile);
cb();
}).catch(err => {
console.log(err);
cb();
});
} else {
cb();
}
});
// returning the file stream
return stream;
};
// exporting the plugin main function
module.exports = gulpShopifyCritical;