question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gulp 4 style injection

See original GitHub issue

I’m attempting to use browser-sync with Gulp 4 and the new bs .stream() syntax, but bs is not preserving state and does a full refresh. Have you folks created a recipe / best practice for gulp v4 yet, so I can compare notes. Thanks for this awesome tool.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13

github_iconTop GitHub Comments

9reactions
JamesJosephFinncommented, Jan 15, 2016

@iliakan sure:

var config = {
    server: {
        baseDir: './dev/'
    },
    files: [
        './dev/*.css'
    ],
    browser: 'google chrome canary',
    notify: false
};

gulp.task('browserSync', function() {
    browserSync.init(config);
});

and then a separate file for css task setup:

var config = {
    src: './src/css/styles.css',
    dest: {
        dev: './dev/'
    }
};

gulp.task('css:dev', function() {
    return gulp
        .src(config.src)
        .pipe(foo()) // whatever you're into
        .pipe(gulp.dest(config.dest.dev));
});

watching is a joy in gulp4:

var config = {
    paths: {
        css: './src/css/**/*'
    }
};

gulp.task('watch:css', function() {
    return gulp.watch(config.paths.css,
        gulp.series('css:dev'));
});

gulp.task('watch', gulp.parallel('watch:css'));

and then the default task… I’ve only shown css related stuff above, to keep this a simple illustration of live style injection using browser-sync and gulp4:

gulp.task('default',
    gulp.series(
        'clean:dev',
        gulp.parallel(
            'copy:dev', 'html:dev', 'css:dev',
            'js:dev', 'svgSymbol:dev', 'svg:dev'
        ),
        gulp.parallel('watch', 'browserSync')
    )
);
6reactions
JamesJosephFinncommented, Jan 15, 2016

Fixed. Here’s where I went wrong:

The browser-sync constructor takes an options object, which can include a files array. Most of the tutorials I’ve read, including the gulpfile for Google’s very own Web Starter Kit, do not include this. As it turns out, this is required for style injection to preserve state.

Furthermore, do not pass .stream() or .reload() as the final pipe in your styles task. It is not needed, and will short circuit style injection, forcing a full refresh.

Finally, the browserSync process must not be terminated, and watch and browserSync tasks must execute in parallel in order for live style injection to take place.

Hope this helps anybody facing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

gulp-style-inject - npm
A plugin for Gulp. Inject css file into html style tags.. Latest version: 0.1.3, last published: 2 years ago. Start using gulp-style-inject ......
Read more >
Setting up gulp 4 for automatic Sass compilation and CSS ...
Step 1: Sass/Scss compilation · Step 2: Automating tasks · Step 3: Make it DRYer · Step 4: Let's add sourcemaps, autoprefixer and...
Read more >
Gulp 4 & BrowserSync: Style Injection? - Stack Overflow
The browser-sync constructor takes an options object, which can include a files array. Most of the tutorials I've read, including the gulpfile ......
Read more >
Just Sharing My Gulpfile - CSS-Tricks
Now that I could use Gulp 4.x, I re-wrote my gulpfile.js into smaller ... Runs a web server (Browsersync) for style injection and...
Read more >
Gulp 4 Tutorial with Node JS, ImageMin, Browser Sync, SASS ...
This is completely updated tutorial video on Gulp 4.0 with NodeJS, BrowserSync, SASS, SourceMaps, CleanCSS , AutoPrefixer, Gulp Changed, ...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found