Skip to content

Instantly share code, notes, and snippets.

@takeshy
Last active December 17, 2015 23:29
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 takeshy/5689027 to your computer and use it in GitHub Desktop.
Save takeshy/5689027 to your computer and use it in GitHub Desktop.
asset pipline & minify & gzip and s3 upload for node.js
require('./lib/compiled');
app.configure('development', function(){
var Mincer = require('mincer');
var environment = new Mincer.Environment();
environment.appendPath(__dirname + '/app/assets/javascripts');
app.use('/assets', Mincer.createServer(environment));
ASSET_JS_PATH = null
});
app.get('/', index);
function index(req, res, next){
res.render('index', { js_path: ASSET_JS_PATH });
}
#!/usr/bin/env node
var fs = require('fs');
var gzip = require('gzip');
var http = require('http');
var Mincer = require('mincer');
var exec = require('child_process').exec
var querystring = require("querystring");
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: ''access_key_id'',
secretAccessKey: ''secret_access_key'',
region: "ap-northeast-1"});
var s3 = new AWS.S3();
var environment = new Mincer.Environment();
environment.appendPath('app/assets/javascripts');
environment.findAsset('application.js').compile(function (err, asset) {
if(err){
console.log(err);
process.exit(1);
}
var params = [
'compilation_level=SIMPLE_OPTIMIZATIONS',
'output_format=json',
'output_info=compiled_code',
'output_info=warnings',
'output_info=errors',
'output_info=statistics'
]
params.push(querystring.stringify({js_code: asset.toString()}));
var param = params.join("&")
var request_header = {
'user-agent': 'node.js',
'content-length': param.length,
'content-type': 'application/x-www-form-urlencoded'
}
var options = {
host : "closure-compiler.appspot.com",
path : "/compile",
method : "POST",
headers : request_header
}
var msg = ""
var req = http.request(options,function(res){
res.setEncoding('utf8');
res.on('data', function (chunk) {
msg += chunk
});
res.on('end', function (chunk) {
try{
var result = JSON.parse(msg);
}catch(e){
console.log(msg);
console.log(e.stack);
process.exit(1);
}
if(result.errors){
console.log(result.errors);
console.log("cancelled above error!");
process.exit(1);
}
var jsFile = __dirname + "/lib/compiled.js";
gzip(result.compiledCode, function(err, compData) {
if (err) throw err;
var params = {
Bucket: ''bucket_name'',
Key: asset.digestPath + ".jgz",
Body: compData,
ACL:'public-read',
ContentEncoding: "gzip",
ContentType: "application/x-javascript" };
s3.client.putObject(params,function(err,data) {
if (err) throw err;
var url = "https://s3-ap-northeast-1.amazonaws.com/" +
''bucket_name'' + "/" + asset.digestPath + ".jgz";
console.log("It\'s post! " + url);
fs.writeFile(jsFile, "ASSET_JS_PATH='" + url + "';", function (err) {
if (err) throw err;
console.log('SUCCESS');
});
});
});
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(param);
req.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment