jrobinsonc
9/3/2017 - 3:02 AM

BrowserSync using a middleware

BrowserSync using a middleware

browserSync.init({
    middleware: [
        // Displaying views
        function (req, res, next) {
            if (req.url === '/') {
                req.url = '/views/index.html';
            } else if (/^\/[^\/]+$/.test(req.url)) {
                const file = `${__dirname}/dist/views${req.url}.html`;

                if (fs.existsSync(file)) {
                    req.url = `/views${req.url}.html`;
                }
            }

            return next();
        }
    ],
    server: {
        baseDir: config.destDir
        // serveStaticOptions: {
        //     extensions: ['html']
        // }
    },
    watchEvents: ['change', 'add', 'unlink'],
    ghostMode: false,
    notify: false
});