How to watch one gulp file that imports other? - css

My sass structure look like this.
- /base
-- _fonts.scss
-- _all.scss
- /components
-- _all.scss
-- _header.scss
-- _footer.scss
- main.scss
Each _all.scss imports all files in the current folder, then the main.scss imports all _all files from each folder.
The problem I am facing is that I actually need to save main.scss in order for gulp to generate the .css file. If i watch all files it will then generate a lot of .css files which I don't need.
gulpfile.js
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function () {
return gulp.src('./src/stylesheets/main.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./public/css/'));
});
gulp.task('sass:watch', function () {
gulp.watch('./src/stylesheets/main.scss', ['sass']);
});
How do I make gulp watch all files and generate only one css file in the public folder?

Here's how I do it.
Main.scss
#import "your/relative/path/of/your/_partial-file1";
#import "your/relative/path/of/your/_partial-file2";
/*Other scss stylings if needed*/
In your gulpfile.js, add the sass task
gulp.task('css', ()=>{
 return gulp.src('src/scss/*.scss') //target all the .css files in the specified directory
   .pipe(sass()) //gulp-sass module to convert scss files into css file
   .pipe(minifyCSS()) //method to minify the final css file
   .pipe(gulp.dest('build/css')); //outputs final css file into the specified directory
   console.log('CSS build');
});
gulp.task('default', [ 'css','your', 'other','tasks' ],()=>{
console.log("All done. Watching files...");
 //watch the files in the specified directory.
 //if any changes are made to the files, the specified task will be executed in the watch method
gulp.watch('src/**/*.scss',['css']);
});
Find the full article here: http://todarman.com/gulp-configuration-build-html-css-js/
Hope this helps.

Related

Import multiple SCSS files into one SCSS file and compile to one CSS file

So I have the following main scss directory: /assets/scss/main.scss.
In this file, I have the following items:
#charset "utf-8";
// Initial variables must be imported first
#import "../../node_modules/bulma/sass/utilities/initial-variables.sass";
#import "../../node_modules/bulma/sass/utilities/functions.sass";
When I jump into the functions file, I'm able to view Bulmas functions code, so I want that imported.
Now I have a single css file inside the following directory: /assets/dist/main.css.
I would like to import all the .scss files under scss/ into main.scss and then compile main.scss into main.css.
I tried utilizing the following script, but nothing happens when it compiles:
"scripts": {
"css-build": "sass assets/scss/main.scss:assets/dist/main.css --style compressed"
},
The only thing that gets compiled inside the main.css file is the following:
/*# sourceMappingURL=main.css.map */
Does anyone have a good NPM/Gulp/etc.. solution on how I can take a directory of .scss files and import them into one .scss file and then compile to main.css?
This is the config I use :
Installs
npm i node-sass-glob-importer
npm i --global gulp-cli
npm i sass gulp-sass --save-dev
npm i gulp-sass-glob --save-dev
npm i gulp-sourcemaps
npm i gulp-concat
npm i gulp-uglify
gulpfile.js :
const { src, dest } = require('gulp');
const
sourcemaps = require('gulp-sourcemaps'),
sassGlob = require('gulp-sass-glob'),
sass = require('gulp-sass')(require('sass')),
concat = require('gulp-concat'),
uglify = require('gulp-uglify');
function scss() {
return src(['./dev/assets/_scss/main.scss'])
.pipe(sourcemaps.init({largeFile: true}))
.pipe(sassGlob())
.pipe(sass())
.pipe(concat('theme.min.css'))
.pipe(sourcemaps.write('.', {debug: true}))
.pipe(dest('dev/assets/css'));
};
exports.scss = scss;
function js() {
return src('./dev/assets/_scripts/**/*.js')
.pipe(uglify())
.pipe(sourcemaps.init())
.pipe(concat('theme.min.js'))
.pipe(sourcemaps.write('.'))
.pipe(dest('dev/assets/js'));
};
exports.js = js;
main.scss contains imports like #import 'folder/*'; or #import 'file.scss';
Then you can call gulp scss and gulp js

Gulpfile: How to compile each SCSS into separate CSS with same name but with additional common styles

First of all I'd like you guys to be gentle. I haven't been coding much in recent year and since gulp update when then changed syntax to writing functions and exporting I somehow made it work then and left with no changes up to this point, no clue if they changed something else. I've been happy with what it is right now, but I have no idea how to make it work the other way.
So anyway I'm working on a project right now, where there will be many htmls, and each one will have quite different styles, but some will be common. I want to make a main.scss file with common styles for each html, but I want to make a separate scss with styles specific to each html. This way in the end I want to have a separate css file made from a specific scss with same name combined with main.scss, so that it won't have to download a single large file, but only styles I need.
Example:
main.scss
01.scss
02.scss
03.scss
will compile to:
01.css ( main.scss + 01.scss )
02.css ( main.scss + 02.scss )
03.css ( main.scss + 03.scss )
This is my gulpfile right now:
const gulp = require('gulp');
const sass = require('gulp-sass');
const browserSync = require('browser-sync').create();
function style() {
return gulp.src('./scss/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream());
}
function watch() {
browserSync.init({
server: {
baseDir: './'
}
});
gulp.watch('./scss/**/*.scss', style);
gulp.watch('./*.html').on('change', browserSync.reload);
gulp.watch('./js/**/*.js').on('change', browserSync.reload);
}
exports.style = style;
exports.watch = watch;
If you have an idea how to do it in a better way I would really appreciate it.
I think you will have to import your main.scss into each of your other files and exclude main.scss from your gulp.src.
function style() {
return gulp.src(['./scss/**/*.scss', '!./scss/**/main.scss'])
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream());
}
'!./scss/**/main.scss' this negates or excludes that file from being passes into this task - I assumed main.scss was in the same folder as your other scss files, if that is not the case you will have to modify the path.
Then #import main.scss into each of your 01.scss, 02.scss, etc. files:
#import "main.scss"; // also assumes in same folder
You can put this import statement anywhere in the file, if it is first any of main.scss styles will be overridden by conflicting styles in the rest of the 0x.scss file. If you put the import statement at the end, then main.scss styles will override any previous conflicting styles.
Note: you should really be using #use instead of #import and gulp-dart-sass instead of gulp-sass at this point. See sass #use rule.
// in your gulpfile.js
const sass = require('gulp-dart-sass'); // once installed
#use "main.scss"; // must be at top of each scss file, such as 01.scss

Gulp sass compile replicating scss files

I am using GULP to compile my scss files and trying trying to compile it down into a single styles.css file. However when I run gulp sass it converts the scss files down to css but also replicates them instead of compiling them all down into one single file.
gulpfile.js
var gulp = require('gulp');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('sass', function () {
return gulp.src('./src/sass/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest('./src/css'));
});
gulp.task('sass:watch', function () {
gulp.watch('./src/sass/**/*.scss', ['sass']);
});
file structure
--gulpfile.js
--src
--css
--sass
--themes
--module.scss
--tm-1.scss
--styles.scss
when i run gulp sass the css output ends up being:
--css
--styles.css
--themes
--module.css
--tm-1.css
which all I want here is one single styles.css file and cant seem to understand what I'm missing here.
I have fixed the issue by changing the line
return gulp.src('./src/sass/**/*.scss')
to:
return gulp.src('./src/sass/styles.scss')

Compile each SASS file with Gulp, creating multiple CSS files

I have the need to compile a SASS file to a CSS file when saved, without having to compile every SASS file to a single CSS file.
I need the ability to:
- Run a 'watch' on a directory
- If a file is saved, a CSS of it's name is created. Example: 'main.scss' compiles to 'main.css'.
- It should not compile every single SASS if it doesn't need to.
The goal is to optimize the development process to avoid compiling every single SASS file in a directory when 'watching'.
My current SASS task looks a bit like this and results in a single CSS file:
//Compile Sass
gulp.task('styles', function() {
return gulp.src('app/scss/styles.scss')
.pipe(plugins.sass({ includePaths : [paths.sass], style: 'compressed'})
.pipe(plugins.autoprefixer('last 2 version'))
.pipe(plugins.rename({suffix: '.min'}))
.pipe(plugins.minifyCss())
.pipe(gulp.dest('build/css'));
});
Looks like gulp-changed is what you're looking for:
https://github.com/sindresorhus/gulp-changed
You add it as a dependency with npm install --save-dev gulp-changed and plug it into your gulpfile. From the gulp-changed ReadMe:
var gulp = require('gulp');
var changed = require('gulp-changed');
var ngAnnotate = require('gulp-ng-annotate'); // just as an example
var SRC = 'src/*.js';
var DEST = 'dist';
gulp.task('default', function () {
return gulp.src(SRC)
.pipe(changed(DEST))
// ngAnnotate will only get the files that
// changed since the last time it was run
.pipe(ngAnnotate())
.pipe(gulp.dest(DEST));
});

sass file not compiled by gulp

I want to switch from less to sass so I installed gulp-sass with npm and modified my gulpfile to compile sass instead of less (nothing else changed). Sadly gulp doesn't compile my .scss file to css and after googling and trying all i could think of it still doesn't compile. Here are all the informations I can show you:
gulpfile.js
// GULP CONFIG
// REQUIRES
// general
var gulp = require('gulp');
// css
var sass = require('gulp-sass');
var minifycss = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
var rename = require("gulp-rename");
// watch
var watch = require('gulp-watch');
// TASKS
// css
gulp.task('css', function() {
gulp.src('style.css') // get the 'all-import' css-file
// .pipe(sass({includePaths: ['./styles']}))
// .pipe(sass().on('error', sass.logError))
.pipe(sass()) // sass to css
.pipe(autoprefixer('last 2 version', { cascade: false })) // autoprefix
.pipe(minifycss()) // minify
.pipe(rename('style.min.css')) // rename to style.min.css
.pipe(gulp.dest('')); // output to root
});
// watch
gulp.task('watch', function() {
gulp.watch('styles/*', ['css']);
});
// RUN DEFAULT
gulp.task('default', ['watch']);
related Folders & Files
├── style.min.css
├── style.css (with #import styles/style.scss)
│ └── styles
│ ├── style.scss
terminal response:
starting gulp:
[10:19:14] Starting 'watch'...
[10:19:14] Finished 'watch' after 11 ms
[10:19:14] Starting 'default'...
[10:19:14] Finished 'default' after 20 μs
after saving style.scss
[10:19:20] Starting 'css'...
[10:19:20] Finished 'css' after 15 ms
style.scss (content on purpose of testing obviously)
$color: #99cc00;
body {
background-color: $color;
.sub {
color: $color;
}
}
style.min.css after running through gulp
$color:#9c0;body{background-color:$color;.sub{color:$color}
You are not telling gulp to watch for sass file. On this line:
gulp.src('style.css')
You are specifying a css file, not a scss file. Change it to :
gulp.src('style.scss') // update, s missing
Also, there is no output route specified. This line:
.pipe(gulp.dest(''));
Should contain your destiny route, and its currently empty.
So, for the root route, something like this should work:
.pipe(gulp.dest('./')); // or whatever route you want
Anyway, your file structure is a bit weird.
In my opinion, you should create different folders for sass files and compiled ones.
Hope this puts you on the right track.

Resources