source-content.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. /*jshint asi: true*/
  3. var test = require('tap').test
  4. var generator = require('..');
  5. var foo = '' + function foo () {
  6. var hello = 'hello';
  7. var world = 'world';
  8. console.log('%s %s', hello, world);
  9. }
  10. var bar = '' + function bar () {
  11. console.log('yes?');
  12. }
  13. function decode(base64) {
  14. return new Buffer(base64, 'base64').toString();
  15. }
  16. function inspect(obj, depth) {
  17. console.log(require('util').inspect(obj, false, depth || 5, true));
  18. }
  19. test('generated mappings', function (t) {
  20. t.test('one file with source content', function (t) {
  21. var gen = generator()
  22. .addGeneratedMappings('foo.js', foo)
  23. .addSourceContent('foo.js', foo)
  24. t.deepEqual(
  25. gen.toJSON()
  26. , { "version": 3,
  27. "file": "",
  28. "sources": [
  29. "foo.js"
  30. ],
  31. "names": [],
  32. "mappings": "AAAA;AACA;AACA;AACA;AACA",
  33. "sourceRoot": "",
  34. "sourcesContent": [
  35. "function foo() {\n var hello = 'hello';\n var world = 'world';\n console.log('%s %s', hello, world);\n}"
  36. ],
  37. }
  38. , 'includes source content'
  39. )
  40. t.equal(
  41. decode(gen.base64Encode())
  42. , '{"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}"]}'
  43. , 'encodes generated mappings including source content'
  44. )
  45. t.equal(
  46. gen.inlineMappingUrl()
  47. , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59Il19'
  48. , 'returns correct inline mapping url including source content'
  49. )
  50. t.end()
  51. })
  52. t.test('two files with source content', function (t) {
  53. var gen = generator()
  54. .addGeneratedMappings('foo.js', foo)
  55. .addSourceContent('foo.js', foo)
  56. .addGeneratedMappings('bar.js', bar)
  57. .addSourceContent('bar.js', bar)
  58. t.deepEqual(
  59. gen.toJSON()
  60. , { "version": 3,
  61. "file": "",
  62. "sources": [
  63. "foo.js",
  64. "bar.js"
  65. ],
  66. "names": [],
  67. "mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
  68. "sourceRoot": "",
  69. "sourcesContent": [
  70. "function foo() {\n var hello = 'hello';\n var world = 'world';\n console.log('%s %s', hello, world);\n}",
  71. "function bar() {\n console.log('yes?');\n}"
  72. ],
  73. }
  74. , 'includes source content for both files'
  75. )
  76. t.deepEqual(
  77. decode(gen.base64Encode())
  78. , '{"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}"]}'
  79. , 'encodes generated mappings including source content'
  80. )
  81. t.equal(
  82. gen.inlineMappingUrl()
  83. , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59IiwiZnVuY3Rpb24gYmFyKCkge1xuICBjb25zb2xlLmxvZygneWVzPycpO1xufSJdfQ=='
  84. , 'returns correct inline mapping url including source content'
  85. )
  86. t.end()
  87. })
  88. t.test('two files, only one with source content', function (t) {
  89. var gen = generator()
  90. .addGeneratedMappings('foo.js', foo)
  91. .addGeneratedMappings('bar.js', bar)
  92. .addSourceContent('bar.js', bar)
  93. t.deepEqual(
  94. gen.toJSON()
  95. , { "version": 3,
  96. "file": "",
  97. "sources": [
  98. "foo.js",
  99. "bar.js"
  100. ],
  101. "names": [],
  102. "mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
  103. "sourcesContent": [ null, "function bar() {\n console.log('yes?');\n}" ],
  104. "sourceRoot": ""
  105. }
  106. , 'includes source content for the file with source content and [null] for the other file'
  107. )
  108. t.deepEqual(
  109. decode(gen.base64Encode())
  110. , '{"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}"]}'
  111. , 'encodes generated mappings including source content'
  112. )
  113. t.equal(
  114. gen.inlineMappingUrl()
  115. , '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6W251bGwsImZ1bmN0aW9uIGJhcigpIHtcbiAgY29uc29sZS5sb2coJ3llcz8nKTtcbn0iXX0='
  116. , 'returns correct inline mapping url including source content'
  117. )
  118. t.end()
  119. })
  120. t.test('one file with empty source', function (t) {
  121. var gen = generator()
  122. .addGeneratedMappings('empty.js', '')
  123. .addSourceContent('empty.js', '')
  124. t.deepEqual(gen.toJSON()["sourcesContent"], [""])
  125. t.end()
  126. });
  127. })