rollup-watch
Advanced tools
Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "rollup-watch", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Watch files for changes and perform incremental rebuilds with Rollup", | ||
@@ -32,8 +32,9 @@ "main": "dist/rollup-watch.cjs.js", | ||
"rollup": "^0.28.0", | ||
"rollup-plugin-buble": "^0.10.0" | ||
"rollup-plugin-buble": "^0.10.0", | ||
"rollup-plugin-json": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"chokidar": "^1.5.2", | ||
"require-relative": "^0.8.7" | ||
"semver": "^5.1.0" | ||
} | ||
} |
import buble from 'rollup-plugin-buble'; | ||
import json from 'rollup-plugin-json'; | ||
export default { | ||
entry: 'src/index.js', | ||
plugins: [ buble() ], | ||
plugins: [ json(), buble() ], | ||
targets: [ | ||
@@ -7,0 +8,0 @@ { dest: 'dist/rollup-watch.cjs.js', format: 'cjs' }, |
129
src/index.js
@@ -0,76 +1,95 @@ | ||
import EventEmitter from 'events'; | ||
import * as chokidar from 'chokidar'; | ||
import relative from 'require-relative'; | ||
import { sequence } from './utils/promise.js'; | ||
import { name, version } from '../package.json'; | ||
import checkVersion from './utils/checkVersion.js'; | ||
export default function watch ( options ) { | ||
let rollup; | ||
export default function watch ( rollup, options ) { | ||
const emitter = new EventEmitter(); | ||
try { | ||
rollup = relative( 'rollup', process.cwd() ); | ||
} catch ( err ) { | ||
// TODO handle gracefully | ||
throw err; | ||
} | ||
process.nextTick( () => emitter.emit( 'event', { code: 'STARTING' }) ); | ||
let watchedIds; | ||
let rebuildScheduled = false; | ||
let building = false; | ||
checkVersion( name, version ) | ||
.catch( err => { | ||
if ( err.code === 'OUT_OF_DATE' ) { | ||
// TODO offer to update | ||
console.error( `rollup-watch is out of date (you have ${err.localVersion}, latest version is ${err.latestVersion}). Update it with npm install -g rollup-watch` ); // eslint-disable-line no-console | ||
} | ||
}) | ||
.then( () => { | ||
let watchedIds; | ||
let rebuildScheduled = false; | ||
let building = false; | ||
let watcher; | ||
let watcher; | ||
function triggerRebuild () { | ||
rebuildScheduled = true; | ||
if ( !building ) build(); | ||
} | ||
function triggerRebuild () { | ||
rebuildScheduled = true; | ||
if ( !building ) build(); | ||
} | ||
function build () { | ||
if ( building ) return; | ||
function build () { | ||
if ( building ) return; | ||
rebuildScheduled = false; | ||
building = true; | ||
let start = Date.now(); | ||
let initial = !watcher; | ||
return rollup.rollup( options ) | ||
.then( bundle => { | ||
const moduleIds = bundle.modules.map( module => module.id ); | ||
emitter.emit( 'event', { code: 'BUILD_START' }); | ||
if ( !watcher ) { | ||
watcher = chokidar.watch( moduleIds, { | ||
persistent: true, | ||
ignoreInitial: true | ||
}); | ||
rebuildScheduled = false; | ||
building = true; | ||
watcher.on( 'change', triggerRebuild ); | ||
watcher.on( 'unlink', triggerRebuild ); | ||
return rollup.rollup( options ) | ||
.then( bundle => { | ||
const moduleIds = bundle.modules.map( module => module.id ); | ||
watchedIds = moduleIds; | ||
} else { | ||
moduleIds.forEach( id => { | ||
if ( !~watchedIds.indexOf( id ) ) { | ||
watcher.add( id ); | ||
if ( !watcher ) { | ||
watcher = chokidar.watch( moduleIds, { | ||
persistent: true, | ||
ignoreInitial: true | ||
}); | ||
watcher.on( 'change', triggerRebuild ); | ||
watcher.on( 'unlink', triggerRebuild ); | ||
watchedIds = moduleIds; | ||
} else { | ||
moduleIds.forEach( id => { | ||
if ( !~watchedIds.indexOf( id ) ) { | ||
watcher.add( id ); | ||
} | ||
}); | ||
watchedIds.forEach( id => { | ||
if ( !~moduleIds.indexOf( id ) ) { | ||
watcher.unwatch( id ); | ||
} | ||
}); | ||
} | ||
}); | ||
watchedIds.forEach( id => { | ||
if ( !~moduleIds.indexOf( id ) ) { | ||
watcher.unwatch( id ); | ||
if ( options.targets ) { | ||
return sequence( options.targets, target => { | ||
const mergedOptions = Object.assign( {}, options, target ); | ||
return bundle.write( mergedOptions ); | ||
}); | ||
} | ||
}); | ||
} | ||
if ( options.targets ) { | ||
return sequence( options.targets, target => { | ||
const mergedOptions = Object.assign( {}, options, target ); | ||
return bundle.write( mergedOptions ); | ||
return bundle.write( options ); | ||
}) | ||
.then( () => { | ||
emitter.emit( 'event', { | ||
code: 'BUILD_END', | ||
duration: Date.now() - start, | ||
initial | ||
}); | ||
building = false; | ||
if ( rebuildScheduled ) build(); | ||
}); | ||
} | ||
} | ||
return bundle.write( options ); | ||
}) | ||
.then( () => { | ||
building = false; | ||
if ( rebuildScheduled ) build(); | ||
}); | ||
} | ||
build(); | ||
}); | ||
build(); | ||
return emitter; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5424
11
115
5
1
+ Addedsemver@^5.1.0
+ Addedsemver@5.7.2(transitive)
- Removedrequire-relative@^0.8.7
- Removedrequire-relative@0.8.7(transitive)