Skip to content

Instantly share code, notes, and snippets.

@studioTeaTwo
Created November 9, 2016 08:06
Show Gist options
  • Save studioTeaTwo/75bb9a48dade17e90d09a23859a44016 to your computer and use it in GitHub Desktop.
Save studioTeaTwo/75bb9a48dade17e90d09a23859a44016 to your computer and use it in GitHub Desktop.
build script by pure nodejs
const browserify = require('browserify'),
babelify = require('babelify'),
compressor = require('node-minify'),
fs = require('fs');
process.env.NODE_ENV = 'production';
// for development enviroment
// "watch": "watchify -t babelify ./app/javascripts/components/message/app.js -o ./public/js/build.js -d",
// "build-css": "node-sass --include-path scss ./app/stylesheets/components/message/main.scss ./public/css/style.css",
const srcPath = './app/javascripts/components/',
srcFile = 'app.js',
dstPathOfJs = './public/js/';
const list = fs.readdirSync(srcPath);
list.forEach( (folder) => {
if (folder.indexOf('.') !== 0) {
let appName = folder;
let genFile = 'bundle_' + appName + '.js',
minFile = 'bundle_' + appName + '.min.js';
Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
console.log('start to transpile -> ' + appName);
browserify()
.add(srcPath + folder + '/' + srcFile)
.transform(babelify)
.bundle((err,buf) => {
console.log('transpile done! -> ' + appName);
resolve(buf);
});
});
})
.then((buf) => {
return new Promise((resolve, reject) => {
console.log('start to write -> ' + genFile);
let ws = fs.createWriteStream(dstPathOfJs + genFile);
ws.write(buf, () => {
console.log('write done! -> ' + genFile);
resolve();
});
});
})
.then(() => {
return new Promise((resolve, reject) => {
console.log('start to compress -> ' + minFile);
compressor.minify({
compressor: 'uglifyjs',
input: dstPathOfJs + genFile,
output: dstPathOfJs + minFile,
options: {
warnings: true
},
callback: function(err, min) {
console.log('compress done! -> ' + minFile);
resolve();
}
});
});
})
.then(() => {
console.log('end ' + appName);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment