diff --git a/bin/handleError.js b/bin/handleError.js index b7cd224..21a100d 100644 --- a/bin/handleError.js +++ b/bin/handleError.js @@ -27,6 +27,14 @@ var handlers = { DUPLICATE_IMPORT_OPTIONS: function ( err ) { console.error( chalk.red( 'use --input, or pass input path as argument' ) ); + }, + + ROLLUP_WATCH_NOT_INSTALLED: function ( err ) { + console.error( chalk.red( 'rollup --watch depends on the rollup-watch package, which could not be found. You can install it globally (recommended) with ' ) + chalk.cyan( 'npm install -g rollup-watch' ) ); + }, + + WATCHER_MISSING_INPUT_OR_OUTPUT: function ( err ) { + console.error( chalk.red( 'must specify --input and --output when using rollup --watch' ) ); } }; diff --git a/bin/help.md b/bin/help.md index 46886c7..7b02ed1 100644 --- a/bin/help.md +++ b/bin/help.md @@ -9,6 +9,7 @@ Basic options: -h, --help Show this help message -c, --config Use this config file (if argument is used but value is unspecified, defaults to rollup.config.js) +-w, --watch Watch files in bundle and rebuild on changes -i, --input Input (alternative to ) -o, --output Output (if absent, prints to stdout) -f, --format [es6] Type of output (amd, cjs, es6, iife, umd) diff --git a/bin/rollup b/bin/rollup index 200c4bb..123f388 100755 --- a/bin/rollup +++ b/bin/rollup @@ -20,7 +20,8 @@ command = minimist( process.argv.slice( 2 ), { n: 'name', o: 'output', u: 'id', - v: 'version' + v: 'version', + w: 'watch' } }); diff --git a/bin/runRollup.js b/bin/runRollup.js index ea8ade9..38d5339 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -1,7 +1,9 @@ require( 'source-map-support' ).install(); var path = require( 'path' ); +var relative = require( 'require-relative' ); var handleError = require( './handleError' ); +var chalk = require( 'chalk' ); var rollup = require( '../' ); // log to stderr to keep `rollup main.js > bundle.js` from breaking @@ -127,7 +129,43 @@ function execute ( options, command ) { }); try { - bundle( options ).catch( handleError ); + if ( command.watch ) { + if ( !options.entry || !options.dest ) { + handleError({ code: 'WATCHER_MISSING_INPUT_OR_OUTPUT' }); + } + + try { + var watch = relative( 'rollup-watch', process.cwd() ); + var watcher = watch( rollup, options ); + + watcher.on( 'event', function ( event ) { + switch ( event.code ) { + case 'STARTING': + console.error( 'checking rollup-watch version...' ); + break; + + case 'BUILD_START': + console.error( 'bundling...' ); + break; + + case 'BUILD_END': + console.error( 'bundled in ' + event.duration + 'ms. Watching for changes...' ); + break; + + default: + console.error( 'unknown event', event ); + } + }); + } catch ( err ) { + if ( err.code === 'MODULE_NOT_FOUND' ) { + err.code = 'ROLLUP_WATCH_NOT_INSTALLED'; + } + + handleError( err ); + } + } else { + bundle( options ).catch( handleError ); + } } catch ( err ) { handleError( err ); } diff --git a/package.json b/package.json index e6d87b9..dba86ad 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "dependencies": { "chalk": "^1.1.1", "minimist": "^1.2.0", + "require-relative": "^0.8.7", "source-map-support": "^0.4.0" }, "files": [