106 lines
5.6 KiB
JavaScript
106 lines
5.6 KiB
JavaScript
/*const fs = require('fs')
|
|
const path = require('path')
|
|
const zopfli = require('node-zopfli')
|
|
const brotli = require('iltorb')
|
|
const mkdirp = require('mkdirp')
|
|
const minify = require('html-minifier').minify
|
|
|
|
const options = {
|
|
collapseBooleanAttributes: true,
|
|
minifyURLs: true,
|
|
removeComments: true,
|
|
removeEmptyAttributes: true,
|
|
removeOptionalTags: true,
|
|
removeRedundantAttributes: true,
|
|
removeScriptTypeAttributes: true,
|
|
removeStyleLinkTypeAttributes: true,
|
|
sortAttributes: true,
|
|
sortClassName: true,
|
|
useShortDoctype: true,
|
|
collapseWhitespace: true,
|
|
collapseBooleanAttributes: true,
|
|
collapseInlineTagWhitespace: true,
|
|
conservativeCollapse: true,
|
|
preserveLineBreaks: false,
|
|
removeTagWhitespace: true
|
|
}
|
|
|
|
const basepath = __dirname + '/../../'
|
|
const dist_path = 'dist/'
|
|
const file = 'index.html'
|
|
|
|
fs.readFile(path.normalize(basepath + file), (err, buf) => {
|
|
const destination = path.normalize(basepath + dist_path + file)
|
|
const result = minify(String(buf), options)
|
|
// create directories if not present
|
|
mkdirp(path.dirname(destination), err => {
|
|
// write uncompressed file
|
|
fs.writeFile(destination, result, err => {
|
|
if (err) return console.error(err)
|
|
})
|
|
// write zopfli compressed file
|
|
zopfli.gzip(new Buffer(result), (err, output) => {
|
|
fs.writeFile(destination + '.gz', output, err => {
|
|
if (err) return console.error(err)
|
|
})
|
|
})
|
|
// write brotli compressed file
|
|
brotli.compress(new Buffer(result), (err, output) => {
|
|
fs.writeFile(destination + '.br', output, err => {
|
|
if (err) return console.error(err)
|
|
})
|
|
})
|
|
})
|
|
})*/
|
|
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const mkdirp = require('mkdirp')
|
|
const Handlebars = require('handlebars')
|
|
|
|
/* Templates */
|
|
const fetch_templates = async (template_files) => Promise.all(template_files.map(template => new Promise((resolve, reject) => fs.readFile(template.path, (err, body) => err ? reject(err) : resolve(body+'')))))
|
|
const get_template_files = config => new Promise((resolve, reject) => fs.readdir(path.join(config.basepath, config.template_path), (err, files) => err ? reject(err) : resolve(files.map(file => path.join(config.basepath, config.template_path, file)).filter(file => fs.statSync(file).isFile()).filter(file => path.extname(file) === '.hbs').map(file => Object.assign({path: file, name: path.basename(file).replace(path.extname(file), '')})))))
|
|
const load_templates = async config => {
|
|
const template_files = await get_template_files(config)
|
|
const template_content = await fetch_templates(template_files)
|
|
const templates = template_files.map((template, idx) => Object.assign({}, template, {content: template_content[idx], compiled: Handlebars.compile(template_content[idx])}))
|
|
return templates
|
|
}
|
|
|
|
/* Partials */
|
|
const fetch_partials = async (partial_files) => Promise.all(partial_files.map(partial => new Promise((resolve, reject) => fs.readFile(partial.path, (err, body) => err ? reject(err) : resolve(body+'')))))
|
|
const get_partial_files = config => new Promise((resolve, reject) => fs.readdir(path.join(config.basepath, config.template_path, config.partials_path), (err, files) => err ? reject(err) : resolve(files.map(file => path.join(config.basepath, config.template_path, config.partials_path, file)).filter(file => fs.statSync(file).isFile()).filter(file => path.extname(file) === '.hbs').map(file => Object.assign({path: file, name: path.basename(file).replace(path.extname(file), '')})))))
|
|
const load_partials = async config => {
|
|
const partial_files = await get_partial_files(config)
|
|
const partial_content = await fetch_partials(partial_files)
|
|
const partials = partial_files.map((partial, idx) => Object.assign({}, partial, {content: partial_content[idx]}))
|
|
partials.forEach(partial => Handlebars.registerPartial(partial.name, partial.content))
|
|
return partials
|
|
}
|
|
|
|
/* Compile */
|
|
const create_page = (config, result, templates, page) => templates.filter(template => template.name === page.template).map(template => template.compiled)[0]({content: result.content, meta: generate_meta(config, result, page)})
|
|
const generate_pages = (config, content, templates) => pages = config.html.pages.map(page => Object.assign({}, page, {content: inject_scripts(create_page(config, content, templates, page), config, content, page)}))
|
|
const generate_meta = (config, result, page) => Object.assign({}, {icons: result.icons ? result.icons.html : ''}, {styles: generate_styles(config, result, page)})
|
|
const generate_styles = (config, result, page) => page.css.map(style => result.css.filter(css => css.name === style + '.css')[0].html).join('')
|
|
const generate_scripts = (config, result, page) => page.js.map(script => result.javascript.filter(js => js.name === script + '.js')[0].html).join('')
|
|
const inject_scripts = (content, config, result, page) => content.replace('</body>', generate_scripts(config, result, page) + '</body>')
|
|
|
|
/* Output */
|
|
const write_page = (config, page) => {
|
|
const destination = path.join(config.basepath, config.dist_path, page.name + '.html')
|
|
return new Promise((resolve, reject) => mkdirp(path.dirname(destination), err => err ? reject(err) : fs.writeFile(destination, page.content, err => err ? reject(err) : resolve())))
|
|
}
|
|
const write_pages = async (config, pages, templates, partials) => {
|
|
await Promise.all(pages.map(write_page.bind(null, config)))
|
|
return {config, pages, templates, partials}
|
|
}
|
|
|
|
|
|
module.exports = async (config, result) => {
|
|
const partials = await load_partials(config)
|
|
const templates = await load_templates(config)
|
|
const pages = generate_pages(config, result, templates)
|
|
return await write_pages(config, pages, templates, partials)
|
|
} |