-
Notifications
You must be signed in to change notification settings - Fork 3
/
handlers.js
69 lines (50 loc) · 1.9 KB
/
handlers.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
const path = require('path')
, fs = require('fs-extra')
, getToStaticDirectory = require('refo-directory-to-other-directory')
, getHtmlHandler = require('refo-handle-html')
, bundle = require('bundle-js')
, minifyJS = require('uglify-js').minify
require('hot-module-replacement')()
const getPdfSourceChangeHandler = require('refo-pdf-source-change-handler')
module.exports = ({assetDirectory, siteDirectory, staticDirectory, watchedFileSource, pdfSourceChangeHandler, handlebarsHandler}) => {
String.prototype.toStaticDirectory = getToStaticDirectory(siteDirectory, staticDirectory)
const handleHTML = getHtmlHandler(watchedFileSource)
, handlePdfSourceChange = getPdfSourceChangeHandler(staticDirectory, !!watchedFileSource, pdfSourceChangeHandler)
String.prototype.saveToStaticDirectory = function(html) {
var staticFilePath = this.toStaticDirectory()
fs.writeFile(staticFilePath, html
, () => handlePdfSourceChange(staticFilePath)
)
}
return {
'.html':
async filePath => {
var html = fs.readFileSync(filePath, 'UTF-8')
html = await handleHTML(filePath, html)
filePath.saveToStaticDirectory(html)
}
, '.js': (() => {
var requiredModule = {}
return filePath => {
if (filePath.replace(path.resolve(siteDirectory) + path.sep, '').split(path.sep)[0] == assetDirectory) {
let js = bundle({entry: filePath, disablebeautify: true})
for (let i = 1; i <= 2; i++)
js = minifyJS(js).code
fs.writeFile(filePath.toStaticDirectory(), js)
} else
if (!requiredModule[filePath]) {
requiredModule[filePath] = true
let html
;(async function handleModule(path, firstCall) {
html = require(filePath)
if (firstCall)
module.hot.accept(filePath, handleModule)
html = await handleHTML(filePath, html)
filePath.replace('.js', '.html')
.saveToStaticDirectory(html)
})(undefined, true)
}
}
})()
}
}