Skip to content

Commit

Permalink
Fixed options passing, since rollup@0.48 options.output is required
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Sep 12, 2017
1 parent 150aba8 commit ec10569
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
35 changes: 30 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
var Readable = require('stream').Readable;
var path = require('path');

function isObject(obj) {
return typeof obj === 'object' && obj !== null;
}

var assign = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};

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

if(typeof options === 'object' && options !== null) {
if(isObject(options)) {
options = Promise.resolve(options);
} else if(typeof options === 'string') {
var optionsPath = path.resolve(options);
Expand Down Expand Up @@ -41,6 +57,10 @@ module.exports = function rollupStream(options) {
}

options.then(function(options0) {
if (options0.output === undefined || !isObject(options0.output)) {
return Promise.reject(new Error("You must specify options.output and it must be an object"));
}

var rollup = options0.rollup;
var hasCustomRollup = true;
if(!rollup) {
Expand All @@ -49,28 +69,33 @@ module.exports = function rollupStream(options) {
}

var options = {};
var outputOptions = {};

for(var key in options0) {
if(key === 'sourceMap' && !hasCustomRollup) {
console.warn(
"The sourceMap option has been renamed to \"sourcemap\" " +
"(lowercase \"m\") in Rollup. The old form is now deprecated " +
"in rollup-stream."
);
options.sourcemap = options0.sourceMap;
outputOptions.sourcemap = options0.sourceMap;
} else if(key !== 'rollup') {
options[key] = options0[key];
outputOptions[key] = options0[key];
}
}

assign(outputOptions, options0.output);

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

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

stream.push(code);
if(options.sourcemap || options.sourceMap) {
if(outputOptions.sourcemap) {
stream.push('\n//# sourceMappingURL=');
stream.push(map.toUrl());
}
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import hypothetical from 'rollup-plugin-hypothetical';

export default {
input: './entry.js',
format: 'es',
output: {
format: 'es',
},
plugins: [hypothetical({
files: {
'./entry.js': 'import x from "./x.js"; console.log(x);',
Expand Down
48 changes: 39 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ describe("rollup-stream", function() {
});

it("should emit an error if options.input isn't present", function(done) {
var s = rollup({});
var s = rollup({
output: {
format: 'es'
}
});
s.on('error', function(err) {
expect(err.message).to.equal("You must supply options.input to rollup");
done();
Expand All @@ -50,10 +54,23 @@ describe("rollup-stream", function() {
});
});

it("should emit an error if options.output isn't present", function(done) {
var s = rollup({ input: './entry.js' });
s.on('error', function(err) {
expect(err.message).to.equal("You must specify options.output and it must be an object");
done();
});
s.on('data', function() {
done(Error("No error was emitted."));
});
});

it("should take a snapshot of options when the function is called", function() {
var options = {
input : './entry.js',
format: 'es',
output: {
format: 'es'
},
plugins: [hypothetical({
files: {
'./entry.js': 'import x from "./x.js"; console.log(x);',
Expand All @@ -70,12 +87,15 @@ describe("rollup-stream", function() {

it("should use a custom Rollup if options.rollup is passed", function() {
var options = {
output: {
format: 'es'
},
rollup: {
rollup: function(options) {
expect(options).to.equal(options);
expect(options).to.eql(options);
return Promise.resolve({
generate: function(options) {
expect(options).to.equal(options);
expect(options).to.eql(options);
return Promise.resolve({ code: 'fake code' });
}
});
Expand All @@ -90,7 +110,9 @@ describe("rollup-stream", function() {
it("shouldn't raise an alarm when options.rollup is passed", function() {
return collect(rollup({
input : './entry.js',
format: 'es',
output: {
format: 'es'
},
rollup: require('rollup'),
plugins: [{
resolveId: function(id) {
Expand Down Expand Up @@ -125,7 +147,9 @@ describe("rollup-stream", function() {
it("should emit a 'bundle' event when the bundle is output", function(done) {
var s = rollup({
input : './entry.js',
format: 'es',
output: {
format: 'es'
},
plugins: [{
resolveId: function(id) {
return id;
Expand Down Expand Up @@ -161,7 +185,9 @@ describe("sourcemaps", function() {
it("should be added when options.sourcemap is true", function() {
return collect(rollup({
input: './entry.js',
format: 'es',
output: {
format: 'es'
},
sourcemap: true,
plugins: [{
resolveId: function(id) {
Expand All @@ -179,7 +205,9 @@ describe("sourcemaps", function() {
it("should still be added when options.sourceMap is true", function() {
return collect(rollup({
input: './entry.js',
format: 'es',
output: {
format: 'es'
},
sourceMap: true,
plugins: [{
resolveId: function(id) {
Expand All @@ -197,7 +225,9 @@ describe("sourcemaps", function() {
it("should not be added otherwise", function() {
return collect(rollup({
input: './entry.js',
format: 'es',
output: {
format: 'es'
},
plugins: [{
resolveId: function(id) {
return id;
Expand Down

0 comments on commit ec10569

Please sign in to comment.