Created
November 9, 2016 08:06
-
-
Save studioTeaTwo/75bb9a48dade17e90d09a23859a44016 to your computer and use it in GitHub Desktop.
build script by pure nodejs
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 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