2
0
mirror of https://github.com/Y2Z/monolith synced 2024-11-05 06:01:10 +00:00
monolith/compactor.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-02-20 07:51:24 +00:00
#!/usr/bin/env node
'use strict'
var path = require('path')
var jsdom = require('jsdom')
var options = require('./options.js');
var functions = require('./functions.js')
var absoluteURLPath = functions.absoluteURLPath,
isURL = functions.isURL,
base64 = functions.base64,
resolve = functions.resolve,
retrieveFile = functions.retrieveFile
var modules = [
// 1. CSS
require('./modules/css.js').parser,
// 2. JS
require('./modules/js.js').parser,
// 3. images
require('./modules/img.js').parser,
// 4. favicon
require('./modules/favicon.js').parser,
// 5. anchors
require('./modules/anchors.js').parser,
]
function monolith (targetDocumentPath, callback) {
// Determine the absolute initial document path
var absBasePath = isURL(targetDocumentPath)
? absoluteURLPath(targetDocumentPath)
: path.dirname(path.resolve(targetDocumentPath))
absBasePath += '/' // Append trailing slash
// Retrieve the root document to use as a base
var rootFileContent = retrieveFile(absBasePath, targetDocumentPath)
// Convert the target document into a DOM tree
jsdom.env(rootFileContent, [], function (err, window) {
for (var i = 0, ilen = modules.length; i < ilen; i++)
modules[i](window, absBasePath)
var result = window.document.documentElement.innerHTML
callback(null, options.outputFinalResultAsBase64 ? base64(result) : result)
})
}
module.exports = monolith