I'm trying to compile a set of scss files into a single css file. For example, here is my folder structure:
theme
- assets
- src
- sass
- config
- _grid-system.scss
- _variables.scss
- _client-styles.scss
- _typography.scss
- styles.scss
From the above, I'm looking to compile _client-styles.scss and _typography.scss into one css file. This new CSS file will sit in the same folder and will be called core.css (will sit under the sass folder).
I have gulpfile.js set up which compiles all .scss files into css in the same folder. But don't know how to approach this conversion as I want to ignore the styles.scss file.
Current gulpfile.js:
'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
What styles.scss looks like (for reference):
#import "config/**.scss";
#import "client-styles.scss";
#import "typography.scss";
How do I go about this? Compile the '_client-styles.scss' and '_typography.scss' file into 'core.css' that will sit under sass folder:
theme
- assets
- src
- sass
- config
- _grid-system.scss
- _variables.scss
- _client-styles.scss
- _typography.scss
- styles.scss
- core.css (new file here)
You can create a core.scss file where you will import all sass files in the subfolders like this:
// change the path accordingly
#import "./sass/**/*.scss";
Using gulp-sass-glob you can pipe it in your gulpfile.js this way:
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('autoprefixer');
var postcss = require('gulp-postcss');
var sassGlob = require("gulp-sass-glob");
var paths = {
styles: {
src: 'modules/**/*.scss',
dest: 'modules'
}
}
function scss() {
return gulp.src(paths.styles.src)
.pipe(sassGlob())
.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
Another way to compile many SCSS files would be to use the many-to-many compilation mode on the dart sass cli tool.
In my project, SASS is being managed by NPM, so to achieve this you can do:
If sass is not already installed with NPM, npm install --save-dev sass to install sass as a dev dependency for your current project or npm install -g sass to install it globally on your system
npx sass some_path/scss_dir:some_other_path/css_output_dir to compile all the *.scss files in some_path/scss_dir and output them (with source maps) to some_other_path/css_output_dir
npx sass some_path/scss_dir:some_other_path/css_output_dir --watch will re-build the compiled CSS when any of the SCSS files are changed
Related
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
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')
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.
I have following task which compiles *.scss files to scc, minifies them and concatenates to one css file.
gulp.task("scss-to-css", ["clean-css"], function () {
return gulp.src(pathToScssFiles)
.pipe(sass())
.pipe(minifyCss({}))
.pipe(concat("app.min.css"))
.pipe(gulp.dest(contentDir));
});
Is it possible to add sourcemap from app.min.css to *.scss files?
I recommend for using the gulp-ruby-sass module instead of gulp-sass.
When I tried to make source map like you, I failed to get the source map of original each scss file. So I'm looking for other way, the below gulpfile code seemed to be better.
var gulp = require('gulp');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var minifyCss = require('gulp-minify-css');
//var sass = require('gulp-sass');
var sass = require('gulp-ruby-sass');
gulp.task('scss-to-css', function() {
return sass('scss/*.scss', { sourcemap: true })
.pipe(sourcemaps.write())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(minifyCss({}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe( concat("app.min.css"))
.pipe(sourcemaps.write())
.pipe(gulp.dest( './build' ));
});
gulp.task('default', ['scss-to-css']);
Before you run gulp, gem install sass is required.
Please refer to the github example repo.
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.