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 11, 2017
1 parent 150aba8 commit 48a0bd0
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 34 deletions.
51 changes: 38 additions & 13 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 All @@ -21,15 +37,15 @@ module.exports = function rollupStream(options) {
}).then(function(result) {
// don't look at me. this is how Rollup does it.
var defaultLoader = require.extensions['.js'];

require.extensions['.js'] = function(m, filename) {
if(filename === optionsPath) {
m._compile(result.code, filename);
} else {
defaultLoader(m, filename);
}
};

try {
return require(optionsPath);
} finally {
Expand All @@ -39,38 +55,47 @@ module.exports = function rollupStream(options) {
} else {
options = Promise.reject(Error("options must be an object or a string!"));
}

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) {
rollup = require('rollup');
hasCustomRollup = false;
}

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 All @@ -81,6 +106,6 @@ module.exports = function rollupStream(options) {
stream.emit('error', reason);
});
});

return stream;
};
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
70 changes: 50 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe("rollup-stream", function() {
it("should export a function", function() {
expect(rollup).to.be.a('function');
});

it("should return a readable stream", function() {
expect(rollup()).to.be.an.instanceof(Readable);
});

it("should emit an error if options isn't passed", function(done) {
var s = rollup();
s.on('error', function(err) {
Expand All @@ -38,9 +38,13 @@ describe("rollup-stream", function() {
done(Error("No error was emitted."));
});
});

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 @@ -49,11 +53,24 @@ describe("rollup-stream", function() {
done(Error("No error was emitted."));
});
});


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 @@ -67,15 +84,18 @@ describe("rollup-stream", function() {
expect(data).to.have.string('Hello, World!');
});
});

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 @@ -86,11 +106,13 @@ describe("rollup-stream", function() {
expect(data).to.equal('fake code');
});
});

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 All @@ -104,13 +126,13 @@ describe("rollup-stream", function() {
expect(data).to.have.string('Hello, World!');
});
});

it("should import config from the specified file if options is a string", function() {
return collect(rollup('test/fixtures/config.js')).then(function(data) {
expect(data).to.have.string('Hello, World!');
});
});

it("should reject with any error thrown by the config file", function(done) {
var s = rollup('test/fixtures/throws.js');
s.on('error', function(err) {
Expand All @@ -121,11 +143,13 @@ describe("rollup-stream", function() {
done(Error("No error was emitted."));
});
});

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 @@ -175,11 +201,13 @@ describe("sourcemaps", function() {
expect(data).to.have.string('\n//# sourceMappingURL=data:application/json;');
});
});

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 @@ -193,11 +221,13 @@ describe("sourcemaps", function() {
expect(data).to.have.string('\n//# sourceMappingURL=data:application/json;');
});
});

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 48a0bd0

Please sign in to comment.