为什么gulp无法将scss文件转换为css文件?

时间:2019-12-20 08:55:31

标签: css sass gulp

我是使用口香糖的初学者。

我正在使用Visual Studio Code Editor-1.41.1版本。 我的操作系统是Windows7。

我已经在gulpjs.com上安装了带有指导的gulp。刷新链接在这里:https://gulpjs.com/。 但是,当我在Visual Studio代码编辑器中键入命令:gulp sass时,我的scss文件未转换为css文件。并且,编辑器改为显示以下消息。

The term 'gulp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of  the name, or if a path was included, verify that the path is correct and try again. At line:1 char:5
+ gulp <<<<  sass
    + CategoryInfo          : ObjectNotFound: (gulp:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

我还检查了gulpfile.js中的路径。我认为路径没有问题。 我的gulpfile.js正在关注。

var gulp = require('gulp');
var sass = require('gulp-sass');

sass.compiler = require('node-sass');

gulp.task('sass', function () {
  return gulp.src('project/src/sass/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('project/dist/css'));
});

您能告诉我为什么以及如何解决此问题吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

尝试一下,我已经为gulpJs 4创建了此代码。 可能对您有帮助

运行此命令

npm install gulp-concat gulp-autoprefixer gulp-sass gulp-sass-glob node-sass --save-dev 

如果使用的是Linux,请使用Sudo

    const { src, dest } = require("gulp");
const concat = require("gulp-concat");
const autoprefixer = require("gulp-autoprefixer");
const sass = require("gulp-sass");
const sassGlob = require("gulp-sass-glob");
sass.compiler = require("node-sass");



function css() {
    return src("src/css/*.css")
        .pipe(concat("style.css"))
        .pipe(sassGlob())
        .pipe(
            sass({
                outputStyle: "compressed" //expand or compact or compressed
            }).on("error", sass.logError)
        )
        .pipe(
            autoprefixer({
                cascade: true
            })
        )
        .pipe(dest("build/css/"));
}


function scss() {
    return src('src/scss/style.scss') // import your all file in style.scss
        .pipe(sassGlob())
        .pipe(
            sass({
                outputStyle: 'compressed' // you can set "expand or compact or compressed" view 
            })
                .on('error', sass.logError)
        ).pipe(
            autoprefixer({
                cascade: true
            })
        ).pipe(dest('build/scss/'));
}

exports.css = css;
exports.scss= scss;
相关问题