2

I am trying to setup browser-sync on my gulp pipeline for development but I can't seem to get the reload working. A browser tab is opened when I run gulp start but reload doesn't cause subsequent browser refresh.

Steps that I follow in my gulpfile -

gulp start - Build ts, copy js, libs and index.html. Start express server. Init browser-sync. Setup watch on source files.

Also, there's this error that I see only when I run with browser-sync image I do not see the above error when I run standard - gulp node dist/app.js

I have searched all over the place but can't find an example which leads me to believe that there is something wrong with my gulp workflow.

Also something that may come in handy is that when I open browser sync UI - Chrome dev tools - issued error

I can confirm that the <body> tag is present and this is what my source looks like on browser -

<html>

<head>
    <base href="/">
    <title>Angular 2 QuickStart</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body><script type='text/javascript' id="__bs_script__">//<![CDATA[
    document.write("<script async src='/browser-sync/browser-sync-client.2.11.1.js'><\/script>".replace("HOST", location.hostname));
//]]></script>

<script src="lib/es6-shim.min.js"></script>
    <script src="lib/system-polyfills.js"></script>
    <script src="lib/shims_for_IE.js"></script>
    <script src="lib/angular2-polyfills.js"></script>
    <script src="lib/system.src.js"></script>
    <script src="lib/Rx.js"></script>
    <script src="lib/angular2.dev.js"></script>
    <script src="lib/router.dev.js"></script>
    <script src="lib/http.dev.js"></script>
    <script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        }
    });
    System.import('app/main')
        .then(null, console.error.bind(console));
    </script>
    <my-app>Loading...</my-app>
</body>

</html>

I can see the browser-sync script tag rendered here.

I am totally at loss here. Could someone help me out with this/ identify if this is an issue with browser-sync itself?

Here's my gulpfile -

var gulp = require('gulp');
var tsc = require('gulp-typescript');
var tsProject = tsc.createProject('client/tsconfig.json');
var sourcemaps = require('gulp-sourcemaps');
var config = require('./gulp.config')();
var del = require('del');
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var node;
var spawn = require('child_process').spawn;
var path = require('path');

gulp.task('clean', function() {
    return del(config.distDir);
});

gulp.task('server', ['clean'], function() {
    return gulp.src(config.nodeHost)
        .pipe(gulp.dest(config.distDir))
});

gulp.task('lib', ['clean'], function() {
    return gulp.src(config.angularLibraries)
        .pipe(gulp.dest(path.join(config.distDir,'client','lib')));
});

gulp.task('compile-ts', ['clean'], function() {
    var sourceTsFiles = [
        config.allTs,
        config.typings
    ];

    var tsResult = gulp.src(sourceTsFiles)
        .pipe(sourcemaps.init())
        .pipe(tsc(tsProject));

    return tsResult.js
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(path.join(config.distDir,'client')));
});

gulp.task('index', ['compile-ts', 'lib', 'clean'], function() {
    return gulp.src(config.indexFile)
        .pipe(gulp.dest(path.join(config.distDir,'client')));
});

gulp.task('build', ['compile-ts', 'lib', 'index', 'server']);
gulp.task('default', ['build']);

// Deveopment serve tasks - start

gulp.task('watch', function() {
    gulp.watch(['./app.js', '!./dist/**', './client/**'], ['stop', 'reload']);
});

gulp.task('browser-sync', ['nodestart'], function() {
    browserSync.init(null, {
        proxy: "http://localhost:3001",
        port: 7000,
    });
});

gulp.task('reload-browser-sync', ['nodestart'], function() {
    reload();
});

gulp.task('nodestart', ['build'], function() {
    node = spawn('node', ['dist/app.js'], { stdio: 'inherit' })
    node.on('close', function(code) {
        if (code === 8) {
            gulp.log('Error detected, waiting for changes...');
        }
    });
});

gulp.task('start', ['build', 'nodestart', 'watch', 'browser-sync']);
gulp.task('reload', ['build', 'nodestart', 'reload-browser-sync']);
gulp.task('stop', function() {
    if (node) node.kill();
});

// Deveopment serve tasks - end

Here's the repo - https://github.com/saumyatripathi/angular_2_gulp_workflow

EDIT: Here's the browser console when I run it without browser-sync. No error on console

EDIT: For anyone interested in tracking the progress of the issue I raised on browser-sync repo, here's the link - https://github.com/BrowserSync/browser-sync/issues/1037

Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
stripathi
  • 766
  • 11
  • 23
  • Read from this [comment](https://github.com/angular/angular/issues/7652#issuecomment-198744682). There are issues in beta.11 related to zones. – Eric Martinez Mar 20 '16 at 23:50
  • Thanks for the comment. However, if this was the issue, would it also not pop up with regular build where I just build and host without watch and browser-sync? – stripathi Mar 21 '16 at 00:13
  • Yes, it would, as in this question http://stackoverflow.com/q/36120808/4933038, the problem is zone. You could go back to beta.9 and wait for beta.12. – Eric Martinez Mar 21 '16 at 00:24
  • See my edit. In any case, do you think this browser error could be preventing browser-sync from binding to chrome? – stripathi Mar 21 '16 at 00:42
  • I couldn't really say, I don't use browser-sync. As I see in your edit without browser-sync you have no errors, but I've seen other [issues](https://github.com/angular/angular/issues/7652) related to zone and not to browser-sync. – Eric Martinez Mar 21 '16 at 00:53
  • Oh ok no worries. There was another error popping up after chrome update but new version of shim fixed it. Thanks for your input anyway. – stripathi Mar 21 '16 at 00:58
  • 1
    I edited the title of the question to make it more generic so other users can reach it easily. I also added an answer in case it helps. Thanks for editing and adding the issue you opened! @stripathi – Eric Martinez Mar 21 '16 at 22:45

3 Answers3

2

For reference besides the issue OP created in BrowserSync repo, per this comment from Misko (quoting)

The error is introduced by browser-sync-client.2.11.1.js trying to clearInteval(undefined)

Which was solved in this pull request and will land in zone.js 0.6.5 with beta.12 (see #7700)

Update

Regarding this comment from @shakyShane, BrowserSync's author, zone.js 0.6.5 may not really solve this issue. Follow that issue and those comments for more information.

Update 2

According to @stripathi's feedback this issue was solved in beta.12 and zone.js 0.6.6.

Eric Martinez
  • 31,277
  • 9
  • 92
  • 91
  • well I downloaded zone 0.6.5 and it didn't fix. Is that not it? Are there changes in angular too that will come out with beta12? Sorry for my complete ignorance on this. – stripathi Mar 22 '16 at 02:14
  • Yes, you're using `angular2-polyfills` and that bundle will be updated when the new beta is released. Even now that you've installed zone.js 0.6.5 the polyfills still have the previous version. – Eric Martinez Mar 22 '16 at 02:18
  • Ok I'm an inch away from marking this as an answer. Just one last favor, would you know if I can patch something (polyfill maybe?) manually to make this work for the time being? – stripathi Mar 22 '16 at 02:22
  • Actually the correctness of your answer does not depend on you being my tutor so let me know if you get a chance. Meanwhile, I'm marking this as answer as for all intents and purposes, this comes as close as it gets to identifying issue and providing info. – stripathi Mar 22 '16 at 02:28
  • @stripathi as testing you could try the following [structure](http://pastebin.com/ZQHV0x3F). Note that I commented `angular2-polyfills` and I added manually references to `reflect-metadata` and `zone` (polyfills contain those two). So by pointing to `node_modules/...` you should be loading the latest version you installed. – Eric Martinez Mar 22 '16 at 02:33
  • still doesn't work but that's fine I'll wait. Thanks for all the help @Eric – stripathi Mar 22 '16 at 02:52
  • @stripathi I think you'll be really interested in this [comment](https://github.com/angular/angular/issues/7705#issuecomment-200019429) from BrowserSync author. – Eric Martinez Mar 22 '16 at 21:05
  • Yeah I've been following the thread. He also suggests the same thing as you did. I think I'll just wait for beta12. It's supposedly going to be released this week. I'll try it out and post back the findings. – stripathi Mar 24 '16 at 11:57
  • @stripathi beta.12 was already released and your original issue seems to be solved, but there are another one that was supposed to have been solved as well but it's still there, see https://github.com/angular/zone.js/issues/290 – Eric Martinez Mar 24 '16 at 11:59
  • Yes I got this issue when I tried the fix you suggested. Now I feel like I should have reported my findings. – stripathi Mar 24 '16 at 12:34
  • 2
    Woah just tried it and it works perfectly. I have angular beta.12 and zone0.6.6. No errors at all. – stripathi Mar 25 '16 at 01:17
  • @stripathi thanks for the feedback, updating the answer according to your comment. – Eric Martinez Mar 25 '16 at 01:28
0

Weird fact. I changed lite-server to live-server and the error is gone. Besides, with lite-server updates on the app did not cause page refresh, with live-server page refreshes automatically.

yagger
  • 2,975
  • 1
  • 16
  • 21
0

Just update to the current version: Angular 2.0.0-beta.12, zone.js 0.6.6, and remove es6-promise from the peerDependency. As you can see in the latest CHANGELOG

It's working for me. I hope it helps to others.

Jesús Sobrino
  • 387
  • 2
  • 5