gulp sass源图

时间:2016-07-18 13:22:54

标签: node.js sass gulp gulp-sass

我需要帮助将源映射添加到同一CSS输出文件夹中的SASS编译器。直到现在,我必须在gulpfile.js内安装gulp-sourcemaps模块,但无法知道将sourcemaps.write绑定为gulp.task的成功。

非常感谢任何帮助:)

var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var bs = require('browser-sync').create();

gulp.task('browser-sync', ['sass'], function() {
    bs.init({
    server: {
        baseDir: "./"
    },
    proxy: {
        target: "localhost:8080", // can be [virtual host, sub-directory, localhost with port]
        ws: true // enables websockets
    }
});
});

gulp.task('sass', function() {
    return gulp.src('scss/**/*.scss')
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('assets/css'))
        .pipe(bs.reload({
            stream: true
        }));
});

gulp.task('watch', ['browser-sync'], function () {
    gulp.watch("scss/*.scss", ['sass']);
    gulp.watch("*.php").on('change', bs.reload);
});

3 个答案:

答案 0 :(得分:12)

尝试使用此代码进行gulp任务' sass':

gulp.task('sass', function() {
    return gulp.src('scss/**/*.scss')
        .pipe(sourcemaps.init())
        .pipe(sass())
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('assets/css'))
        .pipe(bs.reload({
            stream: true
        }));
});

然后,在同一文件夹中写入源图之后,首先使用initmaps编译sass()('。')

此致

答案 1 :(得分:3)

我每天使用这个任务5个月并且工作正常,

const gulp            = require('gulp'),
      autoprefixer    = require('gulp-autoprefixer'),
      plumber         = require('gulp-plumber'),
      sass            = require('gulp-sass'),
      sourcemaps      = require('gulp-sourcemaps');

var sassSourcePath = 'YourPath/scss/**/*.scss',
    cssDestPath    = 'YourPath/css/';


gulp.task('sass', () => {

  return gulp
    .src(sassSourcePath)
    .pipe(plumber())
    .pipe(sourcemaps.init())
    .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
    .pipe(sourcemaps.write({includeContent: false}))
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(autoprefixer({ browser: ['last 2 version', '> 5%'] }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(cssDestPath));

});

还建议您使用require('gulp-csso')作为生产版本

答案 2 :(得分:2)

gulp-sass的完整解决方案,映射,计算所有文件,缩小:

./ SASS / partial_folders / index.scss

@import 'base/_reset';
@import 'helpers/_variables';
@import 'helpers/_mixins';
@import 'helpers/_functions';
@import 'base/_typography';
etc..

./ gulpfile.js

var gulp = require('gulp');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglifycss = require('gulp-uglifycss');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('styles', function(){
    return gulp
            .src('sass/**/*.scss')
            .pipe(sourcemaps.init())
            .pipe(sass().on('error', sass.logError))
            .pipe(concat('styles.css'))
            .pipe(uglifycss({
                "maxLineLen": 80,
                "uglyComments": true
            }))
            .pipe(sourcemaps.write('.'))
            .pipe(gulp.dest('./build/css/'));
});


gulp.task('default', function(){
    gulp.watch('sass/**/*.scss', ['styles']);
})