Beispiel #1
0
module.exports = function(app) {
    if (!app || process.env.NO_LIVERELOAD) return;

    // livereload middleware
    // serves the script injected by development.blocks/livereload template
    app.use(tinyLr.middleware({ app, dashboard: true }));
};
Beispiel #2
0
gulp.task('server', function() {
    var app = express();

    // ajax 
    app.use(getPath(path.join(appConfig.contextPath, ajaxPath)), headerStatic(path.join(mockDir, ajaxPath), {
        'Content-Type': 'application/json'
    }));
    // mimg
    app.use(path.join('/hxm', appConfig.product), headerStatic(buildStaticDir, {}));
    // default files
    app.use(appConfig.contextPath, headerStatic(buildWebappDir, {}));
    // for ftl 
    app.use(appConfig.contextPath, function(req, res, next) {
        ftlExpress(req, res, next, appConfig['ftl2html-server']);
    });
    // livereload middleware
    app.use(body()).use(tinylr.middleware({
        app: app
    }));
    app.listen(appConfig.port, function(err) {
        if (err) {
            return console.log(err);
        }
        // 设置了默认打开页面
        if (appConfig.openurl) {
            openurl.open(appConfig.openurl);
        }

        console.log('listening on %d', appConfig.port);
    });
});
Beispiel #3
0
gulp.task('serve', function() {
    var app = express();

    // ajax 
    app.use(getPath(path.join(appConfig.contextPath, ajaxPath)), headerStatic(path.join(mockDir, ajaxPath), {
        'Content-Type': 'application/json'
    }));
    // js
    app.use(jsPath, headerStatic(path.join(webappDir, jsPath), {}));
    // css
    app.use(cssPath, headerStatic(path.join(webappDir, cssPath), {}));
    // images
    app.use(imgPath, headerStatic(path.join(webappDir, imgPath), {}));
    // html
    app.use(appConfig.contextPath, headerStatic(webappDir, {}));

    // for ftl
    app.use(appConfig.contextPath, function(req, res, next) {
        ftlExpress(req, res, next, appConfig['ftl2html']);
    });

    // livereload middleware
    app.use(body()).use(tinylr.middleware({
        app: app
    }));

    app.listen(appConfig.port, function(err) {
        if (err) {
            return console.log(err);
        }
        // 设置了默认打开页面
        if (appConfig.openurl) {
            openurl.open(appConfig.openurl);
        }

        console.log('listening on %d', appConfig.port);
    });


    livereload.listen();
    gulp.watch('src/main/webapp/js/*.js', ['scripts']);
    gulp.watch('src/main/webapp/css/*.css', ['styles']);
    gulp.watch('src/main/webapp/*.html', ['views']);

    function watchFiles(ext) {
        // watch
        gulp.watch(path.join(webappDir, '**/*.' + ext), function(event) {
            tinylr.changed(event.path);
        });
    }
    watchFiles('js');
    watchFiles('html');
    watchFiles('ftl');
    watchFiles('css');
});
  start: function( options ) {
    options = options || {};

    var port = process.env.PORT || options.port || this._port;

    app.use( body.json() );
    app.use( body.urlencoded( {
      extended: true
    } ) );

    app.use( tinylr.middleware( {
      app: app
    } ) );

    app.listen( port, function() {
      console.log( 'Livereload server started on ' + port );
    } );

  }
Beispiel #5
0
gulp.task('serve', function() {
    var app = express();

    // ajax 
    app.use(getPath(path.join(appConfig.contextPath, ajaxPath)), headerStatic(path.join(mockDir, ajaxPath), {
        'Content-Type': 'application/json'
    }));

    // html
    // app.use('/', headerStatic('./', {}));
    app.use(appConfig.contextPath, headerStatic(webappDir, {}));

    // livereload middleware
    app.use(body()).use(tinylr.middleware({
        app: app
    }));

    app.listen(appConfig.port, function(err) {
        if (err) {
            return console.log(err);
        }
        // 设置了默认打开页面
        if (appConfig.openurl) {
            openurl.open(appConfig.openurl);
        }

        console.log('listening on %d', appConfig.port);
    });


    function watchFiles(ext) {
        // watch
        gulp.watch(path.join(webappDir, '**/*.' + ext), function(event) {
            tinylr.changed(event.path);
        });   
    }
    watchFiles('js');
    watchFiles('html');
    watchFiles('css');
});
Beispiel #6
0
module.exports = function (app) {
  // livereload (add ?lr to url to activate - watches served paths)
  if (app.program.livereload) {
    // process lr to add livereload script to page
    app.hooks.beforesend.push(function (str, req) {
      if (req.query.lr != null) {
        let lrTag = `\
<script src="/livereload.js?snipver=1"></script>
`
        let $ = cheerio.load(str)
        if ($('script').length) {
          $('script').eq(0).before(lrTag)
          str = $.html()
        } else {
          str = lrTag + str
        }
      }
      return str
    })

    app.server.use(tinylr.middleware({app: app.server}))

    // would have preferred to use https://www.npmjs.com/package/watchr but breaks on node v0.10.3
    // this method has serious caveats: https://nodejs.org/api/fs.html#fs_caveats
    // The recursive option is only supported on OS X and Windows.
    // Should probably use fs.watchFile as fallback method
    watch(path.resolve(app.program.args[0] || process.cwd()), { recursive: true }, function (evt, filename) {
    // watch(path.resolve(app.program.args[0] || process.cwd()), function (filename) {
      let m = filename.match(/\.([^.]+)$/)
      let extension = m != null ? m[1] : undefined
      if (!!extension && (app.handlers[extension] || app.mimes[extension])) {
        let servedFilename = filename.replace(RegExp(`${m[1]}$`), (app.handlers[extension] != null ? app.handlers[extension].chain : undefined) || extension)
        request(`http://127.0.0.1:${app.program.port}/changed?files=` + servedFilename, (e, response, body) => console.log(`livereloaded due to change: ${filename}`))
      }
    })
  }
}