-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
103 lines (92 loc) · 1.69 KB
/
build.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
var klaw = require('klaw-sync')
var fs = require('fs')
var { compose, map } = require('ramda')
/**
* apply filter ignore chunk files
*/
const filter = file => /cc.js$/.test(file.path) === false
/*
* get files
*
*
*
*/
// TODO: allow output directory to be specified on command line
const results = klaw('./dist', {
nodir: true,
filter
})
/**
* combine bulid steps
*/
const build = compose(
writeHtmlFile,
createOutputBundle
)
/**
* run component render functions
* and write html files
*/
map(build, results)
/**
* CodeBundle object
* @typedef {object} CodeBundle
* @property {object} css
* @property {string} head
* @property {string} html
*/
/**
* html template wrapper function
*
* @param {CodeBundle}
*
* @returns {string}
*
*/
function htmlTemplate ({html, head, css}) {
return `
<!doctype html>
<html>
<head>
${head}
${css.code !== '' ? `<style>${css.code}</style>` : ''}
</head>
<body>
${html}
</body>
</html>
`
}
/**
* @typedef {object} FileObject
* @property {string} path
*/
/**
* @typedef {object} OutputBundle
* @property {string} js - path of js file
* @property {string} file - path of html file
* @property {string} content - html file contents
*/
/**
* create html document and return OutputBundle
*
* @param {fileObject}
*
* @returns {OutputBundle}
*/
function createOutputBundle({ path }) {
const content = require(path).default.render()
const file = path.replace('.js', '.html')
return {js: path, file, content}
}
/**
* write html document to file
*
* @param {OutputBundle}
*
* @returns {null}
*/
function writeHtmlFile({js, file, content}) {
fs.writeFileSync(file, htmlTemplate(content))
fs.unlinkSync(js)
}