-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
82 lines (68 loc) · 2.08 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
const generator = require('./critical')
const path = require('path')
const pkgJson = require(path.join(__dirname, 'package.json'))
const fs = require('fs-extra')
const postcss = require('postcss')
const postcssrc = require('postcss-load-config')
const atImport = require('./postcss-remove-imports')
const { version } = pkgJson
function processCss (css, file) {
const ctx = {}
const configPath = path.resolve(__dirname, 'postcss.config.js')
return postcssrc(ctx, configPath)
.then((config) => {
const options = { ...config.options }
options.from = undefined
return postcss(config.plugins).use(atImport({})).process(css, options)
})
.catch((err) => {
throw err
})
}
function main (urls, options) {
console.log(`Run generation on: ${urls.join(', ')}`)
let { dimensions, output } = options
dimensions = dimensions || '1300x900'
output = output || './critical.css'
if (!output.endsWith('css')) {
throw new Error(
'Output location needs to be a file path like /var/output/output.css'
)
}
const promises = urls.map((url, index) => {
const fname = `critical-partial-${index}.css`
const promise = generator.generateCritical(
url,
dimensions,
path.join(path.dirname(output), fname)
)
return promise
})
let css
Promise.all(promises)
.then((allCss) => {
console.log(`Processing ${allCss.length} css fragment(s)`)
css = allCss.join(';\n')
processCss(css, output).then((result) => {
console.log(`Generated ${result.css.length} bytes`)
fs.outputFile(output, result.css)
console.log('Done, will shut down')
})
})
.catch((err) => {
console.error(err.name)
console.error(err.message)
})
}
const { Command } = require('commander')
const program = new Command()
program
.version(version)
.arguments('<url...>')
.option('-o, --output <output>', 'Output directory')
.option(
'-d, --dimensions <dimensions>',
'Comma-separated dimension rectangles, like "1300x900,1600x1000"'
)
.action(main)
program.parse(process.args, process.argv)