简体   繁体   English

通过Gulp和BrowserSync进行SASS + CSS注入

[英]SASS + CSS Injecting with Gulp and BrowserSync

Just getting started with BrowserSync and I have a question regarding the common use-cases on configuration for SASS + CSS injecting. 刚开始使用BrowserSync时,我有一个关于SASS + CSS注入配置的常见用例的问题。

What I'm a little confused about is weather I can give the server property its own name. 我有点困惑的是,我可以给server属性起自己的名字。

In the docs here they use "./app" . 这里的文档中他们使用"./app" Can I use another name like "./site" or am I only allowed to use "./app" ? 我可以使用"./site"类的其他名称还是只能使用"./app"

Here's the code 这是代码

// task 
gulp.task('serve',['sass'], function (){

    // static server 
    browserSync.init({
        server: "./app"
    });

    // watching html and scss files for changes then reloading browser
    gulp.watch("app/scss/*.scss", ['sass']);
    gulp.watch("app/*.html").on('change', browserSync.reload);

});

UPDATED 更新

If you're looking to set up browserSync as a static server with live reloading you're pretty much right with your approach. 如果您希望通过实时重载将browserSync设置为静态服务器,则您的方法非常正确。 You can point browsersync to any folder in your repository using the baseDir option. 您可以使用baseDir选项将baseDir存储库中的任何文件夹。 You can also use multiple directories. 您也可以使用多个目录。 For further info check out the options documentation . 有关更多信息,请查看选件文档

First. 第一。 I would look to split up the tasks you have. 我希望将您的任务分解。

Personally, I would opt to have my server task something like; 就个人而言,我会选择执行服务器任务,例如:

gulp.task('serve', ['build'], function(event) {
  var server = browsersync.create();
  server.init({
    baseDir: 'app/'
  });
  return server.watch('app/**', function(evt, file) {
    server.reload();
  });
});

Note how build is a dependency. 注意build是如何依赖的。 This ensures that all of our served files are in place when we initiate BrowserSync. 这样可以确保在启动BrowserSync时,所有提供的文件都到位。

Keeping it generic we can then split the SCSS related tasks into their own tasks such as scss:compile and scss:watch . 保持通用性,然后可以将SCSS相关任务拆分为自己的任务,例如scss:compilescss:watch

gulp.task('scss:compile', function(){
    return gulp.src('src/scss/**/*.scss')
        .pipe(plumber())
        .pipe(scss())
        .pipe(gulp.dest('app/css/');
});

and

gulp.task('scss:watch', function() {
    gulp.watch('src/scss/**/*.scss', ['scss:compile']);
});

For CSS injection. 用于CSS注入。 There are two options for doing this 有两种选择

Remember injection differs from reload. 请记住,注入不同于重新加载。 Whereas reload will reset the page state, injection won't affect the current page state and justs injects the styles. 重新加载会重置页面状态,注入不会影响当前页面状态,只会注入样式。

Option one is to invoke browsersync.stream() with the piped content of your SASS compilation like follows; 第一种方法是使用SASS编译的管道内容来调用browsersync.stream() ,如下所示;

gulp.task('scss:compile', function() {
  return gulp.src('src/scss/**/*.scss')
    .pipe(plumber())
    .pipe(scss())
    .pipe(gulp.dest('app/css')
    .pipe(browsersync.stream());
});

Note that the last pipe injects the changes. 请注意,最后一个管道将注入更改。 This also requires to check the file type in the BrowserSync watch to only reload when a non CSS file changes. 这还需要检查BrowserSync监视中的文件类型,以便仅在非CSS文件更改时才重新加载。

This option is OK. 此选项确定。 However, I think it's not clear to have it just bundled on the end there and separate from BrowserSync setup code. 但是,我认为尚不清楚是否仅将其捆绑在一端并与BrowserSync设置代码分开。

The second option( personally preferred ) is to watch for changes with BrowserSync and if a CSS file changes, use vinyl to stream those changes to BrowserSync. 第二个选项( 个人首选 )是使用BrowserSync监视更改,如果CSS文件发生更改,请使用vinyl将这些更改流式传输到BrowserSync。

Something like the following tends to work; 诸如此类的东西往往会起作用;

var gulp      = require('gulp'),
  browsersync = require('browser-sync'),
  vss         = require('vinyl-source-stream'),
  vb          = require('vinyl-buffer'),
  vf          = require('vinyl-file');

gulp.task('serve', ['build'], function() {
  var server = browsersync.create();
  server.init({ baseDir: 'app/' });
  return server.watch('app/**', function(evt, file) {
    if (evt === 'change' && file.indexOf('.css') === -1)
      server.reload();
    if (evt === 'change' && file.indexOf('.css') !== -1)
      vf.readSync(file)
        .pipe(vss(file))
        .pipe(vb())
        .pipe(server.stream());
  });
});

Hope that helps you out! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM