forked from vincentriemer/io-808
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildTutorial.js
executable file
·98 lines (77 loc) · 2.24 KB
/
buildTutorial.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
#!/usr/bin/env node
var fs = require("fs-extra");
var path = require("path");
var Markdown = require("markdown-it");
var pug = require("pug");
var sass = require("dart-sass");
var uncss = require("uncss");
var CleanCSS = require("clean-css");
var htmlMinify = require("html-minifier").minify;
var stringRequire = function(module, filename) {
module.exports = fs.readFileSync(filename, "utf8");
};
require.extensions[".md"] = stringRequire;
var srcDir = path.resolve("./tutorial");
var outputDir = path.resolve("./out");
function renderHtml(content, css) {
var html = fn({
content: content,
css: css,
livereload: process.env.NODE_ENV !== "production"
});
// minify html
var htmlMin = htmlMinify(html, {
removeComments: true,
collapseWhitespace: true,
processScripts: ["text/javascript"]
});
var htmlPath = path.join(outputDir, "tutorial.html");
var imagesPath = path.join(outputDir, "images");
var exampleFilename = "io808-full-example.json";
// save html file
fs.outputFileSync(htmlPath, htmlMin);
// copy images
fs.copySync("./tutorial/images", imagesPath, { clobber: true });
// copy example savefile
fs.copySync(
path.join(srcDir, exampleFilename),
path.join(outputDir, exampleFilename),
{ clobber: true }
);
}
// CSS
var rawCSS = sass
.renderSync({
file: path.join(srcDir, "sass", "style.scss")
})
.css.toString();
var md = new Markdown({ html: true });
md.use(require("markdown-it-anchor"));
md.use(require("markdown-it-table-of-contents"), { includeLevel: [2, 3] });
md.use(require("markdown-it-decorate"));
md.use(require("markdown-it-video"), {
youtube: { width: 853, height: 480 }
});
// pages
var tutorialMD = require("./tutorial/tutorial.md");
var content = md.render(tutorialMD);
// render
var fn = pug.compileFile(path.join(srcDir, "template.pug"), {});
if (process.env.NODE_ENV === "production") {
var testHtml = fn({ content: content, css: "" });
uncss(
testHtml,
{
ignore: ["canvas"],
raw: rawCSS
},
function(err, unCSS) {
if (err) return console.error(err);
var prodCSS = new CleanCSS({ roundingPrecision: -1 }).minify(unCSS)
.styles;
renderHtml(content, prodCSS);
}
);
} else {
renderHtml(content, rawCSS);
}