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
Related
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/**'))
});
I’m using a pretty simple setup for Gulp with SASS and BrowserSync.
//require plugins here
// Paths
var styleSRC = './assets/css/style.scss'; // Path to main .scss file.
var styleDestination = './ ';
var styleWatchFiles = './assets/css/**/*.scss';
// BrowserSync
gulp.task('browser-sync', function () {
browserSync.init({
proxy: projectURL,
open: false,
injectChanges: true
});
});
//Task
gulp.task('styles', function () {
gulp.src(styleSRC)
.pipe(sass({
errLogToConsole: true,
}))
.on('error', console.error.bind(console)).pipe(autoprefixer(AUTOPREFIXER_BROWSERS))
.pipe(gulp.dest(styleDestination))
.pipe(browserSync.reload({
stream: true
}));
});
// Watch
gulp.task('default', ['styles', 'browser-sync'], function () {
gulp.watch(styleWatchFiles, ['styles']);
});
The issue is though this setup doesn’t recognize when new partial file is created. So if I create a partial and then add it to style.scss nothing is happening, Sass is not compiled -> so you have manually call the task from the console.
Is it possible to automatically compile Sass when new partial is added?
if you remove './' from the source path it will work the way you would expect it
as far as i know this is a windows only problem
here is a github issue that describes the problem : https://github.com/gulpjs/gulp/issues/1422
should be fixed in gulp 4.x , but thats still in alpha
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.
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']);
I'd like to have the same gulpfile.js for my projects where I use a pre-compiler (sass) and for older/smaller projects in pure css.
Also I use browser-sync and I want to inject css into the browser (not reload the whole page).
My following configuration works, i.e. when I edit a sass file, it compiles and injects the compiled css, and when I edit a css file, it injects it correctly. But a little detail is still frustrating me : when I edit a sass file, browser-sync is notified 2 times that the css file has changed. 1 time in the sass watcher and 1 time in the css watcher. It doesn't seem to be a problem for him, since it correctly injects the css in my browser. But this is not very "clean".
I'd like to find a way to do some sort of exclusion for this. Ideally, just with a bit more logic in my coding, without the need to add some other packages like gulp-watch, gulp-if...
var gulp = require('gulp'),
rubySass = require('gulp-ruby-sass'),
sourcemaps = require('gulp-sourcemaps'),
browsersync = require('browser-sync').create();
gulp.task('rubySass', function() {
return rubySass('sources/', {
style: 'expanded',
sourcemap: true
})
.on('error', function (err) {
console.error('Error!', err.message);
})
.pipe(sourcemaps.write('maps', {
includeContent: false,
sourceRoot: '/sources'
}))
.pipe(gulp.dest('./css'))
.pipe(browsersync.stream({match: "**/*.css"}));
// If I remove this line, browser-sync is still notified 2 times
// (the console is still showing 2 times "[BS] 1 file changed (main.css)")
// but, moreover, the css is no longer injected : the whole page is reloading.
});
gulp.task('browsersync', function() {
browsersync.init({
server: './',
logConnections: true
});
});
gulp.task('default', ['rubySass', 'browsersync'], function (){
gulp.watch('**/*.html', browsersync.reload);
gulp.watch('**/*.php', browsersync.reload);
gulp.watch('js/**/*.js', browsersync.reload);
gulp.watch('**/*.css', function(file){
console.log("In gulp watch : css");
gulp.src(file.path)
.pipe(browsersync.stream());
});
gulp.watch('./sources/**/*.scss', ['rubySass']).on('change', function(evt) {
console.log("In gulp watch : sass");
});
});
Here's the console output, when saving a sass file :
As we can see, we enter successively in the 2 watchers and browser-sync is saying 2 times "[BS] 1 file changed (main.css)".
Since your SASS always compiles to .css you just need to watch for changes in your .css files. It will fire on SASS change anyway (because .css will be generated and gulp.watch triggered by this .css change). Otherwise you can use exclusions in gulp.watch() and use some kind of pattern (ex. subdirectory) to identify .css files generated from SASS. You can try this one:
var gulp = require('gulp'),
rubySass = require('gulp-ruby-sass'),
sourcemaps = require('gulp-sourcemaps'),
browsersync = require('browser-sync').create();
gulp.task('rubySass', function() {
return rubySass('sources/', {
style: 'expanded',
sourcemap: true
})
.on('error', function (err) {
console.error('Error!', err.message);
})
.pipe(sourcemaps.write('maps', {
includeContent: false,
sourceRoot: '/sources'
}))
// note the destination change here
.pipe(gulp.dest('./css/sass')) // note the destination change here
.pipe(browsersync.stream({match: "**/*.css"}));
});
gulp.task('browsersync', function() {
browsersync.init({
server: './',
logConnections: true
});
});
gulp.task('default', ['rubySass', 'browsersync'], function (){
gulp.watch('**/*.html', browsersync.reload);
gulp.watch('**/*.php', browsersync.reload);
gulp.watch('js/**/*.js', browsersync.reload);
// added exclusion for the CSS files generated from SASS
gulp.watch(['**/*.css', '!./css/sass/**/*.css'], function(file){
console.log("In gulp watch : css");
gulp.src(file.path)
.pipe(browsersync.stream());
});
gulp.watch('./sources/**/*.scss', ['rubySass']).on('change', function(evt) {
console.log("In gulp watch : sass");
});
});