[Pkg-javascript-commits] [node-inline-source-map] 01/02: Imported Upstream version 0.6.1

Ross Gammon ross-guest at moszumanska.debian.org
Sun Nov 8 19:06:10 UTC 2015


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

ross-guest pushed a commit to branch master
in repository node-inline-source-map.

commit b5ffaad634aaf0f4bfe07176ed183dd255aea218
Author: Ross Gammon <rossgammon at mail.dk>
Date:   Sun Nov 8 19:12:41 2015 +0100

    Imported Upstream version 0.6.1
---
 .gitignore                |  16 +++
 .travis.yml               |   9 ++
 LICENSE                   |  23 ++++
 README.md                 |  89 ++++++++++++
 example/foo-bar.js        |   8 ++
 index.js                  | 135 ++++++++++++++++++
 package.json              |  44 ++++++
 test/inline-source-map.js | 342 ++++++++++++++++++++++++++++++++++++++++++++++
 test/source-content.js    | 143 +++++++++++++++++++
 9 files changed, 809 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..de78e27
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,16 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
+tmp
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..5e5e849
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,9 @@
+sudo: false
+language: node_js
+node_js:
+  - 0.8
+  - 0.10
+  - 0.12
+  - io.js
+before_install:
+  - npm install --global npm
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..41702c5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,23 @@
+Copyright 2013 Thorsten Lorenz. 
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..82c6b8b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,89 @@
+# inline-source-map [![build status](https://secure.travis-ci.org/thlorenz/inline-source-map.png)](http://travis-ci.org/thlorenz/inline-source-map)
+
+Adds source mappings and base64 encodes them, so they can be inlined in your generated file.
+
+```js
+var generator = require('inline-source-map');
+
+// default charset 'utf-8' is configurable
+var gen = generator({ charset: 'utf-8' }) 
+  .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
+  .addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 });
+
+console.log('base64 mapping:', gen.base64Encode());
+console.log('inline mapping url:', gen.inlineMappingUrl());
+```
+
+```
+base64 mapping: eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmb28uanMiLCJiYXIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O1VBQ0c7Ozs7Ozs7Ozs7Ozs7O3NCQ0RIO3NCQUNBIn0=
+inline mapping url: //@ sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmb28uanMiLCJiYXIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7O1VBQ0c7Ozs7Ozs7Ozs7Ozs7O3NCQ0RIO3NCQUNBIn0=
+```
+
+## API
+
+### addMappings(sourceFile, mappings, offset)
+
+```
+/**
+ * Adds the given mappings to the generator and offsets them if offset is given 
+ *
+ * @name addMappings
+ * @function
+ * @param sourceFile {String} name of the source file
+ * @param mappings {Array{{Object}} each object has the form { original: { line: _, column: _ }, generated: { line: _, column: _ } }
+ * @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }
+ * @return {Object} the generator to allow chaining
+ */
+```
+
+### addGeneratedMappings(sourceFile, source, offset)
+
+```
+/**
+ * Generates mappings for the given source and adds them, assuming that no translation from original to generated is necessary.
+ *
+ * @name addGeneratedMappings
+ * @function
+ * @param sourceFile {String} name of the source file
+ * @param source {String} source of the file
+ * @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }
+ * @return {Object} the generator to allow chaining
+ */
+```
+
+### addSourceContent(sourceFile, sourceContent)
+
+```
+/**
+ * Adds source content for the given source file.
+ * 
+ * @name addSourceContent
+ * @function
+ * @param sourceFile {String} The source file for which a mapping is included
+ * @param sourceContent {String} The content of the source file
+ * @return {Object} The generator to allow chaining
+ */
+```
+
+
+### base64Encode()
+
+```
+/**
+ * @name base64Encode
+ * @function
+ * @return {String} bas64 encoded representation of the added mappings
+ */
+```
+
+If source contents were added, this will be included in the encoded mappings.
+
+### inlineMappingUrl()
+
+```
+/**
+ * @name inlineMappingUrl
+ * @function
+ * @return {String} comment with base64 encoded representation of the added mappings. Can be inlined at the end of the generated file. 
+ */
+```
diff --git a/example/foo-bar.js b/example/foo-bar.js
new file mode 100644
index 0000000..247ee30
--- /dev/null
+++ b/example/foo-bar.js
@@ -0,0 +1,8 @@
+var generator = require('..');
+
+var gen = generator()
+  .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
+  .addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 });
+
+console.log('base64 mapping', gen.base64Encode());
+console.log('inline mapping url', gen.inlineMappingUrl());
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..df74d61
--- /dev/null
+++ b/index.js
@@ -0,0 +1,135 @@
+'use strict';
+var SourceMapGenerator = require('source-map').SourceMapGenerator;
+
+function offsetMapping(mapping, offset) {
+  return { line: offset.line + mapping.line, column: offset.column + mapping.column };
+}
+
+function newlinesIn(src) {
+  if (!src) return 0;
+  var newlines = src.match(/\n/g);
+
+  return newlines ? newlines.length : 0;
+}
+ 
+function Generator(opts) {
+  opts = opts || {};
+  this.generator = new SourceMapGenerator({ file: opts.file || '', sourceRoot: opts.sourceRoot || '' });
+  this.sourcesContent = undefined;
+  this.opts = opts;
+}
+
+/**
+ * Adds the given mappings to the generator and offsets them if offset is given 
+ *
+ * @name addMappings
+ * @function
+ * @param sourceFile {String} name of the source file
+ * @param mappings {Array{{Object}} each object has the form { original: { line: _, column: _ }, generated: { line: _, column: _ } }
+ * @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }
+ * @return {Object} the generator to allow chaining
+ */
+Generator.prototype.addMappings = function (sourceFile, mappings, offset) { 
+  var generator = this.generator; 
+
+  offset = offset || {};
+  offset.line = offset.hasOwnProperty('line') ? offset.line : 0;
+  offset.column = offset.hasOwnProperty('column') ? offset.column : 0;
+
+  mappings.forEach(function (m) {
+    // only set source if we have original position to handle edgecase (see inline-source-map tests)
+    generator.addMapping({
+        source    :  m.original ? sourceFile : undefined
+      , original  :  m.original
+      , generated :  offsetMapping(m.generated, offset)
+    });
+  });
+  return this;
+};
+
+/**
+ * Generates mappings for the given source, assuming that no translation from original to generated is necessary.
+ *
+ * @name addGeneratedMappings
+ * @function
+ * @param sourceFile {String} name of the source file
+ * @param source {String} source of the file
+ * @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }
+ * @return {Object} the generator to allow chaining
+ */
+Generator.prototype.addGeneratedMappings = function (sourceFile, source, offset) {
+  var mappings = []
+    , linesToGenerate = newlinesIn(source) + 1;
+
+  for (var line = 1; line <= linesToGenerate; line++) {
+    var location = { line: line, column: 0 };
+    mappings.push({ original: location, generated: location });
+  }
+
+  return this.addMappings(sourceFile, mappings, offset);
+};
+
+/**
+ * Adds source content for the given source file.
+ * 
+ * @name addSourceContent
+ * @function
+ * @param sourceFile {String} The source file for which a mapping is included
+ * @param sourcesContent {String} The content of the source file
+ * @return {Object} The generator to allow chaining
+ */
+Generator.prototype.addSourceContent = function (sourceFile, sourcesContent) {
+  this.sourcesContent = this.sourcesContent || {};
+  this.sourcesContent[sourceFile] = sourcesContent;
+  return this;
+};
+
+/**
+ * @name base64Encode
+ * @function
+ * @return {String} bas64 encoded representation of the added mappings
+ */
+Generator.prototype.base64Encode = function () {
+  var map = this.toString();
+  return new Buffer(map).toString('base64');
+};
+
+/**
+ * @name inlineMappingUrl
+ * @function
+ * @return {String} comment with base64 encoded representation of the added mappings. Can be inlined at the end of the generated file. 
+ */
+Generator.prototype.inlineMappingUrl = function () {
+  var charset = this.opts.charset || 'utf-8';
+  return '//# sourceMappingURL=data:application/json;charset=' + charset + ';base64,' + this.base64Encode();
+};
+
+Generator.prototype.toJSON = function () {
+  var map = this.generator.toJSON();
+  if (!this.sourcesContent) return map;
+
+  var toSourcesContent = (function (s) {
+    if (typeof this.sourcesContent[s] === 'string') {
+      return this.sourcesContent[s];
+    } else {
+      return null;
+    }
+  }).bind(this);
+  map.sourcesContent = map.sources.map(toSourcesContent);
+  return map;
+};
+
+Generator.prototype.toString = function () {
+  return JSON.stringify(this);
+};
+
+Generator.prototype._mappings = function () {
+  return this.generator._mappings._array;
+};
+
+Generator.prototype.gen = function () {
+  return this.generator;
+};
+
+module.exports = function (opts) { return new Generator(opts); };
+module.exports.Generator = Generator;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..933d12e
--- /dev/null
+++ b/package.json
@@ -0,0 +1,44 @@
+{
+  "name": "inline-source-map",
+  "version": "0.6.1",
+  "description": "Adds source mappings and base64 encodes them, so they can be inlined in your generated file.",
+  "main": "index.js",
+  "scripts": {
+    "test-main": "tap test/*.js",
+    "test-0.8": "nave use 0.8 npm run test-main",
+    "test-0.10": "nave use 0.10 npm run test-main",
+    "test-0.12": "nave use 0.12 npm run test-main",
+    "test-all": "npm run test-main && npm run test-0.8 && npm run test-0.10 && npm run test-0.12",
+    "test": "if [ -e $TRAVIS ]; then npm run test-all; else npm run test-main; fi"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/thlorenz/inline-source-map.git"
+  },
+  "homepage": "https://github.com/thlorenz/inline-source-map",
+  "dependencies": {
+    "source-map": "~0.4.0"
+  },
+  "devDependencies": {
+    "tap": "~0.7.0",
+    "nave": "~0.5.0"
+  },
+  "keywords": [
+    "source",
+    "map",
+    "inline",
+    "base64",
+    "bundle",
+    "generate",
+    "transpile"
+  ],
+  "author": {
+    "name": "Thorsten Lorenz",
+    "email": "thlorenz at gmx.de",
+    "url": "http://thlorenz.com"
+  },
+  "license": "MIT",
+  "engine": {
+    "node": ">=0.6"
+  }
+}
diff --git a/test/inline-source-map.js b/test/inline-source-map.js
new file mode 100644
index 0000000..a6b9f5e
--- /dev/null
+++ b/test/inline-source-map.js
@@ -0,0 +1,342 @@
+'use strict';
+/*jshint asi: true*/
+
+var test = require('tap').test
+var generator = require('..');
+
+var foo = '' + function foo () {
+  var hello = 'hello';
+  var world = 'world';
+  console.log('%s %s', hello, world);
+}
+
+var bar = '' + function bar () {
+  console.log('yes?');
+}
+
+function decode(base64) {
+  return new Buffer(base64, 'base64').toString();
+} 
+
+function inspect(obj, depth) {
+  console.error(require('util').inspect(obj, false, depth || 5, true));
+}
+
+test('generated mappings', function (t) {
+
+  t.test('one file no offset', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('foo.js', foo)
+
+    t.deepEqual(
+      gen._mappings()
+    , [ { generatedLine: 1,
+          generatedColumn: 0,
+          originalLine: 1,
+          originalColumn: 0,
+          source: 'foo.js',
+          name: null },
+        { generatedLine: 2,
+          generatedColumn: 0,
+          originalLine: 2,
+          originalColumn: 0,
+          source: 'foo.js',
+          name: null },
+        { generatedLine: 3,
+          generatedColumn: 0,
+          originalLine: 3,
+          originalColumn: 0,
+          source: 'foo.js',
+          name: null },
+        { generatedLine: 4,
+          generatedColumn: 0,
+          originalLine: 4,
+          originalColumn: 0,
+          source: 'foo.js',
+          name: null },
+        { generatedLine: 5,
+          generatedColumn: 0,
+          originalLine: 5,
+          originalColumn: 0,
+          source: 'foo.js',
+          name: null } ]      
+      , 'generates correct mappings'
+    )
+
+    t.deepEqual(
+        JSON.parse(decode(gen.base64Encode()))
+      , {"version":3,"file":"","sources":["foo.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA","sourceRoot":""}
+      , 'encodes generated mappings'
+    )
+    t.equal(
+        gen.inlineMappingUrl()
+      , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIifQ=='
+      , 'returns correct inline mapping url'
+    )
+    t.end()
+  })
+
+  t.test('two files no offset', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('foo.js', foo)
+      .addGeneratedMappings('bar.js', bar)
+
+    t.deepEqual(
+        gen._mappings()
+      , [ { generatedLine: 1,
+            generatedColumn: 0,
+            originalLine: 1,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 2,
+            generatedColumn: 0,
+            originalLine: 2,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 3,
+            generatedColumn: 0,
+            originalLine: 3,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 4,
+            generatedColumn: 0,
+            originalLine: 4,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 5,
+            generatedColumn: 0,
+            originalLine: 5,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 1,
+            generatedColumn: 0,
+            originalLine: 1,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null },
+          { generatedLine: 2,
+            generatedColumn: 0,
+            originalLine: 2,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null },
+          { generatedLine: 3,
+            generatedColumn: 0,
+            originalLine: 3,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null } ]      
+        , 'generates correct mappings'
+    )
+    t.deepEqual(
+        JSON.parse(decode(gen.base64Encode()))
+      , {"version":3,"file":"","sources":["foo.js","bar.js"],"names":[],"mappings":"ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA","sourceRoot": ""}
+      , 'encodes generated mappings'
+    )
+    t.equal(
+        gen.inlineMappingUrl()
+      , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIifQ=='
+      , 'returns correct inline mapping url'
+    )
+    t.end()
+  })
+
+  t.test('one line source', function (t) {
+    var gen = generator().addGeneratedMappings('one-liner.js',  'console.log("line one");')
+    t.deepEqual(
+        gen._mappings()
+      , [ { generatedLine: 1,
+            generatedColumn: 0,
+            originalLine: 1,
+            originalColumn: 0,
+            source: 'one-liner.js',
+            name: null } ]      
+    , 'generates correct mappings'
+    )
+    t.end()
+  })
+
+  t.test('with offset', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('foo.js', foo, { line: 20 })
+      .addGeneratedMappings('bar.js', bar, { line: 23, column: 22 })
+
+    t.deepEqual(
+        gen._mappings()
+      , [ { generatedLine: 21,
+            generatedColumn: 0,
+            originalLine: 1,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 22,
+            generatedColumn: 0,
+            originalLine: 2,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 23,
+            generatedColumn: 0,
+            originalLine: 3,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 24,
+            generatedColumn: 0,
+            originalLine: 4,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 25,
+            generatedColumn: 0,
+            originalLine: 5,
+            originalColumn: 0,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 24,
+            generatedColumn: 22,
+            originalLine: 1,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null },
+          { generatedLine: 25,
+            generatedColumn: 22,
+            originalLine: 2,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null },
+          { generatedLine: 26,
+            generatedColumn: 22,
+            originalLine: 3,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null } ]        
+      , 'generates correct mappings'
+    )
+
+    t.deepEqual(
+        JSON.parse(decode(gen.base64Encode()))
+      , {"version":3,"file":"","sources":["foo.js","bar.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AACA;AACA;AACA,sBCHA;ADIA,sBCHA;sBACA", "sourceRoot": ""}
+      , 'encodes generated mappings with offset'
+    )
+    t.end()
+  })
+})
+
+test('given mappings, with one having no original', function (t) {
+  t.test('no offset', function (t) {
+    var gen = generator()
+      .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }])
+
+      // This addresses an edgecase in which a transpiler generates mappings but doesn't include the original position.
+      // If we set source to sourceFile (as usual) in that case, the mappings are considered invalid by the source-map module's
+      // SourceMapGenerator. Keeping source undefined fixes this problem.
+      // Raised issue: https://github.com/thlorenz/inline-source-map/issues/2
+      // Validate function: https://github.com/mozilla/source-map/blob/a3372ea78e662582087dd25ebda999c06424e047/lib/source-map/source-map-generator.js#L232
+      .addMappings('bar.js', [
+            { original: { line: 6, column: 0 } , generated: { line: 7, column: 20 } }
+          , { generated: { line: 8, column: 30 } }
+      ])
+
+    t.deepEqual(
+        gen._mappings()
+      , [ { generatedLine: 5,
+            generatedColumn: 10,
+            originalLine: 2,
+            originalColumn: 3,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 7,
+            generatedColumn: 20,
+            originalLine: 6,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null },
+          { generatedLine: 8,
+            generatedColumn: 30,
+            originalLine: false,
+            originalColumn: false,
+            source: undefined,
+            name: null } ]
+      , 'adds correct mappings'
+    )
+    t.deepEqual(
+        JSON.parse(decode(gen.base64Encode()))
+      , {"version":3,"file":"","sources":["foo.js","bar.js"],"names":[],"mappings":";;;;UACG;;oBCIH;8B", sourceRoot: ""}
+      , 'encodes generated mappings'
+    )
+    t.equal(
+        gen.inlineMappingUrl()
+      , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O1VBQ0c7O29CQ0lIOzhCIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIifQ=='
+      , 'returns correct inline mapping url'
+    )
+    t.end()
+  })
+
+  t.test('with offset', function (t) {
+    var gen = generator()
+      .addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })
+      .addMappings('bar.js', [{ original: { line: 6, column: 0 } , generated: { line: 7, column: 20 } }, { generated: { line: 8, column: 30 } }], { line: 9, column: 3 })
+
+    t.deepEqual(
+        gen._mappings()
+      , [ { generatedLine: 10,
+            generatedColumn: 10,
+            originalLine: 2,
+            originalColumn: 3,
+            source: 'foo.js',
+            name: null },
+          { generatedLine: 16,
+            generatedColumn: 23,
+            originalLine: 6,
+            originalColumn: 0,
+            source: 'bar.js',
+            name: null },
+          { generatedLine: 17,
+            generatedColumn: 33,
+            originalLine: false,
+            originalColumn: false,
+            source: undefined,
+            name: null } ]     
+      , 'adds correct mappings'
+    )
+    t.deepEqual(
+        JSON.parse(decode(gen.base64Encode()))
+      , {"version":3,"file":"","sources":["foo.js","bar.js"],"names":[],"mappings":";;;;;;;;;UACG;;;;;;uBCIH;iC", sourceRoot: ""}
+      , 'encodes mappings with offset'
+    )
+    t.end()
+  })
+});
+
+test('inline mapping url with charset opt', function(t){
+  t.test('set inline mapping url charset to gbk', function(t){
+    var gen = generator({charset: 'gbk'})
+                .addGeneratedMappings('foo.js', foo);
+    t.equal(
+      gen.inlineMappingUrl(),
+      '//# sourceMappingURL=data:application/json;charset=gbk;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIifQ==',
+      'charset set to gbk'
+    );
+
+    t.end();
+  });
+
+  t.test('default charset should be utf-8', function(t){
+    var gen = generator()
+              .addGeneratedMappings('foo.js', foo);
+
+    t.equal(
+      gen.inlineMappingUrl(),
+      '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIifQ==',
+      'charset default to utf-8'
+    );
+
+    t.end();
+  });
+});
diff --git a/test/source-content.js b/test/source-content.js
new file mode 100644
index 0000000..d18997d
--- /dev/null
+++ b/test/source-content.js
@@ -0,0 +1,143 @@
+'use strict';
+/*jshint asi: true*/
+
+var test = require('tap').test
+var generator = require('..');
+
+var foo = '' + function foo () {
+  var hello = 'hello';
+  var world = 'world';
+  console.log('%s %s', hello, world);
+}
+
+var bar = '' + function bar () {
+  console.log('yes?');
+}
+
+function decode(base64) {
+  return new Buffer(base64, 'base64').toString();
+} 
+
+function inspect(obj, depth) {
+  console.log(require('util').inspect(obj, false, depth || 5, true));
+}
+
+test('generated mappings', function (t) {
+
+  t.test('one file with source content', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('foo.js', foo)
+      .addSourceContent('foo.js', foo)
+
+    t.deepEqual(
+        gen.toJSON()
+      , { "version": 3,
+          "file": "",
+          "sources": [
+            "foo.js"
+          ],
+          "names": [],
+          "mappings": "AAAA;AACA;AACA;AACA;AACA",
+          "sourceRoot": "",
+          "sourcesContent": [
+            "function foo() {\n  var hello = 'hello';\n  var world = 'world';\n  console.log('%s %s', hello, world);\n}"
+          ],
+        }
+      , 'includes source content'
+    )
+
+    t.equal(
+        decode(gen.base64Encode())
+      , '{"version":3,"sources":["foo.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA","file":"","sourceRoot":"","sourcesContent":["function foo() {\\n  var hello = \'hello\';\\n  var world = \'world\';\\n  console.log(\'%s %s\', hello, world);\\n}"]}'
+      , 'encodes generated mappings including source content'
+    )
+    t.equal(
+        gen.inlineMappingUrl()
+      , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59Il19'
+      , 'returns correct inline mapping url including source content'
+    )
+    t.end()
+  })
+
+  t.test('two files with source content', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('foo.js', foo)
+      .addSourceContent('foo.js', foo)
+      .addGeneratedMappings('bar.js', bar)
+      .addSourceContent('bar.js', bar)
+
+    t.deepEqual(
+        gen.toJSON()
+      ,  { "version": 3,
+          "file": "",
+          "sources": [
+            "foo.js",
+            "bar.js"
+          ],
+          "names": [],
+          "mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
+          "sourceRoot": "",
+          "sourcesContent": [
+            "function foo() {\n  var hello = 'hello';\n  var world = 'world';\n  console.log('%s %s', hello, world);\n}",
+            "function bar() {\n  console.log('yes?');\n}"
+          ],
+        }
+      , 'includes source content for both files'
+    )
+
+    t.deepEqual(
+        decode(gen.base64Encode())
+      , '{"version":3,"sources":["foo.js","bar.js"],"names":[],"mappings":"ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA","file":"","sourceRoot":"","sourcesContent":["function foo() {\\n  var hello = \'hello\';\\n  var world = \'world\';\\n  console.log(\'%s %s\', hello, world);\\n}","function bar() {\\n  console.log(\'yes?\');\\n}"]}'
+      , 'encodes generated mappings including source content'
+    )
+    t.equal(
+        gen.inlineMappingUrl()
+      , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59IiwiZnVuY3Rpb24gYmFyKCkge1xuICBjb25zb2xlLmxvZygneWVzPycpO1xufSJdfQ=='
+      , 'returns correct inline mapping url including source content'
+    )
+    t.end()
+  })
+
+  t.test('two files, only one with source content', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('foo.js', foo)
+      .addGeneratedMappings('bar.js', bar)
+      .addSourceContent('bar.js', bar)
+
+    t.deepEqual(
+        gen.toJSON()
+      ,  { "version": 3,
+          "file": "",
+          "sources": [
+            "foo.js",
+            "bar.js"
+          ],
+          "names": [],
+          "mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
+          "sourcesContent": [ null, "function bar() {\n  console.log('yes?');\n}" ],
+          "sourceRoot": ""
+        }
+      , 'includes source content for the file with source content and [null] for the other file'
+    )
+
+    t.deepEqual(
+        decode(gen.base64Encode())
+      , '{"version":3,"sources":["foo.js","bar.js"],"names":[],"mappings":"ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA","file":"","sourceRoot":"","sourcesContent":[null,"function bar() {\\n  console.log(\'yes?\');\\n}"]}'
+      , 'encodes generated mappings including source content'
+    )
+    t.equal(
+        gen.inlineMappingUrl()
+      , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6W251bGwsImZ1bmN0aW9uIGJhcigpIHtcbiAgY29uc29sZS5sb2coJ3llcz8nKTtcbn0iXX0='
+      , 'returns correct inline mapping url including source content'
+    )
+    t.end()
+  })
+
+  t.test('one file with empty source', function (t) {
+    var gen = generator()
+      .addGeneratedMappings('empty.js', '')
+      .addSourceContent('empty.js', '')
+    t.deepEqual(gen.toJSON()["sourcesContent"], [""])
+    t.end()
+  });
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-inline-source-map.git



More information about the Pkg-javascript-commits mailing list