Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var Readable = require('stream').Readable;
var path = require('path');
const Readable = require('stream').Readable;
const path = require('path');

module.exports = function rollupStream(options) {
var stream = new Readable();
stream._read = function() { };
const stream = new Readable();
stream._read = () => {};

if(typeof options === 'object' && options !== null) {
options = Promise.resolve(options);
} else if(typeof options === 'string') {
var optionsPath = path.resolve(options);
const optionsPath = path.resolve(options);
options = require('rollup').rollup({
input: optionsPath,
onwarn: function(warning) {
onwarn: warning => {
if(warning.code !== 'UNRESOLVED_IMPORT') {
console.warn(warning.message);
}
}
}).then(function(bundle) {
return bundle.generate({ format: 'cjs' });
}).then(function(result) {
})
.then( bundle => bundle.generate({ format: 'cjs' }) )
.then( result => {
// don't look at me. this is how Rollup does it.
var defaultLoader = require.extensions['.js'];
const defaultLoader = require.extensions['.js'];

require.extensions['.js'] = function(m, filename) {
if(filename === optionsPath) {
Expand All @@ -40,16 +40,16 @@ module.exports = function rollupStream(options) {
options = Promise.reject(Error("options must be an object or a string!"));
}

options.then(function(options0) {
var rollup = options0.rollup;
var hasCustomRollup = true;
options.then( options0 => {
let rollup = options0.rollup;
let hasCustomRollup = true;
if(!rollup) {
rollup = require('rollup');
hasCustomRollup = false;
}

var options = {};
for(var key in options0) {
const options = {};
for(const key in options0) {
if(key === 'sourceMap' && !hasCustomRollup) {
console.warn(
"The sourceMap option has been renamed to \"sourcemap\" " +
Expand All @@ -62,22 +62,32 @@ module.exports = function rollupStream(options) {
}
}

return rollup.rollup(options).then(function(bundle) {
stream.emit('bundle', bundle);
return rollup.rollup(options)
.then( bundle => {
stream.emit('bundle', bundle);

return bundle.generate(options);
}).then(function(result) {
var code = result.code, map = result.map;

stream.push(code);
if(options.sourcemap || options.sourceMap) {
stream.push('\n//# sourceMappingURL=');
stream.push(map.toUrl());
}
stream.push(null);
});
}).catch(function(reason) {
setImmediate(function() {
return bundle.generate(options);
})
.then( result => {
for (const chunkOrAsset of result.output) {
if (chunkOrAsset.isAsset) {
stream.push(chunkOrAsset.source);
} else {
const code = chunkOrAsset.code, map = chunkOrAsset.map;
stream.push(code);

if(options.sourcemap || options.sourceMap) {
stream.push('\n//# sourceMappingURL=');
stream.push(map.toUrl());
}
}
}

stream.push(null);
});
})
.catch( reason => {
setImmediate( () => {
stream.emit('error', reason);
});
});
Expand Down
Loading