var gulp = require('gulp');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var postcss = require('gulp-postcss');
var atImport = require("postcss-import");
var nested = require('postcss-nested');
var autoprefixer = require('autoprefixer-core');
var cssnext = require('cssnext');
var precss = require('precss');
var nano = require('gulp-cssnano');
var rename = require('gulp-rename');
var cp = require('child_process');
var jade = require('gulp-jade');
var plumber = require('gulp-plumber');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var paths = {
'bower': './bower_components/',
'assets': './assets/'
};
var messages = {
jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};
gulp.task('jekyll-build', function (done) {
browserSync.notify(messages.jekyllBuild);
return cp.spawn('jekyll', ['build'], {stdio: 'inherit'})
.on('close', done);
});
gulp.task('jekyll-rebuild', ['jekyll-build'], function () {
browserSync.reload();
});
gulp.task('browser-sync', ['styles', 'jekyll-build'], function() {
browserSync({
server: {
baseDir: '_site'
}
});
});
gulp.task('styles', function() {
var processors = [
atImport,
cssnext({}),
precss({}),
nested,
autoprefixer
];
return gulp.src('assets/css/main.css')
.pipe(postcss(processors))
.pipe(gulp.dest('_site/assets/css'))
.pipe(nano())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('assets/css'))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('scripts', function() {
gulp.src([
paths.bower + '/jquery/dist/jquery.js',
paths.bower + '/Materialize/dist/js/materialize.js',
paths.assets + '/js/main.js'
])
.pipe(plumber())
.pipe(concat('main.js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('assets/js'));
return gulp.src([
paths.bower + '/modernizr/modernizr.js'
])
.pipe(gulp.dest('assets/js'))
.pipe(reload({stream: true}));
});
gulp.task('jade', function() {
return gulp.src('_jadefiles/*.jade')
.pipe(jade())
.pipe(gulp.dest('_includes'));
});
gulp.task('watch', function () {
gulp.watch('assets/css/**', ['styles']);
gulp.watch('assets/js/**', ['jekyll-rebuild']);
gulp.watch(['index.html', '_layouts/*.html', '_posts/*'], ['jekyll-rebuild']);
gulp.watch('_jadefiles/*.jade', ['jade']);
});
gulp.task('default', ['browser-sync', 'scripts', 'jade', 'watch']);
这个gulpfile.js的灵感主要来自使用Jade和Jekyll的[ShakyShane] [1]。
我是gulp.js的新手,想要应用postcss来增强我当前的工作流程,但是即使在终端中检测到它们也不会在浏览器中发生变化。希望你们能帮忙。