index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var JSONStream = require('JSONStream');
  2. var defined = require('defined');
  3. var through = require('through2');
  4. var umd = require('umd');
  5. var Buffer = require('safe-buffer').Buffer;
  6. var fs = require('fs');
  7. var path = require('path');
  8. var combineSourceMap = require('combine-source-map');
  9. var defaultPreludePath = path.join(__dirname, '_prelude.js');
  10. var defaultPrelude = fs.readFileSync(defaultPreludePath, 'utf8');
  11. function newlinesIn(src) {
  12. if (!src) return 0;
  13. var newlines = src.match(/\n/g);
  14. return newlines ? newlines.length : 0;
  15. }
  16. module.exports = function (opts) {
  17. if (!opts) opts = {};
  18. var parser = opts.raw ? through.obj() : JSONStream.parse([ true ]);
  19. var stream = through.obj(
  20. function (buf, enc, next) { parser.write(buf); next() },
  21. function () { parser.end() }
  22. );
  23. parser.pipe(through.obj(write, end));
  24. stream.standaloneModule = opts.standaloneModule;
  25. stream.hasExports = opts.hasExports;
  26. var first = true;
  27. var entries = [];
  28. var basedir = defined(opts.basedir, process.cwd());
  29. var prelude = opts.prelude || defaultPrelude;
  30. var preludePath = opts.preludePath ||
  31. path.relative(basedir, defaultPreludePath).replace(/\\/g, '/');
  32. var lineno = 1 + newlinesIn(prelude);
  33. var sourcemap;
  34. return stream;
  35. function write (row, enc, next) {
  36. if (first && opts.standalone) {
  37. var pre = umd.prelude(opts.standalone).trim();
  38. stream.push(Buffer.from(pre + 'return ', 'utf8'));
  39. }
  40. else if (first && stream.hasExports) {
  41. var pre = opts.externalRequireName || 'require';
  42. stream.push(Buffer.from(pre + '=', 'utf8'));
  43. }
  44. if (first) stream.push(Buffer.from(prelude + '({', 'utf8'));
  45. if (row.sourceFile && !row.nomap) {
  46. if (!sourcemap) {
  47. sourcemap = combineSourceMap.create(null, opts.sourceRoot);
  48. sourcemap.addFile(
  49. { sourceFile: preludePath, source: prelude },
  50. { line: 0 }
  51. );
  52. }
  53. sourcemap.addFile(
  54. { sourceFile: row.sourceFile, source: row.source },
  55. { line: lineno }
  56. );
  57. }
  58. var wrappedSource = [
  59. (first ? '' : ','),
  60. JSON.stringify(row.id),
  61. ':[',
  62. 'function(require,module,exports){\n',
  63. combineSourceMap.removeComments(row.source),
  64. '\n},',
  65. '{' + Object.keys(row.deps || {}).sort().map(function (key) {
  66. return JSON.stringify(key) + ':'
  67. + JSON.stringify(row.deps[key])
  68. ;
  69. }).join(',') + '}',
  70. ']'
  71. ].join('');
  72. stream.push(Buffer.from(wrappedSource, 'utf8'));
  73. lineno += newlinesIn(wrappedSource);
  74. first = false;
  75. if (row.entry && row.order !== undefined) {
  76. entries[row.order] = row.id;
  77. }
  78. else if (row.entry) entries.push(row.id);
  79. next();
  80. }
  81. function end () {
  82. if (first) stream.push(Buffer.from(prelude + '({', 'utf8'));
  83. entries = entries.filter(function (x) { return x !== undefined });
  84. stream.push(
  85. Buffer.from('},{},' + JSON.stringify(entries) + ')', 'utf8')
  86. );
  87. if (opts.standalone && !first) {
  88. stream.push(Buffer.from(
  89. '(' + JSON.stringify(stream.standaloneModule) + ')'
  90. + umd.postlude(opts.standalone),
  91. 'utf8'
  92. ));
  93. }
  94. if (sourcemap) {
  95. var comment = sourcemap.comment();
  96. if (opts.sourceMapPrefix) {
  97. comment = comment.replace(
  98. /^\/\/#/, function () { return opts.sourceMapPrefix }
  99. )
  100. }
  101. stream.push(Buffer.from('\n' + comment + '\n', 'utf8'));
  102. }
  103. if (!sourcemap && !opts.standalone) {
  104. stream.push(Buffer.from(';\n', 'utf8'));
  105. }
  106. stream.push(null);
  107. }
  108. };