Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Support in-memory building
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski committed Feb 9, 2015
1 parent 85749ef commit 386226a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/excluded.js
test/sfx.js
test/tree-build.js
test/tree-build.js.map
test/memory-test
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports.buildTree = function(tree, outFile, opts) {
return Promise.resolve(compileLoad(load, opts))
.then(outputs.push.bind(outputs));
}))
.then(builder.writeOutputFile.bind(this, opts, outputs, loader.baseURL));
.then(builder.writeOutput.bind(this, opts, outputs, loader.baseURL));
};

exports.buildSFX = function(moduleName, outFile, opts) {
Expand Down Expand Up @@ -162,7 +162,7 @@ exports.buildSFX = function(moduleName, outFile, opts) {
outputs.push("});");
})
.then(function() {
return builder.writeOutputFile(opts, outputs, loader.baseURL);
return builder.writeOutput(opts, outputs, loader.baseURL);
});
};

Expand Down
52 changes: 33 additions & 19 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var saucy = require('./sourcemaps');
var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
var asp = require('rsvp').denodeify;
var rsvp = require('rsvp');
var asp = rsvp.denodeify;
var uglify = require('uglify-js');

function countLines(str) {
Expand Down Expand Up @@ -36,7 +37,7 @@ function processOutputs(outputs) {
if (map) {
sourceMapsWithOffsets.push([offset + offset_, map]);
}
}
}
else if (typeof output == 'string') {
source = output;
}
Expand All @@ -59,7 +60,7 @@ function createOutput(outputs, outFile, baseURL, createSourceMaps) {

if (createSourceMaps && outputObj.sourceMapsWithOffsets.length)
sourceMap = saucy.concatenateSourceMaps(outFile, outputObj.sourceMapsWithOffsets, baseURL);

var output = outputObj.sources.join('\n');

return {
Expand Down Expand Up @@ -101,31 +102,44 @@ function minify(output, fileName, mangle) {
return output;
}


exports.writeOutputFile = function(opts, outputs, baseURL) {
// remove 'file:' part
var basePath = baseURL.substr(5);

opts.outFile = path.relative(basePath, path.resolve(opts.outFile));

var output = createOutput(outputs, opts.outFile, baseURL, opts.sourceMaps);

if (opts.minify)
output = minify(output, opts.outFile, opts.mangle);

function writeOutputFile(opts, output, basePath) {
if (opts.sourceMaps) {
var sourceMapFile = path.basename(opts.outFile) + '.map';
output.source += '\n//# sourceMappingURL=' + sourceMapFile;
}

return asp(mkdirp)(path.dirname(path.resolve(basePath, opts.outFile)))
.then(function() {
if (!opts.sourceMaps)
return;
return asp(fs.writeFile)(path.resolve(basePath, path.dirname(opts.outFile), sourceMapFile), output.sourceMap);
if (!opts.sourceMaps) return;
var sourceMapPath = path.resolve(basePath, path.dirname(opts.outFile), sourceMapFile);
return asp(fs.writeFile)(sourceMapPath, output.sourceMap);
})
.then(function() {
return asp(fs.writeFile)(path.resolve(basePath, opts.outFile), output.source);
var sourcePath = path.resolve(basePath, opts.outFile);
return asp(fs.writeFile)(sourcePath, output.source);
});
}

exports.inlineSourceMap = function(output) {
return output.source +
'\n//# sourceMappingURL=data:application/json;base64,' +
new Buffer(output.sourceMap.toString()).toString('base64');
};

exports.writeOutput = function(opts, outputs, baseURL) {
// remove 'file:' part
var basePath = baseURL.substr(5);

if (opts.outFile)
opts.outFile = path.relative(basePath, path.resolve(opts.outFile));

var output = createOutput(outputs, opts.outFile, baseURL, opts.sourceMaps);

if (opts.minify)
output = minify(output, opts.outFile, opts.mangle);

if (opts.outFile)
return writeOutputFile(opts, output, basePath);
else
return rsvp.resolve(output);
};
15 changes: 14 additions & 1 deletion test/run-build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
var builder = require('../index');
var inline = require('../lib/builder').inlineSourceMap;
var fs = require('fs');

var err = function(e) {
setTimeout(function() {
throw e;
});
}
};

console.log('Running in-memory build...');
builder.loadConfig('./cfg.js')
.then(function() {
builder.build('tree/first', null, { sourceMaps: true, minify: true })
.then(function(output) {
fs.writeFile('memory-test', inline(output));
console.log('Wrote in-memory build to ./memory-test');
})
.catch(err);
});

console.log('Running a multi-format build...');

Expand Down

0 comments on commit 386226a

Please sign in to comment.