Skip to content

Instantly share code, notes, and snippets.

@btakita
Last active January 11, 2016 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btakita/7bc096dbdc9899217479 to your computer and use it in GitHub Desktop.
Save btakita/7bc096dbdc9899217479 to your computer and use it in GitHub Desktop.
browserify,babelify,riotify,uglify,gzip-size build script
#!/usr/bin/env babel-node
var fs = require('fs')
, path = require('path')
, browserify = require('browserify')
, babelify = require('babelify')
, riotify = require('riotify')
, uglify = require('uglify-js2')
, watchify = require('watchify')
, gzipSize = require('gzip-size')
, source = require('vinyl-source-stream')
, gulp = require('gulp')
;
var sourcePath = './path/to/source.js'
, distPath = './public/dist'
, distJsBasename = 'dest.js'
, distJsPath = path.join(distPath, distJsBasename)
, distMinJsPath = './public/dist/dest.min.js'
;
var b = browserify({
entries: [sourcePath],
cache: {},
packageCache: {},
plugin: [watchify]
});
b
.require('riot', {expose: true})
.transform(riotify)
.transform(babelify, {presets: ["es2015"]})
;
b.on('update', bundle);
b.on('log', onLog);
bundle();
function bundle() {
b
.bundle()
.pipe(source(distJsBasename))
.pipe(gulp.dest(distPath))
.on('end', bundleEnd)
;
}
function onLog(msg) {
console.log(msg);
}
function bundleEnd() {
var code = fs.readFileSync(distJsPath).toString();
var ast = uglify.parse(code, {
filename: distJsPath,
topLevel: null
});
var stream = uglify.OutputStream();
ast.print(stream);
var codeMinified = stream.toString();
fs.writeFileSync(distMinJsPath, codeMinified, 'utf8');
var minGzipSize = gzipSize.sync(codeMinified);
console.log('min gzip size: '+minGzipSize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment