Skip to content

Commit

Permalink
Flush sourcemaps if any upon Manifest#compile
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Mar 23, 2014
1 parent 93bfa4b commit fc9bdb3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 48 deletions.
48 changes: 2 additions & 46 deletions lib/mincer/assets/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@


// stdlib
var fs = require('fs');
var path = require('path');
var path = require('path');


// 3rd-party
var _ = require('lodash');
var fstools = require('fs-tools');
var pako = require('pako');
var _ = require('lodash');


// internal
Expand Down Expand Up @@ -212,47 +209,6 @@ getter(Asset.prototype, 'relativePath', function () {
});


/**
* Asset#writeTo(filename, options) -> Void
* - filename (String)
* - options (Object)
*
* Save asset to disk. Automatically gzip content if `options.compress` is true
* or `filename` matches `*.gz` pattern. Throws exception on error.
**/
Asset.prototype.writeTo = function (filename, options) {
var self = this,
mtime = this.mtime,
tempname = filename + '+';

options = options || {};

if (undefined === options.compress && '.gz' === path.extname(filename)) {
options.compress = true;
}

try {
fstools.mkdirSync(path.dirname(filename));

if (options.compress) {
fs.writeFileSync(tempname, new Buffer(pako.gzip(new Uint8Array(self.buffer))));
} else {
fs.writeFileSync(tempname, self.buffer);
}

fs.renameSync(tempname, filename);
fs.utimesSync(filename, mtime, mtime);
} catch (err) {
// Try to remove tmp file on error.
// Don't check if exists, just suppress errors
try { fstools.removeSync(tempname); } catch (_) {}

throw err;
}
};



Asset.prototype.relativizeRootPath = function (pathname) {
pathname = String(pathname);

Expand Down
41 changes: 39 additions & 2 deletions lib/mincer/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var format = require('util').format;
// 3rd-party
var _ = require('lodash');
var fstools = require('fs-tools');
var pako = require('pako');


// internal
Expand Down Expand Up @@ -133,6 +134,37 @@ getter(Manifest.prototype, 'files', function () {
});


// Coerce data to buffer
function toBuffer(data) {
return Buffer.isBuffer(data) ? data : new Buffer(data);
}


// Write data String/Buffer into filename
function write(filename, mtime, data) {
var tempname = filename + '+';

try {
fstools.mkdirSync(path.dirname(filename));
fs.writeFileSync(tempname, toBuffer(data));
fs.renameSync(tempname, filename);
fs.utimesSync(filename, mtime, mtime);
} catch (err) {
// Try to remove tmp file on error.
// Don't check if exists, just suppress errors
try { fstools.removeSync(tempname); } catch (_) {}

throw err;
}
}


// Compress given String or Buffer
function gzip(data) {
return new Buffer(pako.gzip(new Uint8Array(toBuffer(data))));
}


/**
* Manifest#compile(files[, callback]) -> Object
* - files (Array):
Expand Down Expand Up @@ -201,10 +233,15 @@ Manifest.prototype.compile = function (files) {
return;
}

asset.writeTo(target, {});
write(target, asset.mtime, asset.buffer);

if ('bundled' === asset.type) {
asset.writeTo(target + '.gz', {});
write(target + '.gz', asset.mtime, gzip(asset.buffer));
}

if (asset.sourceMap) {
write(target + '.map', asset.mtime, asset.sourceMap);
write(target + '.map.gz', asset.mtime, gzip(asset.sourceMap));
}

self.save();
Expand Down

0 comments on commit fc9bdb3

Please sign in to comment.