Gulp: Compile SASS to CSS in the same folder - css

I'm trying to set up a modular workflow where, when running gulp, it will compile SASS files in a folder to CSS. Those new CSS files will be stored in the same folder.
For example, here's my current folder structure:
theme
- modules
- hero
- hero.html
- hero.scss
Here is my 'gulpfile.js':
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task( 'sass', function() {
return gulp.src('modules/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('modules/**/*.css'))
});
However, when running gulp sass, this is what my folder structure looks like:
theme
- **
- *.css
- hero
- hero.css
- modules
- hero
- hero.html
- hero.scss
Whereas what I'm after is:
theme
- modules
- hero
- hero.html
- hero.scss
- hero.css (hero.css to appear here after running gulp)
Am I far off?

If I understand correctly you want to return .css file where .scss file exist. Here is the gulpfile.js code.
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('autoprefixer');
var postcss = require('gulp-postcss');
var paths = {
styles: {
src: 'modules/**/*.scss',
dest: 'modules'
}
}
function scss() {
return gulp.src(paths.styles.src)
.pipe(sass().on('error', sass.logError))
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(postcss([autoprefixer()]))
.pipe(gulp.dest(paths.styles.dest));
}
exports.scss = scss
function watch() {
scss()
gulp.watch(paths.styles.src, scss);
}
exports.watch = watch
package.json file need devDependencies and browserlist. it will be like this.
"devDependencies": {
"autoprefixer": "^9.7.4",
"gulp": "^4.0.2",
"gulp-postcss": "^8.0.0",
"gulp-sass": "^4.0.2"
},
"browserslist": [
"last 71 version"
],
use $ gulp watch running your task.
Your folder structure like this
Themes
modules
hero
hero.html
hero.scss
header
header.html
header.scss
footer
footer.html
footer.scss
package.json
gulpfile.js
It will return
Themes
modules
hero
hero.html
hero.css
hero.scss
header
header.html
header.css
header.scss
footer
footer.html
footer.css
footer.scss
package.json
gulpfile.js
Hope this help!

gulp.dest() only wants a path, it will then append the current file path to it. And sass() will already have changed the extension to css so that shouldn't be a problem either. So in your case it should work like this.
gulp.task('sass', function() {
return gulp.src('modules/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('modules/'));
});

gulp.src tells the Gulp task what files to use for the task,
while gulp.dest tells Gulp where to output the files once the task is completed.
So in your case it should work like this
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task( 'sass', function() {
return gulp.src('modules/**/*.scss')
.pipe(sass())
.pipe(gulp.dest('modules/**'))
});

Related

How to update css theme file in WordPress

I am confused on where to edit WordPress themes. I am new to WordPress and have a custom theme which main style.css file just imports the style for this theme like this:
#import url('assets/stylesheets/app.css');
I read that it is recommended to make a new child theme, but I don't see the need for that in my case, since I would like to almost completely change the css of the theme, so there is no need to keep the original theme files. Since, I tried to modify the file 'assets/stylesheets/app.css' I couldn't see any changes in the browser. Can I edit the styles there, or I need to do it in the WP admin dashboard somewhere?
I would like to build my scripts with gulp, which I set up like this:
var gulp = require('gulp');
var sass = require('gulp-sass');
var include = require('gulp-include');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var sourcemaps = require('gulp-sourcemaps');
var prefix = require('gulp-autoprefixer');
var connect = require('gulp-connect');
var browserify = require('gulp-browserify');
var livereload = require('gulp-livereload');
var browsersync = require('browser-sync');
var config = {
srcDir: './assets',
styles: {
src: '/scss/app.scss',
dest: '/stylesheets',
includePaths: [
'node_modules/foundation-sites/scss'
],
prefix: ["last 2 versions", "> 1%", "ie 9"]
},
scripts: {
src: '/js/app.js',
dest: '/js'
},
img: {
src: '/images/**/*',
dest: '/images'
}
};
var srcDir = './src',
destDir = './build';
gulp.task('styles', function() {
return gulp.src(config.srcDir + config.styles.src)
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: config.styles.includePaths,
sourceMap: true,
outFile: config.srcDir + config.styles.dest + '/app.css',
outputStyle: 'compressed'
}))
.pipe(prefix(config.styles.prefix))
.pipe(sourcemaps.write())
.on('error', sass.logError)
.pipe(gulp.dest(config.srcDir + config.styles.dest))
.pipe(browsersync.reload({ stream: true }));
});
gulp.task('scripts', function() {
gulp.src(config.srcDir + config.scripts.src)
.pipe(browserify({
insertGlobals : true,
debug : !gulp.env.production
}))
.pipe(gulp.dest(config.srcDir + config.scripts.dest))
});
gulp.task('include', function() {
return gulp.src(config.srcDir + config.img.src)
.pipe(gulp.dest(config.srcDir + config.img.dest));
});
gulp.task('watch', function () {
// Watch .scss files
gulp.watch(config.srcDir + config.styles.src, ['styles']);
// Watch .js files
gulp.watch(config.srcDir + config.scripts.src, ['scripts']);
});
gulp.task('default', ['styles', 'scripts', 'watch']);
So, not sure how can I do it utilizing gulp. Where can I change the theme without creating the child theme?
Where does the import of "app.css" happen - at the beginning or at the end of the "style.css" file? If it's at the beginning, the changed rules in "app.css" might be overwritten by the following "style.css" rules.

gulp-recess doesn't change properties order

I try to convert my less files into css files using gulp-less and then use gulp-recess to change properties order in css files. Task less works properly but task recess doesn't work.
This is my gulpfile.js file
'use strict';
var gulp = require('gulp'),
less = require('gulp-less'),
path = require('path'),
recess = require('gulp-recess');
// less
gulp.task('less', function () {
return gulp.src(['less/style.less', 'less/fonts.less'])
.pipe(less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.pipe(gulp.dest('css'));
});
// recess
gulp.task('recess', ['less'], function () {
return gulp.src(['css/style.css', 'css/fonts.css'])
.pipe(recess())
.pipe(recess.reporter())
.pipe(gulp.dest('css'));
});
// watch
gulp.task('watch', function () {
gulp.watch('less/*.less', ['less']),
gulp.watch('css/*.css', ['recess']);
});
// default
gulp.task('default', ['less', 'recess', 'watch']);
Errors in Node.js console
What's wrong? How to fix it?
gulp-recess won't reorder your css properties, its purpose is to 'lint' your css/less files - checks for syntax errors so you can fix there warnings/errors yourself in the source files.

Gulp-sass with different paths

I have this project in Symfony
src
ProjectName
AdminBundle
Resources
public
css
style1.css
style2.ccs
sass
style1.scss
style2.scss
AdvertiserBundler
..
CommonBundle
..
PublicBundle
..
WebmaserBundle
..
All bundles have the same structure. All *.scss files are in Resources/sass/ and compiled *.css files are in Resources/public/css/. How I can put all files from A to B with one function? I try work with base, but nothing happens.
This is gulpfile.js
var gulp = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
minifycss = require('gulp-minify-css')
;
gulp.task('styles', function() {
gulp.src('src/ProjectName/*Bundle/Resources/sass/*.scss', {
base: 'sass'
})
.pipe(sass({
outputStyle: 'compressed',
errLogToConsole: true
}))
.pipe(autoprefixer('last 5 version', 'ie 9'))
.pipe(minifycss())
.pipe(gulp.dest('public/css/'))
;
});
gulp.task('default', ['styles'], function() {
gulp.watch('src/ProjectName/*Bundle/Resources/sass/*.scss', ['styles']);
});
I don't want write like this five times:
....
gulp.src('src/ProjectName/AdminBundle/Resources/sass/*.scss')
.pipe(gulp.dest('src/ProjectName/AdminBundle/Resources/public/css'))
....
You can define multiple paths as follows: gulp.src(['path1', 'path2']).
Source: Glob stream

Using Sourcemaps with google chrome

i have tried to configure sourcemaps in less files. When I inspect an Element I see the correct less file with the file extension. So it seems to work
But when I do changes the file name for Example style.less:1120 change to style.css:129
What went wrong?
Another problem is, that chrome shows me only the style.less file. This file imports only my components. So instead of seeing component.less:10 i see style.less:221
My gulpfile:
var gulp = require('gulp');
var less = require('gulp-less');
var rename = require('gulp-rename');
var sourcemaps = require('gulp-sourcemaps');
var paths = {
scripts: ['client/js/**/*.coffee', '!client/external/**/*.coffee'],
images: 'client/img/**/*',
less: 'web/style/style.less'
};
gulp.task('less' , function() {
// Minify and copy all JavaScript (except vendor scripts)
// with sourcemaps all the way down
return gulp.src(paths.less)
.pipe(sourcemaps.init())
.pipe(less())
.pipe(rename(function(path) {
path.extname = ".css";
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('web/style/.'));
});
// Rerun the task when a file changes
gulp.task('watch', function() {
gulp.watch(paths.less, ['less']);
});
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['watch']);

BrowserSync to inject css into a live Wordpress page via proxy

I am trying to use BrowserSync & sass to edit the style.css from a Wordpress site.
I'd like to inject the changes into the page and reload it automatically.
So far I tried a lot of variants to the gulpfile.js code without success -- the scss files compile into style.css (ok), but the page doesn't reflect the changes.
Here is my code:
var gulp = require('gulp'),
sass = require('gulp-sass'),
bs = require('browser-sync');
// -- sass
//
gulp.task('sass', function () {
return gulp
.src('src/scss/*.scss')
.pipe(sass({ includePaths: [ 'src/scss/include' ] }))
.pipe(gulp.dest('wp-content/themes/my-theme'))
.pipe(bs.reload({ stream: true }));
});
// -- serve
//
gulp.task('serve', [ 'sass' ], function() {
bs({
proxy: "http://my-site.com",
files: "wp-content/themes/my-theme/style.css"
});
gulp.watch( "src/scss/**/*.scss", ['sass'] );
});
For the moment, I resorted to use the awkward Chrome workspace override, but the workflow is not as smooth as it could be.
Thanks in advance

Resources