UNPKG

925 BJavaScriptView Raw
1import { Readable } from 'stream';
2import { rollup } from 'rollup';
3
4const build = async (options, stream) => {
5 const bundle = await rollup(options);
6 stream.emit('bundle', bundle);
7 const { output } = await bundle.generate(options);
8 for (const chunk of output) {
9 if (chunk.type === 'asset') {
10 stream.push(chunk.source);
11 }
12 else {
13 stream.push(chunk.code);
14 if (chunk.map) {
15 stream.push(`\n//# sourceMappingURL=${chunk.map.toUrl()}`);
16 }
17 }
18 }
19 // signal end of write
20 stream.push(null);
21};
22const stream = (options) => {
23 const result = new Readable({
24 // stub _read() as it's not available on Readable stream, needed by gulp et al
25 read: () => { }
26 });
27 build(options, result).catch((error) => {
28 result.emit('error', error);
29 });
30 return result;
31};
32
33export default stream;