From 7485b60d45f3a7a82e9bffc644450024ee07e74a Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 8 Jun 2016 01:07:40 -0400 Subject: [PATCH 1/5] implement rollup --watch --- bin/help.md | 1 + bin/rollup | 3 ++- bin/runRollup.js | 16 +++++++++++++++- package.json | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) 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..27629b2 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -1,6 +1,7 @@ require( 'source-map-support' ).install(); var path = require( 'path' ); +var relative = require( 'require-relative' ); var handleError = require( './handleError' ); var rollup = require( '../' ); @@ -127,7 +128,20 @@ function execute ( options, command ) { }); try { - bundle( options ).catch( handleError ); + if ( command.watch ) { + let watch; + + try { + watch = relative( 'rollup-watch', process.cwd() ); + } catch ( err ) { + // TODO offer to install rollup-watch + throw err; + } + + watch( options ); + } 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": [ From fa93c5fe37abef5242641ce1b40895cf186fda55 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 8 Jun 2016 08:58:19 -0400 Subject: [PATCH 2/5] var not let --- bin/runRollup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runRollup.js b/bin/runRollup.js index 27629b2..46ac35c 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -129,7 +129,7 @@ function execute ( options, command ) { try { if ( command.watch ) { - let watch; + var watch; try { watch = relative( 'rollup-watch', process.cwd() ); From d61df97d8d7ce4e6e522fd506a12390dd81f34ef Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 8 Jun 2016 10:56:49 -0400 Subject: [PATCH 3/5] auto-install rollup-watch if requested --- bin/getRollupWatch.js | 43 +++++++++++++++++++++++++++++++++++++++++++ bin/handleError.js | 8 ++++++++ bin/runRollup.js | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 bin/getRollupWatch.js diff --git a/bin/getRollupWatch.js b/bin/getRollupWatch.js new file mode 100644 index 0000000..d830023 --- /dev/null +++ b/bin/getRollupWatch.js @@ -0,0 +1,43 @@ +var chalk = require( 'chalk' ); +var relative = require( 'require-relative' ); + +module.exports = function getRollupWatch () { + try { + watch = relative( 'rollup-watch', process.cwd() ); + return Promise.resolve( watch ); + } catch ( err ) { + if ( err.code === 'MODULE_NOT_FOUND' ) { + return installRollupWatch().then( () => { + return relative( 'rollup-watch', process.cwd() ); + }); + } else { + return Promise.reject( err ); + } + } +}; + +function installRollupWatch () { + console.error( 'rollup --watch depends on the rollup-watch package, which could not be found. You can install it globally with ' + chalk.cyan( 'npm install -g rollup-watch' ) + '. Do this now? ' + chalk.green.bold( '[y/n]' ) ); + + process.stdin.setEncoding( 'utf8' ); + + return new Promise( ( fulfil, reject ) => { + process.stdin.on( 'readable', function () { + var chunk = process.stdin.read(); + if ( !chunk ) return; + + if ( /^y(?:es)?$/i.test( chunk ) ) { + console.error( 'installing rollup-watch...' ); + child = require( 'child_process' ).exec( 'npm install --global rollup-watch', err => { + if ( err ) { + reject( err ); + } else { + fulfil(); + } + }); + } else { + reject( new Error( 'aborted' ) ); + } + }); + }); +} diff --git a/bin/handleError.js b/bin/handleError.js index b7cd224..33ee847 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( 'failed to find or install 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/runRollup.js b/bin/runRollup.js index 46ac35c..91a3df4 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -1,8 +1,9 @@ require( 'source-map-support' ).install(); var path = require( 'path' ); -var relative = require( 'require-relative' ); var handleError = require( './handleError' ); +var getRollupWatch = require( './getRollupWatch' ); +var chalk = require( 'chalk' ); var rollup = require( '../' ); // log to stderr to keep `rollup main.js > bundle.js` from breaking @@ -129,16 +130,34 @@ function execute ( options, command ) { try { if ( command.watch ) { - var watch; - - try { - watch = relative( 'rollup-watch', process.cwd() ); - } catch ( err ) { - // TODO offer to install rollup-watch - throw err; + if ( !options.entry || !options.dest ) { + handleError({ code: 'WATCHER_MISSING_INPUT_OR_OUTPUT' }); } - watch( options ); + getRollupWatch() + .then( watch => { + const watcher = watch( rollup, options ); + + watcher.on( 'event', 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( handleError ); } else { bundle( options ).catch( handleError ); } From 138d255af45209716b2c11ca549125a76219227e Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 8 Jun 2016 11:49:50 -0400 Subject: [PATCH 4/5] on second thoughts, dont auto-install. will cause problems in many cases --- bin/getRollupWatch.js | 43 --------------------------------- bin/handleError.js | 2 +- bin/runRollup.js | 55 +++++++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 69 deletions(-) delete mode 100644 bin/getRollupWatch.js diff --git a/bin/getRollupWatch.js b/bin/getRollupWatch.js deleted file mode 100644 index d830023..0000000 --- a/bin/getRollupWatch.js +++ /dev/null @@ -1,43 +0,0 @@ -var chalk = require( 'chalk' ); -var relative = require( 'require-relative' ); - -module.exports = function getRollupWatch () { - try { - watch = relative( 'rollup-watch', process.cwd() ); - return Promise.resolve( watch ); - } catch ( err ) { - if ( err.code === 'MODULE_NOT_FOUND' ) { - return installRollupWatch().then( () => { - return relative( 'rollup-watch', process.cwd() ); - }); - } else { - return Promise.reject( err ); - } - } -}; - -function installRollupWatch () { - console.error( 'rollup --watch depends on the rollup-watch package, which could not be found. You can install it globally with ' + chalk.cyan( 'npm install -g rollup-watch' ) + '. Do this now? ' + chalk.green.bold( '[y/n]' ) ); - - process.stdin.setEncoding( 'utf8' ); - - return new Promise( ( fulfil, reject ) => { - process.stdin.on( 'readable', function () { - var chunk = process.stdin.read(); - if ( !chunk ) return; - - if ( /^y(?:es)?$/i.test( chunk ) ) { - console.error( 'installing rollup-watch...' ); - child = require( 'child_process' ).exec( 'npm install --global rollup-watch', err => { - if ( err ) { - reject( err ); - } else { - fulfil(); - } - }); - } else { - reject( new Error( 'aborted' ) ); - } - }); - }); -} diff --git a/bin/handleError.js b/bin/handleError.js index 33ee847..21a100d 100644 --- a/bin/handleError.js +++ b/bin/handleError.js @@ -30,7 +30,7 @@ var handlers = { }, ROLLUP_WATCH_NOT_INSTALLED: function ( err ) { - console.error( chalk.red( 'failed to find or install rollup-watch' ) ); + 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 ) { diff --git a/bin/runRollup.js b/bin/runRollup.js index 91a3df4..20b6264 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -1,8 +1,8 @@ require( 'source-map-support' ).install(); var path = require( 'path' ); +var relative = require( 'require-relative' ); var handleError = require( './handleError' ); -var getRollupWatch = require( './getRollupWatch' ); var chalk = require( 'chalk' ); var rollup = require( '../' ); @@ -134,30 +134,35 @@ function execute ( options, command ) { handleError({ code: 'WATCHER_MISSING_INPUT_OR_OUTPUT' }); } - getRollupWatch() - .then( watch => { - const watcher = watch( rollup, options ); - - watcher.on( 'event', 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( handleError ); + try { + var watch = relative( 'rollup-watch', process.cwd() ); + var watcher = watch( rollup, options ); + + watcher.on( 'event', 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 ); } From f2671c0519817bc42b8915215b175cdecffb0e30 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 8 Jun 2016 11:55:04 -0400 Subject: [PATCH 5/5] oops i did it again --- bin/runRollup.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runRollup.js b/bin/runRollup.js index 20b6264..38d5339 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -138,7 +138,7 @@ function execute ( options, command ) { var watch = relative( 'rollup-watch', process.cwd() ); var watcher = watch( rollup, options ); - watcher.on( 'event', event => { + watcher.on( 'event', function ( event ) { switch ( event.code ) { case 'STARTING': console.error( 'checking rollup-watch version...' );