[Pkg-javascript-commits] [node-browser-pack] 26/141: adding sourceMappingURL support

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:23:21 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-browser-pack.

commit 2d631596ad26a2bc93e6095967f29e7c1e7d9a33
Author: Thorsten Lorenz <thlorenz at gmx.de>
Date:   Sun Mar 3 16:17:40 2013 -0500

    adding sourceMappingURL support
---
 .gitignore                   |   1 +
 example/sourcemap/input.json |  15 ++++++
 example/sourcemap/output.js  |   6 +++
 index.js                     |  37 ++++++++++++--
 package.json                 |   3 +-
 test/source-maps.js          | 113 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 170 insertions(+), 5 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/example/sourcemap/input.json b/example/sourcemap/input.json
new file mode 100644
index 0000000..0e1d2ed
--- /dev/null
+++ b/example/sourcemap/input.json
@@ -0,0 +1,15 @@
+[
+  {
+    "id": "a1b5af78",
+    "source": "console.log(require('./foo')(5))",
+    "deps": { "./foo": "b8f69fa5" },
+    "entry": true,
+    "sourceFile": "wunder/bar.js"
+  },
+  {
+    "id": "b8f69fa5",
+    "source": "module.exports = function (n) { return n * 111 }",
+    "deps": {},
+    "sourceFile": "foo.js"
+  }
+]
diff --git a/example/sourcemap/output.js b/example/sourcemap/output.js
new file mode 100644
index 0000000..e6f4b27
--- /dev/null
+++ b/example/sourcemap/output.js
@@ -0,0 +1,6 @@
+(function(e,t,n,r){function i(r){if(!n[r]){if(!t[r]){if(e)return e(r);throw new Error("Cannot find module '"+r+"'")}var s=n[r]={exports:{}};t[r][0](function(e){var n=t[r][1][e];return i(n?n:e)},s,s.exports)}return n[r].exports}for(var s=0;s<r.length;s++)i(r[s]);return i})(typeof require!=="undefined"&&require,{"a1b5af78":[function(require,module,exports){
+console.log(require('./foo')(5))
+},{"./foo":"b8f69fa5"}],"b8f69fa5":[function(require,module,exports){
+module.exports = function (n) { return n * 111 }
+},{}]},{},["a1b5af78"])
+//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJ3dW5kZXIvYmFyLmpzIiwiZm9vLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTs7QUNBQSJ9
\ No newline at end of file
diff --git a/index.js b/index.js
index 44b7b66..078255c 100644
--- a/index.js
+++ b/index.js
@@ -6,11 +6,20 @@ var uglify = require('uglify-js');
 var fs = require('fs');
 var path = require('path');
 
+var createGenerator = require('inline-source-map');
+
 var prelude = (function () {
     var src = fs.readFileSync(path.join(__dirname, 'prelude.js'), 'utf8');
-    return uglify(src) + '(typeof require!=="undefined"&&require,{'
+    return uglify(src) + '(typeof require!=="undefined"&&require,{';
 })();
 
+function newlinesIn(src) {
+  if (!src) return 0;
+  var newlines = src.match(/\n/g);
+
+  return newlines ? newlines.length : 0;
+}
+
 module.exports = function (opts) {
     if (!opts) opts = {};
     var parser = opts.raw ? through() : JSONStream.parse([ true ]);
@@ -21,20 +30,38 @@ module.exports = function (opts) {
     var entries = [];
     var order = []; 
     
+    var lineno = 1 + newlinesIn(prelude);
+    var generator;
+
     return duplexer(parser, output);
     
+    function addMappings(row) {
+      generator = generator || createGenerator();
+      var offset = { line: lineno, column: 0 };
+
+      if (row.mappings && row.mappings.length)
+          generator.addMappings(row.sourceFile, row.mappings, offset);
+      else 
+          generator.addGeneratedMappings(row.sourceFile, row.source, offset);
+    }
+
     function write (row) {
         if (first) this.queue(prelude);
         
-        this.queue([
+        if (row.sourceFile) addMappings(row);
+
+        wrappedSource = [
             (first ? '' : ','),
             JSON.stringify(row.id),
             ':[',
-            'function(require,module,exports){' + row.source + '\n}',
+            'function(require,module,exports){\n' + row.source + '\n}',
             ',',
             JSON.stringify(row.deps || {}),
             ']'
-        ].join(''));
+        ].join('');
+
+        this.queue(wrappedSource);
+        lineno += newlinesIn(wrappedSource);
         
         first = false;
         if (row.entry && row.order !== undefined) {
@@ -47,6 +74,8 @@ module.exports = function (opts) {
         if (first) this.queue(prelude);
         
         this.queue('},{},' + JSON.stringify(entries) + ')');
+        if (generator) this.queue('\n' + generator.inlineMappingUrl());
+
         this.queue(null);
     }
 };
diff --git a/package.json b/package.json
index 6029693..445d5da 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,8 @@
         "JSONStream": "~0.4.3",
         "duplexer": "~0.0.3",
         "through": "~2.2.0",
-        "uglify-js": "1.3.4"
+        "uglify-js": "1.3.4",
+        "inline-source-map": "~0.1.3"
     },
     "devDependencies": {
         "tap": "~0.4.0",
diff --git a/test/source-maps.js b/test/source-maps.js
new file mode 100644
index 0000000..9c7aaa8
--- /dev/null
+++ b/test/source-maps.js
@@ -0,0 +1,113 @@
+var test = require('tape');
+var pack = require('../');
+
+function decode(base64) {
+    return new Buffer(base64, 'base64').toString();
+} 
+
+function grabSourceMap(lastLine) {
+    var base64 = lastLine.split(',').pop();
+    return JSON.parse(decode(base64));
+}
+
+function grabLastLine(src) {
+    return src.split('\n').pop();
+}
+
+test('pack one file with source file field and one without', function (t) {
+    t.plan(7);
+    
+    var p = pack();
+    var src = '';
+    p.on('data', function (buf) { src += buf });
+    p.on('end', function () {
+        var r = Function(['T'], 'return ' + src)(t);
+        t.equal(r('xyz')(5), 555);
+        t.equal(r('xyz')(5), 555);
+
+        var lastLine = grabLastLine(src);
+        var sm = grabSourceMap(lastLine);
+
+        t.ok(/^\/\/@ sourceMappingURL/.test(lastLine), 'contains source mapping url as last line');
+        t.deepEqual(sm.sources, [ 'foo.js' ], 'includes mappings for sourceFile only');
+        t.equal(sm.mappings, ';;;AAAA;AACA;AACA;AACA', 'adds offset mapping for each line' );
+    });
+    
+    p.end(JSON.stringify([
+        {
+            id: 'abc',
+            source: 'T.equal(require("./xyz")(3), 333)',
+            entry: true,
+            deps: { './xyz': 'xyz' }
+        },
+        {
+            id: 'xyz',
+            source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}',
+            sourceFile: 'foo.js'
+        }
+    ]));
+});
+
+test('pack two files with source file field', function (t) {
+    t.plan(7);
+    
+    var p = pack();
+    var src = '';
+    p.on('data', function (buf) { src += buf });
+    p.on('end', function () {
+        var r = Function(['T'], 'return ' + src)(t);
+        t.equal(r('xyz')(5), 555);
+        t.equal(r('xyz')(5), 555);
+
+        var lastLine = grabLastLine(src);
+        var sm = grabSourceMap(lastLine);
+
+        t.ok(/^\/\/@ sourceMappingURL/.test(lastLine), 'contains source mapping url as last line');
+        t.deepEqual(sm.sources, [ 'wunder/bar.js', 'foo.js' ], 'includes mappings for both files');
+        t.equal(sm.mappings, ';AAAA;;ACAA;AACA;AACA;AACA', 'adds offset mapping for each line' );
+    });
+    
+    p.end(JSON.stringify([
+        {
+            id: 'abc',
+            source: 'T.equal(require("./xyz")(3), 333)',
+            entry: true,
+            deps: { './xyz': 'xyz' },
+            sourceFile: 'wunder/bar.js'
+        },
+        {
+            id: 'xyz',
+            source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}',
+            sourceFile: 'foo.js'
+        }
+    ]));
+});
+
+test('pack two files without source file field', function (t) {
+    t.plan(5);
+    
+    var p = pack();
+    var src = '';
+    p.on('data', function (buf) { src += buf });
+    p.on('end', function () {
+        var r = Function(['T'], 'return ' + src)(t);
+        t.equal(r('xyz')(5), 555);
+        t.equal(r('xyz')(5), 555);
+
+        var lastLine = grabLastLine(src); 
+        t.notOk(/^\/\/@ sourceMappingURL/.test(lastLine), 'contains no source mapping url');
+    });
+    
+    p.end(JSON.stringify([
+        {
+            id: 'abc',
+            source: 'T.equal(require("./xyz")(3), 333)',
+            entry: true,
+            deps: { './xyz': 'xyz' }
+        },
+        {
+            id: 'xyz',
+            source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}'
+        }
+    ]));
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browser-pack.git



More information about the Pkg-javascript-commits mailing list