Skip to content
Permalink
1e63f07ee9
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 54 lines (45 sloc) 2.32 KB
#!/usr/bin/env node
/*! @license ©2014 Ruben Verborgh - Multimedia Lab / iMinds / Ghent University */
/* Builds the minified HTML and JavaScript. */
var fs = require('fs'),
path = require('path'),
UglifyJS = require("uglify-js"),
UglifyCSS = require('uglifycss');;
var sourceDir =__dirname,
outputDir = ensureExists(path.join(sourceDir, 'build')),
scriptsDir = ensureExists(path.join(outputDir, 'scripts')),
imagesDir = ensureExists(path.join(outputDir, 'images')),
html = fs.readFileSync(path.join(sourceDir, 'index.html'), 'utf8');
// Bundle all script files into a single script
var scriptFiles = [], inlineScript = '', minifiedScript;
html = html.replace(/\s*<script src="([^"]+)"><\/script>\s*/g, function (match, script) {
return scriptFiles.push(path.join(sourceDir, script)), '';
});
if (scriptFiles.length) {
minifiedScript = UglifyJS.minify(scriptFiles, { output: { comments: /^!/ } });
fs.writeFileSync(path.join(scriptsDir, 'ldf-client-jquery-packaged.js'), minifiedScript.code);
html = html.replace('</body>', '<script src="scripts/ldf-client-jquery-packaged.js"></script>\n</body>');
}
// Bundle all inline scripts into a single script
html = html.replace(/\s*<script>([^]*?)<\/script>\s*/g, function (match, script) {
return inlineScript += script + '\n', '';
});
if (inlineScript) {
inlineScript = UglifyJS.minify(inlineScript, { fromString: true }).code;
html = html.replace('</body>', '<script>' + inlineScript + '</script>\n</body>');
}
// Inline the styles
var style = UglifyCSS.processFiles([path.join(sourceDir, 'ldf-client.css')])
html = html.replace(/<link[^>]*ldf-client[^>]*>/, '\n<style>' + style + '</style>');
// Copy auxiliary files
copy(path.join(sourceDir, 'queries.json'), path.join(outputDir, 'queries.json'));
fs.readdirSync(path.join(sourceDir, 'images')).forEach(function (image) {
copy(path.join(sourceDir, 'images', image), path.join(imagesDir, image));
});
copy(path.join(sourceDir, 'favicon.ico'), path.join(outputDir, 'favicon.ico'));
// Write the output HTML
fs.writeFileSync(path.join(outputDir, 'index.html'), html);
// Ensures the path exists
function ensureExists(path) { return fs.existsSync(path) || fs.mkdirSync(path), path; }
// Copies the source file to the destination
function copy(source, destination) { fs.writeFileSync(destination, fs.readFileSync(source)); }