Permalink
Browse files

Working source maps with messy code

  • Loading branch information...
1 parent eff389c commit 9094d4a3e228cf6013eed5ee4ef8df37b51b353d @download13 committed May 5, 2012
Showing with 81 additions and 50 deletions.
  1. +1 −1 lib/minify-closure.js
  2. +4 −2 lib/source-build.js
  3. +3 −3 lib/source-package.js
  4. +43 −17 lib/sourcemap.js
  5. +30 −26 lib/write.js
  6. +0 −1 package.json
View
@@ -19,7 +19,7 @@ function minify(source, cb) {
child.on('exit', function() {
fs.readFile(mappath, 'utf8', function(err, mapdata) {
- fs.unlink(mappath);
+ fs.unlinkSync(mappath);
cb(null, minified, mapdata);
});
});
View
@@ -54,8 +54,6 @@ var async = require('async')
, addPackage: function (srcPackage) { // add a SourcePackage object for each package
this.packages.push(srcPackage)
- srcPackage.asString()
- this.sourcesMap[srcPackage.filename] = srcPackage.fileContents;
}
, asString: function (options, callback) {
@@ -70,6 +68,10 @@ var async = require('async')
}).join(' ')
}
+ this.packages.forEach(function(v, i) {
+ this.sourcesMap[v.filename] = v.fileContents;
+ }, this);
+
template.generateSource(templateFile, data, function (err, source) {
if (err) return callback(err) // wrapped in template.js
if (options.type === 'minified') minify.minify(source, callback)
View
@@ -81,7 +81,6 @@ var fs = require('fs')
// utility to read multiple files in order and append them
, loadFilesAsString: function (root, files, callback) {
- var self = this;
if (!Array.isArray(files)) files = [ files ]
if (!files.length || (files.length == 1 && files[0] == 'noop')) return callback()
@@ -93,8 +92,6 @@ var fs = require('fs')
file = path.join(root, file).replace(/(\.js)?$/, '.js')
fs.readFile(file, 'utf-8', function (err, contents) {
if (err) return callback(new FilesystemError(err))
- self.filename = file;
- self.fileContents = contents;
callback.apply(null, arguments)
})
}
@@ -141,6 +138,9 @@ var fs = require('fs')
, handleSourceData = function (err, sources) {
if (err) return callback(err)
+
+ this.filename = path.join(root, mainSources).replace(/(\.js)?$/, '.js')
+ this.fileContents = sources.main;
generateSource(
this.isRoot
View
@@ -6,9 +6,20 @@ function SourceMap(data) {
this.sourceRoot = data.sourceRoot;
this.file = data.file;
+ var prev = [0, 0, 0, 0, 0];
this.groups = data.mappings.split(';').map(function(group) {
+ prev[0] = 0;
return group.split(',').map(function(segment) {
segment = decodeVLQStr(segment);
+
+ segment = segment.map(function(v, i) { // Stabby function would really help here
+ return v + prev[i];
+ });
+ prev = prev.map(function(v, i) {
+ v = segment[i];
+ return (v != null && !isNaN(v)) ? v : prev[i];
+ });
+
var r = {};
for(var i = 0; i < segment.length; i++) {
r['field' + (i + 1)] = segment[i];
@@ -21,34 +32,49 @@ SourceMap.prototype = {
translate: function(offsetMap) {
var sources = this.sources = Object.keys(offsetMap);
var groups = this.groups;
+ var self = this;
- for(var k = 0; k < groups.length; k++) {
- var segments = groups[k];
- for(var i = 0; i < segments.length; i++) {
- var segment = segments[i];
- var enderline = segment.field3;
-
- for(var j = 0; j < sources.length; j++) {
- var current = offsetMap[sources[j]];
- var next = offsetMap[sources[j + 1]] || Infinity; // It had better match...
-
- if(enderline >= current && enderline <= next) {
- segment.field3 -= current;
- segment.field2 = j;
+ sources.forEach(function(sourceName, sourceIndex) {
+ var source = offsetMap[sourceName];
+ var offset = source.offset;
+ var lines = source.lines;
+
+ groups.forEach(function(segments, minline) {
+ segments.forEach(function(segment) {
+ if(segment.field3 == null) {
+ return;
}
- }
- }
- }
+ var tmp = segment.field3 - offset;
+ if(tmp >= 0 && tmp < lines) {
+ //console.log(sourceName, offset, lines, tmp + ' <- ' + segment.field3, (self.names[segment.field5] || segment.field5) + ' <- ' + segment.field4);
+ segment.field3 = tmp;
+ segment.field2 = sourceIndex;
+ }
+ });
+ });
+ });
},
serialize: function() {
+ var prev = [0, 0, 0, 0, 0];
var mappings = this.groups.map(function(group) {
+ prev[0] = 0;
return group.map(function(segment) {
var r = [];
var len = Object.keys(segment).length;
for(var i = 0; i < len; i++) {
r.push(segment['field' + (i + 1)]);
}
- return encodeVLQArr(r);
+ segment = r;
+
+ var saved = segment;
+ segment = segment.map(function(v, i) {
+ return v - prev[i];
+ });
+ prev = saved.map(function(v, i) {
+ return (v != null && !isNaN(v)) ? v : prev[i];
+ });
+
+ return encodeVLQArr(segment);
}).join(',');
}).join(';');
View
@@ -35,39 +35,43 @@ var fs = require('fs')
, util = require('./util')
, FilesystemError = require('./errors').FilesystemError
- , writeFile = function (file, sourcesMap, callback, err, data, mapdata) {
- if (err) return callback(err) // wrapped in source-build.js
-
- var diff = require('diff');
- var offsetMap = {}; // TODO: Where do I get this again?
- for(var filename in sourcesMap) {
- // TODO: mess around with diff to figure out how to apply it to this
- }
- var sm = new require('source-map')(mapdata);
- sm.translate(offsetMap);
- mapdata = sm.serialize();
-
+ , writeFile = function (file, data, callback) {
fs.writeFile(file, data, 'utf-8', function (err) {
if (err) return callback(new FilesystemError(err))
callback.apply(null, arguments)
})
}
- , writePlainFile = function (options, sourceBuild, out, callback) {
- var filename = util.getOutputFilenameFromOptions(options)
- sourceBuild.asString({ type: 'plain' }, writeFile.bind(null, filename, sourceBuild.sourcesMap, callback))
- }
-
- , writeMinifiedFile = function (options, sourceBuild, out, callback) {
- var filename = util.getOutputFilenameFromOptions(options).replace(/(\.min)?\.js/, '.min.js')
- sourceBuild.asString({ type: 'minified' }, writeFile.bind(null, filename, sourceBuild.sourcesMap, callback))
- }
-
, write = function (options, sourceBuild, out, callback) {
- async.parallel([
- writePlainFile.bind(null, options, sourceBuild, out)
- , writeMinifiedFile.bind(null, options, sourceBuild, out)
- ], callback)
+ sourceBuild.asString({ type: 'plain' }, function(err, source) {
+ var filename = util.getOutputFilenameFromOptions(options)
+ var nonMinSource = source;
+
+ writeFile(filename, source, function() {
+ var filename = util.getOutputFilenameFromOptions(options).replace(/(\.min)?\.js/, '.min.js')
+ sourceBuild.asString({ type: 'minified' }, function(err, source, sourceMapSeg2) {
+ var sourcesMap = sourceBuild.sourcesMap;
+ var offsetMap = {};
+
+ for(var sourcePath in sourcesMap) {
+ var relpath = path.relative('.', sourcePath);
+ var start = nonMinSource.indexOf(sourcesMap[sourcePath]);
+ var offset = nonMinSource.substr(0, start).split('\n').length - 1;
+ offsetMap[relpath] = {offset: offset, lines: sourcesMap[sourcePath].split('\n').length};
+ }
+
+ var SourceMap = require('./sourcemap');
+ var sm = new SourceMap(sourceMapSeg2);
+ sm.translate(offsetMap);
+ var sourceMap = sm.serialize();
+
+ source += '//@ sourceMappingURL=ender.map.js'; // For demo purposes
+ writeFile(filename, source, function() {
+ writeFile('ender.map.js', sourceMap, callback);
+ });
+ })
+ })
+ })
}
module.exports.write = write
View
@@ -23,7 +23,6 @@
, "colors-tmpl": ">= 0.0.3"
, "xregexp": ">= 1.5.3"
, "errno": ">= 0.0.1"
- , "diff": ">= 1.0.3"
}
, "directories": {
"lib": "./lib"

0 comments on commit 9094d4a

Please sign in to comment.