Created
April 22, 2017 14:29
-
-
Save psaia/b478c77b5367ffb27385a70ef44a9223 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
#!/usr/bin/env node | |
/** | |
* Process the client side application using babelify and browserify. | |
*/ | |
if (!process.env.CLIENT_PATH || !process.env.SERVER_PATH) { | |
throw 'CLIENT_PATH and SERVER_PATH is required.' | |
} | |
const UglifyJS = require('uglify-js'); | |
const fs = require('fs'); | |
const browserify = require('browserify'); | |
const watchify = require('watchify'); | |
const entry = process.env.CLIENT_PATH + '/js/main.js'; | |
const output = process.env.SERVER_PATH + '/public/js/app.js'; | |
const outputMin = process.env.SERVER_PATH + '/public/js/app.min.js'; | |
console.log('Starting app.js build. Environment: ' + process.env.NODE_ENV); | |
const b = browserify(entry, { | |
debug: true, | |
sourceMaps: true, | |
cache: {}, | |
packageCache: {}, | |
plugin: [process.env.WATCH ? watchify : null], | |
basedir: process.env.CLIENT_PATH | |
}) | |
.transform('babelify', { | |
plugins: [ | |
process.env.SERVER_PATH + '/babel-plugins/babelRelayPlugin', | |
'transform-decorators-legacy', | |
'transform-class-properties' | |
], | |
presets: [ | |
'es2015', | |
'react' | |
] | |
}); | |
function bundle() { | |
const stream = fs.createWriteStream(output); | |
const onComplete = (function() { | |
const begin = Date.now(); | |
return function() { | |
console.log('Build complete in ' + (Date.now() - begin) / 1000 + 'secs'); | |
}; | |
})(); | |
b.bundle().pipe(stream) | |
stream.on('finish', function() { | |
if (process.env.NODE_ENV === 'production') { | |
console.log('Complete, now compressing because production.'); | |
const stream2 = fs.createWriteStream(outputMin); | |
const minified = UglifyJS.minify([output], { | |
compress: { | |
dead_code: true | |
} | |
}); | |
stream2.on('finish', function() { | |
onComplete(); | |
}); | |
stream2.write(minified.code); | |
} else { | |
console.log('All done! (dev mode - not compressing)'); | |
onComplete(); | |
} | |
}); | |
} | |
bundle(); | |
if (process.env.WATCH) { | |
b.on('update', bundle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment