几天前我开始使用gulp,我不得不解决很多问题,在非编译语言中修复错误非常困难,而且他们的运行时间也很慢。
不幸的是,我发现的最后一个问题非常不同,它没有给出任何错误,只是把文件从流中吃掉了。
为了用较少的文字总结问题,这里是gulpfile.js的部分。
gulp.src([ folder.src + "scss/vendor/_bootstrap.scss" ], { allowEmpty: true })
.pipe(debug()) // file exists
.pipe(sourcemaps.init())
.pipe(debug()) // file exists
.pipe(sass().on('error', sass.logError)) // scss to css
.pipe(debug()) // file doesn't exist
.pipe(autoprefixer({
overrideBrowserslist: ["last 2 versions"]
}))
.pipe(debug())
.pipe(cleanCSS()) // minifies css
.pipe(rename({
// rename bootstrap.css to bootstrap.min.css
suffix: ".min"
}))
.pipe(sourcemaps.write("./")) // source maps for bootstrap.min.css
.pipe(gulp.dest(out));
前两条debug语句输出的是gulp管道的流中有一个文件,而在运行了gulpfile. sass()
管道上的文件立即消失,没有通知或错误。
[20:21:45] Starting 'css'...
[20:21:45] gulp-debug: src/main/resources/assets/scss/vendor/_bootstrap.scss
[20:21:45] gulp-debug: src/main/resources/assets/scss/vendor/_bootstrap.scss
[20:21:45] gulp-debug: 1 item
[20:21:45] gulp-debug: 1 item
[20:21:45] gulp-debug: 0 items
[20:21:45] gulp-debug: 0 items
[20:21:45] Finished 'css' after 169 ms
是不是很奇怪?我该如何解决这个问题。没有堆栈跟踪,没有错误,没有警告:(
其实文件前缀有下划线(_
)被sass函数忽略了。可能是因为它们代表了 sass 的 partials。
通过重命名和使用简单的regex对流进行管道化处理就可以解决这个问题。
gulp.src([ folder.src + "scss/vendor/_bootstrap.scss" ], { allowEmpty: true })
// ...
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/^_/, ''); // renames files starting with _
return opt;
}))
.pipe(sass())
// ...