Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Exclude files #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ Here's [a blog post explaining it](https://60devs.com/hot-reloading-for-chrome-e
- Works by checking timestamps of files
- Supports nested directories
- Automatically disables itself in production
- And it's just a <a href="https://github.com/xpl/crx-hotreload/blob/master/hot-reload.js">50 lines of code</a>!
- And it's just <a href="https://github.com/xpl/crx-hotreload/blob/master/hot-reload.js">50 lines of code</a>!

## Options

| Name | Value | Description |
|--------------|------------------------------|----------------------------------------|
| `reloadTab` | `boolean` (default: `true`) | reload the active tab when crx changes.|
| `exclude` | `string[]` (default: `[]`) | file names that we shouldn't watch. |

## How To Use

Expand All @@ -31,4 +38,10 @@ It is also available as NPM module:
npm install crx-hotreload
```

Then use a `require` (or `import`) to execute the script.
In your script:
```
const hotReload = require('crx-hotreload');
hotReload.default({
reloadTab: false
});
```
64 changes: 41 additions & 23 deletions hot-reload.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
const filesInDirectory = dir => new Promise (resolve =>
const filesInDirectory = (dir, exclude) => new Promise (resolve =>

dir.createReader ().readEntries (entries =>

Promise.all (entries.filter (e => e.name[0] !== '.').map (e =>

Promise.all (entries
.filter (e => e.name[0] !== '.')
.filter (f => !exclude.includes(f.name))
.map (e =>
e.isDirectory
? filesInDirectory (e)
? filesInDirectory (e, exclude)
: new Promise (resolve => e.file (resolve))
))
.then (files => [].concat (...files))
.then (resolve)
)
)

const timestampForFilesInDirectory = dir =>
filesInDirectory (dir).then (files =>
files.map (f => f.name + f.lastModifiedDate).join ())
const timestampForFilesInDirectory = (dir, exclude) =>
filesInDirectory (dir, exclude).then (files =>
files. map (f => f.name + f.lastModifiedDate).join ())

const reload = () => {
const reload = (reloadTab) => {

chrome.tabs.query ({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5
if (reloadTab) {
chrome.tabs.query ({ active: true, currentWindow: true }, tabs => { // NB: see https://github.com/xpl/crx-hotreload/issues/5

if (tabs[0]) { chrome.tabs.reload (tabs[0].id) }
if (tabs[0]) { chrome.tabs.reload (tabs[0].id) }

chrome.runtime.reload ()
})
} else {
chrome.runtime.reload ()
})
}
}

const watchChanges = (dir, lastTimestamp) => {

timestampForFilesInDirectory (dir).then (timestamp => {
const watchChanges = (dir, opts, lastTimestamp) => {

timestampForFilesInDirectory (dir, opts.exclude).then (timestamp => {
if (!lastTimestamp || (lastTimestamp === timestamp)) {

setTimeout (() => watchChanges (dir, timestamp), 1000) // retry after 1s
setTimeout (() => watchChanges (dir, opts, timestamp), 1000) // poll every 1s

} else {

reload ()
reload (opts.reloadTab)
}
})

}

chrome.management.getSelf (self => {

if (self.installType === 'development') {

chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir))
const defaultOpts = {
reloadTab: true,
exclude: [],
};

if (typeof module === 'object') {
exports.default = (opts) => {
const combinedOpts = Object.assign({}, defaultOpts, opts);
chrome.management.getSelf (self => {
if (self.installType === 'development') {
chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir, combinedOpts))
}
})
}
})
} else {
chrome.management.getSelf (self => {
if (self.installType === 'development') {
chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir, defaultOpts))
}
})
}