Created
October 11, 2015 19:36
-
-
Save jessehattabaugh/47403899c767f35aa3ed to your computer and use it in GitHub Desktop.
build.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const argv = require('minimist')(process.argv.slice(2)); | |
const config = require('./config'); | |
/* Here's what this build script does every time you run it | |
*****************************************************************************/ | |
clean(); | |
html(); | |
css(); | |
js(); | |
console.info("source files built"); | |
/* If the --watch argument is passed, set up file watcher to re-run tasks | |
*****************************************************************************/ | |
if (argv.watch) { | |
const chokidar = require('chokidar'); | |
chokidar.watch(path.join(config.src, '**/*.jade')) | |
.on('change', redo(html)); | |
chokidar.watch(path.join(config.src, '*.scss')) | |
.on('change', redo(css)); | |
chokidar.watch([path.join(config.src, '**/*.js')]) | |
.on('change', redo(js)); | |
function redo(fn) { | |
return function (path, stat) { | |
if (stat.size) fn(); | |
}; | |
} | |
console.info("watchers started"); | |
} | |
/* Remove old build products | |
*****************************************************************************/ | |
function clean() { | |
const sh = require('shelljs'); | |
sh.rm('-rf', config.www); | |
sh.mkdir(config.www); | |
console.info('build directory cleaned'); | |
} | |
/* Build the index.html file using Jade | |
*****************************************************************************/ | |
function html() { | |
const jade = require('jade'); | |
var out = jade.renderFile( | |
path.join(config.src, 'main.jade'), { | |
cache: false, | |
pretty: !config.min | |
} | |
); | |
fs.writeFile(path.join(config.www, 'index.html'), out); | |
console.info('html built'); | |
} | |
/* Build the index.css file using PostCSS | |
*****************************************************************************/ | |
function css() { | |
const postcss = require('postcss'); | |
var cssIn = path.join(config.src, 'main.scss'); | |
var cssOut = path.join(config.www, 'index.css'); | |
var processes = [ | |
require('precss')(), | |
require('autoprefixer')('> 1%') | |
]; | |
if (config.min) processes.push(require('cssnano')); | |
var src = fs.readFileSync(cssIn, 'utf8'); | |
postcss(processes) | |
.process(src, {parser: require('postcss-scss'), from: cssIn, to: cssOut}) | |
.then(function (res) { | |
if (res.css) fs.writeFileSync(cssOut, res.css); | |
else console.warn("PostCSS result is empty"); | |
if (res.map) fs.writeFileSync(path.join(config.www, 'index.css.map'), res.map); | |
console.info('css built'); | |
}) | |
.catch(function (err) { | |
console.error(err); | |
}); | |
} | |
/* Build the index.js file using Browserify | |
*****************************************************************************/ | |
function js() { | |
const browserify = require('browserify'); | |
var b = browserify(); | |
b.add(path.join(config.src, 'main.js')); | |
b.transform('babelify', { blacklist: ["strict"] }); | |
if (config.min) b.transform({global: true}, 'uglifyify'); | |
var dest = fs.createWriteStream(path.join(config.www, 'index.js')); | |
try { | |
b.bundle().pipe(dest); | |
console.info('js built'); | |
} catch (e) { | |
console.error(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment