From 708fb9821b877ddf36ac58ec928b2df285e7ccfd Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 29 Dec 2016 17:43:51 -0500 Subject: [PATCH] warn on missing format (fixes #1197) --- src/Bundle.js | 6 ++---- src/rollup.js | 12 +++++++++++- test/cli/sourcemap-newline/_config.js | 2 +- test/test.js | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Bundle.js b/src/Bundle.js index 79cd8d0..a532b84 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -398,8 +398,6 @@ export default class Bundle { options.format = 'es'; } - const format = options.format || 'es'; - // Determine export mode - 'default', 'named', 'none' const exportMode = getExportMode( this, options ); @@ -409,7 +407,7 @@ export default class Bundle { timeStart( 'render modules' ); this.orderedModules.forEach( module => { - const source = module.render( format === 'es', this.legacy ); + const source = module.render( options.format === 'es', this.legacy ); if ( source.toString().length ) { magicString.addSource( source ); @@ -446,7 +444,7 @@ export default class Bundle { const indentString = getIndentString( magicString, options ); - const finalise = finalisers[ format ]; + const finalise = finalisers[ options.format ]; if ( !finalise ) throw new Error( `You must specify an output type - valid options are ${keys( finalisers ).join( ', ' )}` ); timeStart( 'render format' ); diff --git a/src/rollup.js b/src/rollup.js index dfb537d..c3e6379 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -67,7 +67,17 @@ export function rollup ( options ) { return bundle.build().then( () => { timeEnd( '--BUILD--' ); - function generate ( options ) { + function generate ( options = {} ) { + if ( !options.format ) { + bundle.warn({ + code: 'MISSING_FORMAT', + message: `No format option was supplied – defaulting to 'es'`, + url: `https://github.com/rollup/rollup/wiki/JavaScript-API#format` + }); + + options.format = 'es'; + } + timeStart( '--GENERATE--' ); const rendered = bundle.render( options ); diff --git a/test/cli/sourcemap-newline/_config.js b/test/cli/sourcemap-newline/_config.js index 1427619..8433548 100644 --- a/test/cli/sourcemap-newline/_config.js +++ b/test/cli/sourcemap-newline/_config.js @@ -2,7 +2,7 @@ const assert = require( 'assert' ); module.exports = { description: 'adds a newline after the sourceMappingURL comment (#756)', - command: 'rollup -i main.js -m inline', + command: 'rollup -i main.js -f es -m inline', result: code => { assert.equal( code.slice( -1 ), '\n' ); } diff --git a/test/test.js b/test/test.js index 789ac44..13f92e5 100644 --- a/test/test.js +++ b/test/test.js @@ -133,6 +133,25 @@ describe( 'rollup', function () { assert.ok( code[ code.length - 1 ] === '\n' ); }); }); + + it( 'warns on missing format option', () => { + const warnings = []; + + return rollup.rollup({ + entry: 'x', + plugins: [ loader({ x: `console.log( 42 );` }) ], + onwarn: warning => warnings.push( warning ) + }).then( bundle => { + bundle.generate(); + compareWarnings( warnings, [ + { + code: 'MISSING_FORMAT', + message: `No format option was supplied – defaulting to 'es'`, + url: `https://github.com/rollup/rollup/wiki/JavaScript-API#format` + } + ]); + }); + }); }); describe( 'bundle.write()', () => {