-
-
Save Raynos/7c7db71ee1626b0dd947 to your computer and use it in GitHub Desktop.
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
var uglifyJS = require('uglify-js'); | |
var src = require('glob-stream').create; | |
var watch = require('glob-watcher'); | |
var through = require('through'); | |
var fs = require('fs'); | |
var path = require('path'); | |
// uglify -.- wtf. make stream kind of. | |
function uglify(fileName) { | |
var stream = through() | |
fs.readFile(fileName, function (err, file) { | |
if (err) { | |
return stream.emit('error', err) | |
} | |
var payload | |
try { | |
payload = uglifyJS.minify(String(file), { fromString: true }).code | |
} catch (err) { | |
return stream.emit('error', err) | |
} | |
stream.end(payload) | |
}) | |
return stream | |
} | |
function dest(baseDir) { | |
return through(function (file) { | |
file.contents.pipe(fs.createWriteStream( | |
path.join(baseDir, path.filename(file.path)) | |
)) | |
}) | |
} | |
var tasks = { | |
'scripts': function () { | |
src(['client/js/**/*.js', '!client/js/vendor/**']) | |
.pipe(through(function (file) { | |
return { contents: uglify(file.path), path: file.path }; | |
})) | |
.pipe(dest('build/js')) | |
src('client/js/vendor/**') | |
.pipe(dest('build/js/vendor')) | |
}, | |
'copy': function () { | |
src('client/img/**') | |
.pipe(dest('build/img')) | |
src('client/css/**') | |
.pipe(dest('build/css')) | |
src('client/*.html') | |
.pipe(dest('build')) | |
}, | |
'default': function () { | |
tasks.scripts() | |
tasks.copy() | |
watch('client/js/**', tasks.scripts) | |
watch([ | |
'client/img/**', | |
'client/css/**', | |
'client/*.html' | |
], tasks.copy) | |
} | |
} | |
var cmd = process.argv[3] || 'default'; | |
tasks[cmd](); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment