UNPKG

2.7 kBMarkdownView Raw
1<div align="center">
2 <img src="https://github.com/marshallcb/jeye/raw/main/docs/jeye.png" alt="jeye" width="75" />
3</div>
4
5<h1 align="center">jeye</h1>
6<div align="center">
7 <a href="https://npmjs.org/package/jeye">
8 <img src="https://badgen.now.sh/npm/v/jeye" alt="version" />
9 </a>
10</div>
11
12<div align="center">Watch files and their dependencies for changes</div>
13
14## Overview
15- Watch .js files and their dependencies for changes (combination of [`chokidar`](https://github.com/paulmillr/chokidar) and [`esm-module-lexer`](https://github.com/guybedford/es-module-lexer))
16- Run singular callbacks and aggregate callbacks
17- Requires JS files to be written in ES6 syntax (for import/export analysis)
18- Great for bundle-less build tooling
19
20## Usage
21
22```js
23// CJS
24var { watch } = require('jeye');
25// ES6
26import { watch } from 'jeye';
27
28watch('source', {
29 ignore: /(^|[\/\\])[\._]./ //ignore dot files and files with underscore prefix (_hidden.js)
30}).on('change', (p, { exports, imports, code }) => {
31 console.log(p + ' changed')
32}).on('remove', p => {
33 console.log(p + ' removed')
34}).on('aggregate', (targets, changed) => {
35 console.log(changed.length + ' files changed')
36}).on('ready', (targets) => {
37 console.log("READY")
38})
39```
40
41## API
42
43### `watch(source, options?)`
44
45- `source` : `[String]` or `String` pointing to either directories or individual files
46- `options`
47 - `ignore`: Regex to match all filenames that should be ignored
48 - `only`: Regex to match all files that should be included
49 - `chokidar`: Object to be passed to chokidar options [API](https://github.com/paulmillr/chokidar#api)
50
51### .on(event, callback)
52
53Returns instance of watcher (to allow for chained listeners)
54
55#### Events:
56
57- `change` : `(path, scriptInfo) => { }`
58 - `path` : path relative to cwd of the changed file
59 - `scriptInfo` : only available for JS files with ES6 syntax
60 - `imports` : list of imports exported by the changed file (from [`esm-module-lexer`](https://github.com/guybedford/es-module-lexer))
61 - `exports` : list of exports exported by the changed file (from [`esm-module-lexer`](https://github.com/guybedford/es-module-lexer))
62 - `code` : String of source code for that file (using utf8 encoding)
63
64- `remove` : `(path) => { }`
65 - `path` : path relative to cwd of the deleted file
66
67- `aggregate` : `(total, changed) => { }`
68 - `total`: Object with all target paths as keys and `{ imports, exports, code }` as value
69 - `changed`: Number of target files affected by the most recent edit
70
71- `ready` : `(total, changed) => { }`
72 - `total`: Object with all target paths as keys and `{ imports, exports, code }` as value
73
74
75## License
76
77MIT © [Marshall Brandt](https://m4r.sh)
\No newline at end of file