Compile less and output minify and non-minify files - gulp

I have a task in Gulp that compile a less file in css. I want to output the non-minify and the minify version with sourcemaps. I can't find a soluction.
This is my actual task that should do this job:
return gulp.src(['./src/less/main.less'])
.pipe(sourcemaps.init())
.pipe(less({
paths: ['node_modules'],
}))
.pipe(sourcemaps.write('.'))
.pipe(cssnano())
.pipe(rename(function (path) {
console.log(path);
path.basename = path.basename.split('.')[0] + '.min' + (path.basename.split[1] || ''); //to change the name from main.css to main.min.css
}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/css/'));
I get an error because cssnano want minify the map file. Can someone post his complete task with that feature?
Thank you in advance!

Something like this using 'gulp-clone' package:
var files = gulp.src(['./src/less/main.less']);
files
.pipe(clone())
.pipe(sourcemaps.init())
.pipe(less())
.pipe(cssnano())
.pipe(sourcemaps.write('.'));
files
.pipe(sourcemaps.init())
.pipe(less())
.pipe(sourcemaps.write('.'));

Related

script files not loading in rev-manifest.json (gulp)

I have this gulpfile.js where I'm minifying my js files the problem is that when I'm doing gulp build this task is creating the minified js files but not entering the key-value pairs in the rev-manifest.json.
gulp.task('js', function (done) {
console.log('minifying js...');
gulp.src('./assets/**/*.js')
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest('./public/assets'))
.pipe(rev.manifest({
cwd: 'public',
merge: true
}))
.pipe(gulp.dest('./public/assets'));
done()
});
I have a similar task for my scss files which converts scss to CSS and then minifies it. this is working absolutely fine adding proper key-value pairs in the rev-manifest.json
gulp.task('css', function (done) {
console.log('minifying css...');
gulp.src('./assets/sass/**/*.scss')
.pipe(sass())
.pipe(cssnano())
.pipe(gulp.dest('./assets.css'));
gulp.src('./assets/**/*.css')
.pipe(rev())
.pipe(gulp.dest('./public/assets'))
.pipe(rev.manifest({
cwd: 'public',
merge: true
}))
.pipe(gulp.dest('./public/assets'));
done();
});
this is what rev-manifest.json looks like
See it's only adding the css files here but not js files.
my rev-manifest.json is present inside public/assets/
In my case, manifests were not merging, they were getting overwritten. gulp.dest() causes the file to be overwritten. We indeed have to pass the path of the manifest as parameter before the options if we want the merge to work, here is the working code :
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const cssnano = require('gulp-cssnano');
const rev = require('gulp-rev');
const uglify = require('gulp-uglify-es').default;
const imagemin = require('gulp-imagemin');
const del = require('del');
gulp.task('css', function(done){
console.log('minifying css...');
gulp.src('./assets/sass/**/*.scss')
.pipe(sass())
.pipe(cssnano())
.pipe(gulp.dest('./assets/'));
return gulp.src('./assets/**/*.css')
.pipe(rev())
.pipe(gulp.dest('./public/assets/'))
.pipe(rev.manifest('public/assets/rev-manifest.json', {
base: './public/assets',
merge: true // merge with the existing manifest (if one exists)
}))
.pipe(gulp.dest('./public/assets/'));
done();
});
gulp.task('js', function (done) {
console.log('minifying js...');
gulp.src('./assets/**/*.js')
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest('./public/assets/'))
.pipe(rev.manifest('public/assets/rev-manifest.json', {
base: './public/assets',
merge: true // merge with the existing manifest (if one exists)
}))
.pipe(gulp.dest('./public/assets/'));
done()
});
gulp.task('images', function(done){
console.log('compressing images...');
gulp.src('./assets/**/*.+(png|jpg|gif|svg|jpeg)')
.pipe(imagemin())
.pipe(rev())
.pipe(gulp.dest('./public/assets/'))
.pipe(rev.manifest('public/assets/rev-manifest.json', {
base: './public/assets',
merge: true // merge with the existing manifest (if one exists)
}))
.pipe(gulp.dest('./public/assets/'));
done();
});
// empty the public/assets directory
gulp.task('clean:assets', function(done){
del.sync('./public/assets');
done();
});
gulp.task('build', gulp.series('clean:assets', 'css', 'js', 'images'), function(done){
console.log('Building assets');
done();
});
I just deleted the rev-manifest.js file and build it again and it worked. Took me a day to do this.
Why God Why.

Gulp 4 - single task for multiple paths

I am working on a Gulp file for WordPress development. I create Themes & Plugins so require my Gulpfile to watch for sass changes for both.
I currently have 2 paths defined and a separate Gulp task for plugins & themes, however I would like to try and improve this by having a single Sass task that can use either path.
// File paths
const files = {
themeScssPath: 'htdocs/wp-content/themes/**/assets/src/scss/**/*.scss',
pluginScssPath: 'htdocs/wp-content/plugins/jhdev-*/assets/src/scss/**/*.scss',
}
// Theme Sass: compiles Theme style.scss file into style.css
function themeScssTask(){
return src(files.themeScssPath, {base: "./"})
.pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass()) // compile SCSS to CSS
.pipe(postcss([ autoprefixer(), cssnano() ])) // PostCSS plugins
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(rename(function (path) {
path.dirname += "/../../dist";
}))
.pipe(rename({ suffix: ".min" }))
.pipe(dest('./'))
}
// Plugin Sass: compiles Theme style.scss file into style.css
function pluginScssTask(){
return src(files.pluginScssPath, {base: "./"})
.pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass()) // compile SCSS to CSS
.pipe(postcss([ autoprefixer(), cssnano() ])) // PostCSS plugins
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(rename(function (path) {
path.dirname += "/../../dist";
}))
.pipe(rename({ suffix: ".min" }))
.pipe(dest('./'))
}
// Watch task: watch SCSS and JS files for changes
function watchTask(){
watch([files.themeScssPath, files.pluginScssPath],
parallel(themeScssTask, pluginScssTask));
}
exports.default = series(
parallel(themeScssTask, pluginScssTask),
watchTask
);
How about something like:
// File paths
const files = [
'htdocs/wp-content/themes/**/assets/src/scss/**/*.scss',
'htdocs/wp-content/plugins/jhdev-*/assets/src/scss/**/*.scss'
];
// Sass: compiles *.scss file into *.css
function scssTask(){
return src(files, {base: "./"})
.pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass()) // compile SCSS to CSS
.pipe(postcss([ autoprefixer(), cssnano() ])) // PostCSS plugins
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(rename(function (path) {
path.dirname += "/../../dist";
}))
.pipe(rename({ suffix: ".min" }))
.pipe(dest('./'))
}
// Watch task: watch SCSS and JS files for changes
function watchTask(){
watch(files, scssTask);
}
exports.default = series(
scssTask,
watchTask
);

Merge multiple scss files into one css file

I'd like to merge all of my scss files into one compressed css file:
Structure:
stylesheets/
test.scss
test2.scss
gulpfile.js
I tried this two tasks, which I found in several answers and tutorials, but I always got the scss file into a duplicated css file:
gulp.task('sass', function() {
return gulp.src('stylesheets/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('css/'))
});
and
gulp.task('sass', function() {
return gulp.src('stylesheets/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('css/styles.css'))
});
!!! EFFECTIV RESULT !!!
stylesheets/
test.scss
test2.scss
css/
test.css
test2.css
gulpfile.js
or with second tried task:
stylesheets/
test.scss
test2.scss
css/styles.css/
test.css
test2.css
gulpfile.js
??? EXPECTED RESULT ???
stylesheets/
test.scss
test2.scss
css/
styles.css
gulpfile.js
Any ideas?
You indicated that you want to merge your files into one file and compress that file, so you need to add two plugins to do each of those.
Gulp-concat and gulp-uglify
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
gulp.task('sass', function() {
return gulp.src('stylesheets/**/*.scss')
.pipe(sass())
.pipe(concat('styles.css'))
.pipe(uglify())
// .pipe(rename({
// basename: "styles",
// suffix: ".min",
// extname: ".css"
// }))
.pipe(gulp.dest('css'))
});
In addition, you may want to add gulp-rename to indicate that the file has been uglified/minified via a .min.css extension (and change your html link to point to that filename).
Hope this hepls.
gulp.task('styles', () => {
gulp.src([path.src.sass])
.pipe(sassGlob())
.on('error', util.log)
.pipe(sass({
includePaths: ['src/scss'],
}))
.on('error', util.log)
.pipe(gulp.dest(path.dist.css))
});

Watch for changes in subdirectories but target a single file as src in Gulp

I have the following gulp task:
gulp.task("stylePipe", function(){
var postcssPlugins = [
autoprefixer({browsers: ['last 2 version']}), // autoprefixer
cssnano() // css minifier
];
gulp.src("src/sass/*.scss")
.pipe(sourcemaps.init())
.pipe(plumber())
.pipe(sass().on("error", sass.logError)) // scan for errors
.pipe(postcss(postcssPlugins)) // runs postcss plugins listed above
.pipe(rename({ extname: '.min.css' }))
.pipe(sourcemaps.write('maps'))
.pipe(gulp.dest("dist/css")); // compile into dist
});
gulp.task("styleWatch", ["stylePipe"], browsersync.reload);
gulp.task("watch", function(){
browsersync({
server: {
baseDir: "dist/"
}
});
gulp.watch("src/sass/*.scss", ["styleWatch"]);
});
I want to watch for changes in all the subdirectories listed below
using only the main.scss as src in the gulp.src("src/sass/*.scss") (main.scss is the only file that should be preprocessed whenever there is a change in the subdirectories' files).
How can I accomplish this?
I simply modified
gulp.watch("src/sass/*.scss", ["styleWatch"]);
into
gulp.watch("src/sass/**/*.scss", ["styleWatch"]);

Can't get gulp to output to the source directory

I'm trying to get gulp to compile my SASS files and output them to the same source dir however I haven't been able to get it to work properly, I've tried using the base option:
gulp.task('sass_modules', function() {
return gulp.src('Application/modules/**/*.scss', {base: '.'})
.pipe(sassInheritance({dir: 'Application/modules/'}))
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./'));
});
With the above code, it just outputs to the app root dir, the current working directory, any ideas how I can get this to work? An example of how I want it to work is to have a Application/modules/frontend/static/css/main.scss file, and have that compiled to Application/modules/frontend/static/css/main.css and Application/modules/frontend/static/css/main.css.map.
Using this directory structure
Application
│
├─── gulpfile.js
└─┬─ modules
└─┬─ frontend
└─┬─ static
└─┬─ css
└─── main.scss
gulpfile.js
var sourcemaps = require('gulp-sourcemaps');
var sass = require('gulp-sass');
var scssFiles = 'modules/**/*.scss';
gulp.task('sass_modules', function() {
return gulp.src(scssFiles)
.pipe(sourcemaps.init())
.pipe(sass()
.on('error', sass.logError))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./'));
});