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

Commit

Permalink
Fixes and refinements
Browse files Browse the repository at this point in the history
* Fix typos around (opts|output).sourceMap(s)
* Fix typos around opts.lowRes(olution)SourceMaps
* Fix typos around inlineSourceMap(s)
* Make exports.inlineSourceMap more visible
* Explicitly list all `opts` keys in one place
* Remove atob dependency
  • Loading branch information
crisptrutski committed Feb 20, 2015
1 parent f319818 commit 12b2133
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion compilers/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports.compile = function(load, opts, loader) {

if (opts.sourceMaps)
options.sourceMaps = 'memory';
if (opts.lowResolutionSourceMaps)
if (opts.lowResSourceMaps)
options.lowResolutionSourceMap = true;

if (load.metadata.sourceMap)
Expand Down
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ var builder = require('./lib/builder');

var path = require('path');

/* Very basic, null-friendly, shallow clone for object attributes only */
function clone(obj) {
obj = obj || {};
var copy = {};
for (var key in obj) {
copy[key] = obj[key];
}
return copy;
}

function processOpts(opts_, outFile) {
var opts = clone(opts_);
var keys = [
'config',
'lowResSourceMaps',
'minify',
'normalize',
'outFile',
'runtime',
'sourceMaps',
'sourceMapContents'
];
var opts = {};
keys.forEach(function(key) {
opts[key] = opts_ && opts_[key];
});
if (outFile) opts.outFile = outFile;
if (opts.sourceMaps == 'inline')
opts.sourceMapContents = true;
Expand Down
14 changes: 8 additions & 6 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,15 @@ function minify(output, fileName, mangle) {
}

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

return asp(mkdirp)(path.dirname(path.resolve(basePath, opts.outFile)))
.then(function() {
if (!output.sourceMap || !opts.sourceMap) return;
if (!output.sourceMap || !opts.sourceMaps) return;
var sourceMapPath = path.resolve(basePath, path.dirname(opts.outFile), sourceMapFile);
return asp(fs.writeFile)(sourceMapPath, output.sourceMap);
})
Expand All @@ -123,11 +124,12 @@ function writeOutputFile(opts, output, basePath) {
});
}

var inlineSourceMaps = exports.inlineSourceMap = function(output) {
exports.inlineSourceMap = inlineSourceMap;
function inlineSourceMap (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
Expand All @@ -142,7 +144,7 @@ exports.writeOutput = function(opts, outputs, baseURL) {
output = minify(output, opts.outFile, opts.mangle);

if (opts.sourceMaps == 'inline') {
output.source = inlineSourceMaps(output);
output.source = inlineSourceMap(output);
output.sourceMap = undefined;
}

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"es6-module-loader": "0.14.0",
"mocha": "~2.0.0",
"chai": "^1.10.0",
"react-tools": "^0.12.1",
"atob": "^1.1.2"
"react-tools": "^0.12.1"
},
"repository": {
"type": "git",
Expand Down
7 changes: 5 additions & 2 deletions test/unit-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var fs = require('fs');
var Builder = require('../index');
var assert = require('chai').assert;
var atob = require('atob');

function atob(str) {
return new Buffer(str, 'base64').toString('binary');
}

var err = function(e) {
setTimeout(function() {
Expand Down Expand Up @@ -57,7 +60,7 @@ describe('Source Maps', function() {
var instance = new Builder('./test/cfg.js');
instance.build(module, null, { sourceMaps: 'inline' })
.then(function(output) {
assert.equal(undefined, output.sourceMaps);
assert.equal(undefined, output.sourceMap);
var source = output.source;
assert.equal(1, source.match(/sourceMappingURL=/g).length);
var lines = output.source.split("\n");
Expand Down

0 comments on commit 12b2133

Please sign in to comment.