gulp postcss-simple-vars throws error at // comments

和自甴很熟 提交于 2020-01-06 23:47:49

问题


I'm attempting to migrate from Sass to postCSS and testing out postcss-simple-vars gulp plugin.

I'm using Webstorm IDE, which automatically uses javascript style comments (//) in .scss files, so my non-block comments are all // comments, not /* */.

postcss-simple-vars throws errors at all // comments, even with the silent option set to true.

Here's my gulp task:

gulp.task('postcss', function () {
    return gulp
        .src('./styles/sass2/*.scss')
        .pipe($.postcss(
            [
                vars({
                    silent: true,
                    variables: colors
                })
            ]))
        .pipe(gulp.dest('./dest'));

});

Am I missing something really obvious? Is there some way to get postcss-simple-vars to ignore // style comments?

HOW I SOLVED IT:

I replaced all the // comments with /* */ comments with gulp-replace ($.replace).

gulp.task('replace-comments', function() {
    return gulp
        .src(config.scss)
        .pipe($.replace(/\/\/.*/g, function(comment){

            return '/*' + comment.substring(2) + ( '*/');
        }))
        .pipe(gulp.dest('./styles/sass3'));

});

回答1:


The problem is not in postcss-simple-vars. It's that the default parser for postcss expects standard CSS, and // comments are not standard: they break the parser.

To run uncompiled SCSS with // comments through postcss, you should use the alternate parser postcss-scss. The documentation in that README and for gulp-postcss should explain how to use it.



来源:https://stackoverflow.com/questions/34560283/gulp-postcss-simple-vars-throws-error-at-comments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!