1.0.13 • Published 1 year ago

blunt-livereload v1.0.13

Weekly downloads
3
License
ISC
Repository
github
Last release
1 year ago

Description

Live Reload script, mainly designed for usage in programmatic environments like Gulp.

How it works:

It contains 3 entities - LiveReloadServer, FrontClient, BackClient. You start liveReloadServer and BackClient, and include FrontClient script in your html file. Now whenever you change your html/css or bundle new js file, BackClient sends signal to LiveReloadServer, which sends signal to FrontClient in your html page in browser - 'hey, new content available, pls reload'. Then browser automatically reloads, and you see new content, without the need to press F5.

You can detect changes in your html/css/js files via gulp. Also webpack have watch functionality. So you can send liveReload signals when gulp/webpack are finished processing files.

Installation

npm i -D blunt-livereload

Simple gulp example

gulpfile.js

const { listen, makeBackClient, makeLrServer } = require('blunt-livereload');
const gulp = require('gulp');

const { series } = gulp;

const startLrServer = async () => {
  const lrServer = makeLrServer();
  return listen(lrServer);
};

const backClient = makeBackClient();
const reloadBrowser = async () => backClient.notifyWindowReload();

const paths = {
  dest: 'build',
  public: { src: 'src/**/*' },
};

const copyPublic = () =>
  gulp.src(paths.public.src, { since: gulp.lastRun(copyPublic) }).pipe(gulp.dest(paths.dest));

const watch = async () => {
  gulp.watch(paths.public.src, series(copyPublic, reloadBrowser));
};

const start = series(startLrServer, watch);

module.exports = { start };

index.html

<!DOCTYPE html>
<html lang="en">
...
  <body>
    ...
    <script src="/frontClient.js"></script>
  </body>
</html>

Copy frontClient.js from node_modules/blunt-livereload/dist/frontClient.js to your public folder

Then type npx gulp start

Gulp + Webpack example

gulpfile.js

const { listen, makeBackClient, makeLrServer } = require('blunt-livereload');
const gulp = require('gulp');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');

const { series } = gulp;

const startLrServer = async () => {
  const lrServer = makeLrServer();
  return listen(lrServer);
};

const backClient = makeBackClient();
const reloadBrowser = async () => backClient.notifyWindowReload();

const compiler = webpack(webpackConfig);
const startWebpackWatch = done => {
  compiler.hooks.done.tap('done', async () => reloadBrowser());
  compiler.watch({}, done);
};

const start = series(startLrServer, startWebpackWatch);

module.exports = { start };

webpack.config.js

Then type npx gulp start

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

0.1.4

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

4 years ago