-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (47 loc) · 1.52 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
const fs = require('fs')
const path = require('path')
const util = require('util')
const posthtml = require('posthtml')
const posthtmlExtend = require('posthtml-extend')
const posthtmlInclude = require('posthtml-include')
const posthtmlExpressions = require('posthtml-expressions')
const minify = require('./minify')
const is = require('@magic/types')
const log = require('@magic/log')
const exists = util.promisify(fs.exists)
const POST_HTML = async ({ buffer, config }) => {
const { ENV } = config
config.HTML_DIR = config.HTML_DIR || '/'
config.WEB_ROOT = ENV === 'production' && config.WEB_ROOT ? config.WEB_ROOT : '/'
try {
if (is.empty(buffer)) {
throw new Error('POST_HTML: Missing argument: { buffer } missing in first argument')
}
if (!is.string(buffer)) {
if (is.buffer(buffer)) {
buffer = buffer.toString()
} else {
throw new Error('POST_HTML: buffer has to be a string or buffer')
}
}
const plugins = [
posthtmlExtend({ root: config.HTML_DIR }),
posthtmlInclude({ root: config.HTML_DIR }),
]
const varFilePath = path.join(config.HTML_DIR, 'variables.js')
let locals = config
if (await exists(varFilePath)) {
locals = {
...locals,
...require(varFilePath),
}
}
plugins.push(posthtmlExpressions({ locals }))
const html = await posthtml(plugins).process(buffer /*, options */)
const minified = minify(html.html)
return minified
} catch (e) {
throw e
}
}
module.exports = POST_HTML