我正在编写我的gulpfile.js扫描主题目录中的style.scss文件,其目的是读取style.scss文件并编写相应的style.css& .min文件在同一目录中。我遇到的问题是我找不到编写css文件的方法而不知道目录是什么......我不会。
这可以用gulp.dest()吗?
tl; dr:基本上......如何确定正在处理的* .scss文件的当前路径,以便将* .css文件放在同一目录中
// GULP variable declarations
var gulp = require('gulp'),
gutil = require('gulp-util'),
sass = require('gulp-ruby-sass'),
prefix = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');
// Paths array
var path = {
scss: [
'docroot/profile/theme/**/*/style.scss',
],
watch_scss: [
'docroot/profile/theme/**/*.scss',
],
};
// Process SASS functionality
gulp.task('process-scss', function() {
return gulp.src(path.scss)
.pipe(sass({
compass: true,
style: 'expanded',
}))
.pipe(prefix(['last 2 versions']))
.pipe(concat('style.css'))
.pipe(gulp.dest('./relative/dir')
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest('./relative/dir')
.on('error', gutil.log);
});
// Setup the gulp WATCH functionality
gulp.task('default', function() {
gulp.start('process-scss');
gulp.watch(path.watch_scss, ['process-scss']);
});
答案 0 :(得分:6)
只是添加,事实证明你可以使用gulp.dest()为文件的基本URL提供以下代码段。
https://github.com/gulpjs/gulp/blob/master/docs/API.md#path
gulp.dest(function(file) {
return file.base;
}
编辑:我的最终gulp任务看起来像这样:
// File path variable declarations
var stylePath = "";
var scriptPath = "";
// Paths array
var path = {
scss: [
'docroot/profile/theme/**/style/scss/style.scss',
],
watch_scss: [
'docroot/profile/theme/**/style/scss/**/*.scss',
],
theme_base: [
'docroot/profile/theme/',
],
};
// Process SASS functionality
gulp.task('process-scss', function() {
return gulp.src(path.scss)
.pipe(gulp.dest(function(file) {
var relative = file.relative.split("/");
stylePath = path.theme_base + relative[0] + '/style/';
return file.base;
}))
.pipe(sass({
compass: true,
style: 'expanded',
}))
.pipe(prefix(['last 2 versions']))
.pipe(concat('style.css'))
.pipe(gulp.dest(function() { return stylePath; }))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest(function() { return stylePath; }))
.on('error', gutil.log);
});
答案 1 :(得分:4)
gulp.dest
将写入保留其文件夹结构的文件。将基本文件夹提供给gulp.dest
,它将写入。{/ p>
所以你有这个
gulp.src('docroot/profile/theme/**/*/style.scss')
和gulp.dest('dist/profile/theme')
docroot/profile/theme/sample/style.scss
转为dist/profile/theme/sample/style.scss
。