/* eslint-disable */
import gulp from 'gulp';
import Browser from 'browser-sync';
import webpack from 'webpack';
import gulpNotify from 'gulp-notify';
import { stylesheets } from './styles';
import { nunjucks } from './html';
import webpackDevMiddleware from 'webpack-dev-middleware';

import { config as webpackConfig } from './webpack';

export const browser = Browser.create();
const bundler = webpack(webpackConfig);

function reload(done) {
    browser.reload();
    done();
}

export const server = () => {

    const config = {
        server: 'dist',
        port: 3000,
        open: false,
        middleware: [
            webpackDevMiddleware(bundler, { /* options */ }),
        ]
    };

    browser.init(config);

    gulp.src('.')
        .pipe(gulpNotify('Server running on localhost'));

    gulp.watch('./src/assets/js/**/*.js').on('change', () => browser.reload());
    gulp.watch('./src/assets/scss/**/*.scss').on('change', () => stylesheets().pipe(browser.stream({match: '**/*.css'})));
    gulp.watch('./src/**/*.htm').on('change', () => gulp.series(nunjucks, reload)());
};
