【问题标题】:Gulpfile.js requestGulpfile.js 请求
【发布时间】:2016-04-08 20:02:45
【问题描述】:

我正在尝试使用 autoprefixer、sass 和 livereload 运行 gulpfile。我花了整个晚上但我无法运行它们,它到处爆炸T.T

谁能提供安装了这些插件的教程或 gulpfile 吗?

非常感谢:D

gulpfile.js

'use strict';

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

gulp.task('sass', function () {
    gulp.src('public_html/style/scss/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: false
        }))
        .pipe(gulp.dest('css'))
        .pipe(livereload());
});

gulp.task('watch', function () {
    livereload.listen(35729);
    gulp.watch('public_html/style/scss/**/*.scss', ['sass']);
});

来自终端的错误日志:

[22:46:02] Using gulpfile /var/www/satbalma/Gulpfile.js
[22:46:02] Starting 'watch'...
[22:46:03] Finished 'watch' after 357 ms
[22:48:43] Starting 'sass'...
[22:48:43] Finished 'sass' after 22 ms

/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:157
    this.processing = new Promise(function (resolve, reject) {
                          ^
ReferenceError: Promise is not defined
at LazyResult.async (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:157:31)
at LazyResult.then (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:79:21)
at DestroyableTransform._transform (/var/www/satbalma/node_modules/gulp-autoprefixer/index.js:24:6)
at DestroyableTransform.Transform._read (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
at DestroyableTransform.Transform._write (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
at doWrite (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:313:64)
at writeOrBuffer (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:302:5)
at DestroyableTransform.Writable.write (/var/www/satbalma/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:241:11)
at DestroyableTransform.ondata (/var/www/satbalma/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:531:20)
at DestroyableTransform.emit (events.js:95:17)

【问题讨论】:

标签: javascript gulp gulp-sass


【解决方案1】:

我之前遇到过这个错误。 将以下内容添加到您的 package.json

"es6-promise": "~3.1.2",
"es6-promise-polyfill": "~1.2.0",

并在您的gulpfile.js 中执行:

var Promise      = require('es6-promise').Promise;

应该有帮助

【讨论】:

  • 我想我需要调用 npm install promise promise-polyfill --save-dev no?
  • 好的,好的,它工作正常!但是,livereload 只重新加载 sass 文件,我想重新加载我工作目录中的所有文件,包括 js 脚本、php 等
【解决方案2】:

您可以像这样使用普通的autoprefixergulp-postcss

npm i --save-dev gulp-postcss autoprefixer

gulpfile.js

'use strict';

var gulp = require('gulp'),
    sass = require('gulp-sass'),
    livereload = require('gulp-livereload'),
    postcss = require('gulp-postcss'),
    autoprefixer = require('autoprefixer')({ browsers: ['last 2 versions'] });

gulp.task('sass', function () {
    gulp.src('public_html/style/scss/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(postcss([autoprefixer]))
        .pipe(gulp.dest('css'))
        .pipe(livereload());
});

gulp.task('watch', function () {
    livereload.listen(35729);
    gulp.watch('public_html/style/scss/**/*.scss', ['sass']);
});

【讨论】:

    【解决方案3】:

    最后,我修复了 ES6-Promise 的所有问题,正如 @Unamata Sanatarai 所说,但是当我对所有文件进行更改时,gulp 没有重新加载 web,仅在 .scss 文件中。

    现在使用这段代码,livereload 监视我项目中的所有文件:

    gulp.watch(['app/**/*.*', 'public_html/**/*.*', 'utils/**/*.*']).on('change', function(file) {livereload.changed(file.path);});
    

    app/**/*.* ... utils/**/*.* 是我的项目目录。使用此代码,我跳过 node_modules 和供应商文件夹(在我的情况下),但如果需要,您可以扩展它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 2015-05-25
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 2015-05-03
      • 2017-04-09
      相关资源
      最近更新 更多