swappie

A very light, barebones Node module to help make programmatic code hot reloading possible

Usage no npm install needed!

<script type="module">
  import swappie from 'https://cdn.skypack.dev/swappie';
</script>

README

Swappie

A very light, barebones Node module to help make programmatic code hot reloading possible

Install

Swappie is an npm module, so you can install it easily with npm, just like below:

npm i swappie

Usage

const Swappie = require('swappie');
const Path = require('path');

// Initialize
const swappie = new Swappie({
    watchPath: Path.join(process.cwd(), 'reloadables'),
});

// We can also listen to FS events
swappie.on('all', (event, path) => console.log('[+] Event'));

/**
 * Example logic only;
 * Run this script and change the file below to see changes appear live.
 */
setInterval(() => require('./reloadables/module1.js')(), 3000);

API

Swappie is a very thin wrapper around Chokidar. The API in turn is rather similar when defining paths.

Swappie({ watchPath, ignored? }) - class

Exported class that should be instantiated

  • watchPath: String, or an array of strings, referring to file paths, directories, or glob patterns. Required.
  • ignored: anymatch-compatible definition, referring to files/paths that should be ignored. Default: [].

uncache(path)

Deletes an entry from the require cache

  • path: String, path pointing to the module

watch(path)

Adds another file or directory to be watched

  • path: String, or an array of strings, referring to file paths, directories, or glob patterns.

unwatch(path)

Removes file or directory from being watched

  • path: String, or an array of strings, referring to file paths, directories, or glob patterns.

Examples

Examples of Swappie can be found within the repository.

Todo

  • Rewrite README
  • Rewrite event forwarding