Skip to content

Commit 588ebc1

Browse files
committed
chore(server): replaced LiveReload with Browsersync
1 parent fcd881c commit 588ebc1

2 files changed

Lines changed: 61 additions & 95 deletions

File tree

gulpfile.js

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var gulp = require('gulp'),
66
minimist = require('minimist'),
77
wiredep = require('wiredep').stream,
88
plugins = require('gulp-load-plugins')(),
9-
server = require('tiny-lr')(),
9+
bs = require('browser-sync').create(),
1010
config = require('./config.json'),
1111
pkg = require('./package.json');
1212

@@ -18,7 +18,8 @@ var gulp = require('gulp'),
1818
var knownOptions = {
1919
boolean: ['nobrowser', 'notest']
2020
};
21-
var options = minimist(process.argv.slice(2), knownOptions);
21+
var args = minimist(process.argv.slice(2), knownOptions);
22+
2223

2324

2425

@@ -35,23 +36,6 @@ var processWinPath = function (file) {
3536
};
3637

3738
// Compile SASS and add prefixes
38-
var fnSass = function (path) {
39-
return gulp.src(path)
40-
.on('data', processWinPath)
41-
.pipe(plugins.plumber())
42-
.pipe(plugins.sourcemaps.init())
43-
.pipe(plugins.sass())
44-
.pipe(plugins.sourcemaps.write())
45-
.on('error', function (err) {
46-
console.log(err.message);
47-
// process.exit(1);
48-
})
49-
.pipe(plugins.size({ showFiles: true, title: '[CSS]' }))
50-
.pipe(gulp.dest(config.build + '/assets'))
51-
.on('end', function () {
52-
require('fs').unlink(path);
53-
});
54-
};
5539
gulp.task('styles:sass:imports', function () {
5640
var files = [config.app + '/+(sass|app|common)/**/*.scss', '!' + config.app + '/sass/includes/*.scss', '!' + config.app + '/+(app|common)/**/_*.scss'];
5741
return gulp.src(files, { read: false })
@@ -68,7 +52,22 @@ gulp.task('styles:sass:imports', function () {
6852
});
6953
gulp.task('styles:sass', ['styles:sass:imports'], function () {
7054
var files = config.build + '/assets/app.scss';
71-
return fnSass(files);
55+
return gulp.src(files)
56+
.on('data', processWinPath)
57+
.pipe(plugins.plumber())
58+
.pipe(plugins.sourcemaps.init())
59+
.pipe(plugins.sass())
60+
.pipe(plugins.sourcemaps.write())
61+
.on('error', function (err) {
62+
console.log(err.message);
63+
// process.exit(1);
64+
})
65+
.pipe(plugins.size({ showFiles: true, title: '[CSS]' }))
66+
.pipe(gulp.dest(config.build + '/assets'))
67+
.on('end', function () {
68+
require('fs').unlink(files);
69+
})
70+
.pipe(bs.stream());
7271
});
7372

7473

@@ -106,7 +105,8 @@ var fnCacheTpls = function (path) {
106105
standalone: true
107106
}))
108107
.pipe(plugins.concat('templates.js'))
109-
.pipe(gulp.dest(config.build + '/app'));
108+
.pipe(gulp.dest(config.build + '/app'))
109+
.pipe(bs.stream());
110110
};
111111
gulp.task('scripts:cacheTpls', function () {
112112
return fnCacheTpls(config.paths.templates);
@@ -124,7 +124,8 @@ var fnLint = function (path, exitOnError) {
124124
}
125125
cb(null, file);
126126
}))
127-
.pipe(gulp.dest(config.build));
127+
.pipe(gulp.dest(config.build))
128+
.pipe(bs.stream());
128129
};
129130
gulp.task('scripts:lint', function () {
130131
return fnLint(config.paths.scripts, true);
@@ -138,7 +139,8 @@ gulp.task('scripts:lint', function () {
138139
// Copy assets
139140
var fnImg = function (path) {
140141
return gulp.src(path, { base: config.app })
141-
.pipe(gulp.dest(config.build));
142+
.pipe(gulp.dest(config.build))
143+
.pipe(bs.stream());
142144
};
143145
gulp.task('assets:img', function () {
144146
return fnImg(config.paths.assets);
@@ -174,7 +176,8 @@ var fnInject = function (path) {
174176
addRootSlash: false,
175177
ignorePath: ['/', config.build + '/']
176178
}))
177-
.pipe(gulp.dest(config.build));
179+
.pipe(gulp.dest(config.build))
180+
.pipe(bs.stream());
178181
};
179182
gulp.task('html:inject', ['styles:sass', 'scripts:lint', 'scripts:cacheTpls', 'wiredep'], function () {
180183
return fnInject(config.build + '/index.html');
@@ -264,59 +267,58 @@ gulp.task('test:watch', ['scripts:lint', 'scripts:cacheTpls', 'styles:sass', 'ht
264267
// Set up Watch
265268
// ============
266269

267-
// Add files to Watch
268-
var watchTasks = ['styles:sass', 'scripts:lint', 'scripts:cacheTpls', 'assets:img', 'test:watch', 'html:inject'];
269-
if (options.notest) {
270-
watchTasks.splice(watchTasks.indexOf('test:watch'), 1);
271-
}
270+
gulp.task('watch', ['styles:sass', 'scripts:lint', 'scripts:cacheTpls', 'assets:img', 'html:inject'], function () {
271+
var runSequence = require('run-sequence');
272272

273-
gulp.task('watch', watchTasks, function () {
274-
require('./server.js')(server, options);
273+
bs.init({
274+
logPrefix: 'Browsersync',
275+
open: !args.nobrowser,
276+
reloadOnRestart: true,
277+
server: {
278+
baseDir: './build',
279+
routes: {
280+
'/vendor': './vendor'
281+
},
282+
}
283+
284+
});
275285

276286
// watch for JS changes
277287
gulp.watch(config.paths.scripts, function (event) {
278-
if (event.path.lastIndexOf('.js') === event.path.length - 3) {
279-
if (event.type === 'deleted') {
288+
switch (event.type) {
289+
case 'deleted':
280290
del(event.path.replace(config.app, config.build));
281-
} else {
282-
return fnLint(event.path).pipe(plugins.livereload(server));
283-
}
284-
}
285-
});
286-
287-
// remove deleted JS files from index.html
288-
gulp.watch(config.build + '/+(app|common)/**/*.js', function (event) {
289-
if (event.type !== 'changed') {
290-
return fnInject(config.paths.html).pipe(plugins.livereload(server));
291+
return fnInject(config.build + '/index.html');
292+
case 'added':
293+
runSequence('scripts:lint', function () {
294+
return fnInject(config.build + '/index.html');
295+
});
296+
break;
297+
default:
298+
return fnLint(event.path);
291299
}
292300
});
293301

294302
// watch AngularJS templates to cache
295-
gulp.watch(config.app + '/+(app|common)/**', function (event) {
296-
if (event.path.lastIndexOf('.tpl.html') === event.path.length - 9) {
297-
return fnCacheTpls(config.paths.templates).pipe(plugins.livereload(server));
298-
}
299-
});
303+
gulp.watch(config.app + '/+(app|common)/**/*.tpl.html', ['scripts:cacheTpls']);
300304

301305
// watch for SASS changes
302-
var runSequence = require('run-sequence');
303-
gulp.watch(config.paths.sass, function (event) {
304-
runSequence('styles:sass:imports', function () {
305-
var files = config.build + '/assets/app.scss';
306-
return fnSass(files).pipe(plugins.livereload(server));
307-
});
308-
});
306+
gulp.watch(config.paths.sass, ['styles:sass']);
309307

308+
// watch for assets changes
310309
gulp.watch(config.paths.assets, function (event) {
311310
if (event.type === 'deleted') {
312311
del(event.path.replace(config.app, config.build));
313312
} else {
314-
return fnImg(event.path).pipe(plugins.livereload(server));
313+
return fnImg(event.path);
315314
}
316315
});
317316

318-
gulp.watch(config.paths.html, function (event) {
319-
return fnInject(event.path).pipe(plugins.livereload(server));
317+
// watch for index.html changes
318+
gulp.watch(config.paths.html, function () {
319+
runSequence('wiredep', function () {
320+
return fnInject(config.build + '/index.html');
321+
});
320322
});
321323
});
322324

@@ -339,7 +341,7 @@ gulp.task('clean:dist', function (cb) {
339341

340342
gulp.task('build', ['clean:build'], function () {
341343
var buildTasks = ['styles:sass', 'scripts:lint', 'scripts:cacheTpls', 'test:run', 'assets:img', 'html:inject'];
342-
if (options.notest) {
344+
if (args.notest) {
343345
buildTasks.splice(buildTasks.indexOf('test:run'), 1);
344346
}
345347
gulp.start(buildTasks);

server.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)