UNPKG

2.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const path_1 = require("path");
5const stream_1 = require("stream");
6const plugin_error_1 = tslib_1.__importDefault(require("plugin-error"));
7const rollup = tslib_1.__importStar(require("rollup"));
8const source_map_compactor_1 = tslib_1.__importDefault(require("source-map-compactor"));
9const vinyl_1 = tslib_1.__importDefault(require("vinyl"));
10function isEmittedAsset(chunkOrAsset) {
11 return !!chunkOrAsset.isAsset;
12}
13function rollupToVinyl(inputOptions, outputOptions) {
14 const stream = new stream_1.Readable({ objectMode: true });
15 stream._read = () => { };
16 function generatePath(fileName) {
17 return outputOptions.dir
18 ? path_1.posix.join(outputOptions.dir, fileName)
19 : fileName;
20 }
21 function emitAsset({ fileName, source }) {
22 stream.push(new vinyl_1.default({
23 contents: source instanceof Uint8Array
24 ? Buffer.from(source)
25 : Buffer.from(source, 'utf8'),
26 path: path_1.resolve(process.cwd(), generatePath(fileName)),
27 }));
28 }
29 function emitChunk({ code, fileName, map, isEntry }) {
30 const chunkPath = generatePath(fileName);
31 if (map) {
32 map.mappings = JSON.parse(source_map_compactor_1.default(map)).mappings;
33 map.file = chunkPath;
34 }
35 stream.push(new vinyl_1.default({
36 isEntryPoint: isEntry,
37 contents: Buffer.from(code, 'utf8'),
38 path: path_1.resolve(process.cwd(), chunkPath),
39 sourceMap: map,
40 }));
41 }
42 rollup
43 .rollup(inputOptions)
44 .then((bundle) => {
45 stream.emit('bundle', bundle);
46 return bundle.generate(outputOptions).then(({ output }) => {
47 for (const chunkOrAsset of output) {
48 if (isEmittedAsset(chunkOrAsset))
49 emitAsset(chunkOrAsset);
50 else
51 emitChunk(chunkOrAsset);
52 }
53 stream.push(null);
54 });
55 })
56 .catch((reason) => {
57 setImmediate(() => {
58 const pluginErr = new plugin_error_1.default('rollup', reason);
59 if (reason.loc) {
60 pluginErr.fileName = reason.loc.file;
61 pluginErr.lineNumber = reason.loc.line;
62 pluginErr.columnNumber = reason.loc.column;
63 }
64 delete pluginErr.loc;
65 delete pluginErr.pos;
66 stream.emit('error', pluginErr);
67 });
68 });
69 return stream;
70}
71exports.default = rollupToVinyl;