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