2

我从Gulp Autoprefixer Not Working尝试了这个解决方案,但它不适合我。

我使用的代码是:

"use strict";

var gulp = require('gulp');
var sass = require('gulp-sass');
var prefix = require('gulp-autoprefixer');
var minify = require('gulp-minify-css');
var plumber = require('gulp-plumber');

function onError(err) {
    console.log(err);
}

gulp.task('sass', function(){
    return gulp.src('src/style.scss')
        .pipe(sass())
        .pipe(prefix({browsers:['last 2 versions']}))
        .pipe(minify())
        .pipe(gulp.dest('css/'))
        .pipe(plumber({
            errorHandler: onError
        }))
});

我收到此错误消息:

/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:152
        this.processing = new Promise(function (resolve, reject) {
                              ^
ReferenceError: Promise is not defined
    at LazyResult.async (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:152:31)
    at LazyResult.then (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:75:21)
    at DestroyableTransform._transform (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/index.js:28:13)
    at DestroyableTransform.Transform._read (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:10)
    at DestroyableTransform.Transform._write (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:160:12)
    at doWrite (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:326:12)
    at writeOrBuffer (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:312:5)
    at DestroyableTransform.Writable.write (/Users/krisvandermast/gulptests/area512/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:239:11)
    at write (/Users/krisvandermast/gulptests/area512/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/Users/krisvandermast/gulptests/area512/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)
4

1 回答 1

3

我在 GitHub 上为它打开了一个错误:https ://github.com/sindresorhus/gulp-autoprefixer/issues/45 。

结果是我需要将我的 Node.js 版本升级到 4。现在可以4.1.0使用以下代码:

包.json

{
  "name": "a",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Kris van der Mast",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-autoprefixer":"^3.0.1"
  }
}

gulpfile.js

"use strict";

var gulp = require('gulp'),
    autoprefixer = require('gulp-autoprefixer');

gulp.task('default', function () {
    return gulp.src('src/app.css')
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: false
        }))
        .pipe(gulp.dest('dist'));
});

应用程序.css

body {
    opacity: .5;
    box-sizing: border-box;
    transform: scale(.5);
    display: flex;
}

此时运行gulp,得到如下输出:

前缀后的 app.css

body {
    opacity: .5;
    box-sizing: border-box;
    -webkit-transform: scale(.5);
    transform: scale(.5);
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
}
于 2015-09-22T12:11:54.383 回答