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

Commit

Permalink
Merge pull request #63 from systemjs/memory-output
Browse files Browse the repository at this point in the history
Output to memory
  • Loading branch information
crisptrutski committed Feb 9, 2015
2 parents 8bd565d + 386226a commit c2203de
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 32 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
54 changes: 34 additions & 20 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 @@ -92,7 +93,7 @@ function minify(output, fileName, mangle) {
ascii_only: true,
// keep first comment
comments: function(node, comment) {
return comment.line == 1 && comment.col == 0;
return comment.line === 1 && comment.col === 0;
},
source_map: sourceMap
});
Expand All @@ -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);
};
33 changes: 23 additions & 10 deletions 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 Expand Up @@ -51,55 +64,55 @@ builder.loadConfig('./cfg.js')
.then(function() {
return builder.trace('tree/amd-1').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-1.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-2').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-2.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-3').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-3.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-4').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-4.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-5a').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-5a.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-5b').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-5b.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-6a').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-6a.js');
})
});
})

.then(function() {
return builder.trace('tree/amd-6b').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'amd-6b.js');
})
});
})

.then(function() {
return builder.trace('tree/umd').then(function(trace) {
return builder.buildTree(builder.subtractTrees(trace.tree, treeFirst), 'umd.js');
})
});
})

.then(function() {
Expand Down

0 comments on commit c2203de

Please sign in to comment.