Skip to content

feat(project): replace livereload with browser-sync #2073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions templates/app/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"babel-core": "^6.6.5",
"babel-eslint": "^6.0.4",
"babel-register": "^6.6.5",
"browser-sync": "^2.8.0",
"bs-fullscreen-message": "^1.0.0",
<%_ if(filters.flow) { -%>
"flow-bin": "^0.27.0",
"babel-plugin-syntax-flow": "^6.8.0",
Expand Down Expand Up @@ -92,7 +94,6 @@
"gulp-inject": "^4.0.0",
"gulp-istanbul": "~0.10.3",
"gulp-istanbul-enforcer": "^1.0.3",
"gulp-livereload": "^3.8.0",
"gulp-load-plugins": "^1.0.0-rc.1",
"gulp-clean-css": "^2.0.6",
"gulp-mocha": "^2.1.3",
Expand Down Expand Up @@ -159,7 +160,6 @@
<%# END WEBPACK %>
"through2": "^2.0.1",
"open": "~0.0.4",
"connect-livereload": "^0.5.3",
"istanbul": "~0.4.1",
"chai": "^3.2.0",
"sinon": "^1.16.1",
Expand All @@ -182,6 +182,7 @@
"jasmine-spec-reporter": "^2.4.0",<% } %>
"phantomjs-prebuilt": "^2.1.4",
"proxyquire": "^1.0.1",
"strip-ansi": "^3.0.1",
"supertest": "^1.1.0"<% if(filters.ts) { %>,
"tslint": "^3.5.0",
"typings": "^0.8.1"<% } %>
Expand Down
10 changes: 3 additions & 7 deletions templates/app/gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ gulp.task('webpack:dev', function() {
return gulp.src(webpackDevConfig.entry.app)
.pipe(plugins.plumber())
.pipe(webpack(webpackDevConfig))
.pipe(gulp.dest('.tmp'))
.pipe(plugins.livereload());
.pipe(gulp.dest('.tmp'));
});

gulp.task('webpack:dist', function() {
Expand Down Expand Up @@ -311,7 +310,7 @@ gulp.task('clean:tmp', () => del(['.tmp/**/*'], {dot: true}));

gulp.task('start:client', cb => {
whenServerReady(() => {
open('http://localhost:' + config.port);
open('http://localhost:' + config.browserSyncPort);
cb();
});
});
Expand Down Expand Up @@ -347,12 +346,9 @@ gulp.task('start:server:debug', () => {
gulp.task('watch', () => {
var testFiles = _.union(paths.client.test, paths.server.test.unit, paths.server.test.integration);

plugins.livereload.listen();

plugins.watch(_.union(paths.server.scripts, testFiles))
.pipe(plugins.plumber())
.pipe(lintServerScripts())
.pipe(plugins.livereload());
.pipe(lintServerScripts());

plugins.watch(_.union(paths.server.test.unit, paths.server.test.integration))
.pipe(plugins.plumber())
Expand Down
3 changes: 3 additions & 0 deletions templates/app/server/config/environment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var all = {
// Root path of server
root: path.normalize(`${__dirname}/../../..`),

// Browser-sync port
browserSyncPort: process.env.BROWSER_SYNC_PORT || 3000,

// Server port
port: process.env.PORT || <%= devPort %>,

Expand Down
80 changes: 37 additions & 43 deletions templates/app/server/config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var MongoStore = connectMongo(session);<% } else if(filters.sequelize) { %>
import sqldb from '../sqldb';
import expressSequelizeSession from 'express-sequelize-session';
var Store = expressSequelizeSession(session.Store);<% } %>
import stripAnsi from 'strip-ansi';

var browserSync = require('browser-sync').create();

export default function(app) {
var env = app.get('env');
Expand Down Expand Up @@ -93,52 +96,43 @@ export default function(app) {
const webpackConfig = makeWebpackConfig({ DEV: true });
const compiler = webpack(webpackConfig);

const pkgConfig = require('../../package.json');
const livereloadServer = require('tiny-lr')();
var livereloadServerConfig = {
ignore: [
/^\/api\/(.*)/,
/\.js(\?.*)?$/, /\.css(\?.*)?$/, /\.svg(\?.*)?$/, /\.ico(\?.*)?$/, /\.woff(\?.*)?$/,
/\.png(\?.*)?$/, /\.jpg(\?.*)?$/, /\.jpeg(\?.*)?$/, /\.gif(\?.*)?$/, /\.pdf(\?.*)?$/
],
port: (pkgConfig.livereload || {}).port
};
var triggerLiveReloadChanges = function() {
livereloadServer.changed({
body: {
files: [webpackConfig.output.path + webpackConfig.output.filename]
}
});
};
if(livereloadServerConfig.port) {
livereloadServer.listen(livereloadServerConfig.port, triggerLiveReloadChanges);
} else {
/**
* Get free port for livereload
* server
*/
livereloadServerConfig.port = require('http').createServer().listen(function() {
/*eslint no-invalid-this:0*/
this.close();
livereloadServer.listen(livereloadServerConfig.port, triggerLiveReloadChanges);
}).address().port;
}

/**
* On change compilation of bundle
* trigger livereload change event
* Run Browsersync and use middleware for Hot Module Replacement
*/
compiler.plugin('done', triggerLiveReloadChanges);

app.use(webpackDevMiddleware(compiler, {
stats: {
colors: true,
timings: true,
chunks: false
}
}));
browserSync.init({
open: false,
logFileChanges: false,
proxy: 'localhost:' + config.port,
ws: true,
middleware: [
webpackDevMiddleware(compiler, {
noInfo: false,
stats: {
colors: true,
timings: true,
chunks: false
}
})
],
port: config.browserSyncPort,
plugins: ['bs-fullscreen-message']
});

app.use(require('connect-livereload')(livereloadServerConfig));
/**
* Reload all devices when bundle is complete
* or send a fullscreen error message to the browser instead
*/
compiler.plugin('done', function (stats) {
console.log('webpack done hook');
if (stats.hasErrors() || stats.hasWarnings()) {
return browserSync.sockets.emit('fullscreen:message', {
title: "Webpack Error:",
body: stripAnsi(stats.toString()),
timeout: 100000
});
}
browserSync.reload();
});
}

if ('development' === env || 'test' === env) {
Expand Down