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