Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp SASS - Use @import without compiling scss -> css

I know this is probably a weird question, but stick with me. :)

Is it possible to use gulp sass (or other gulp plugins) to read a single .scss file with multiple @import statements, and import/concat all those @import-ed files to a single .scss file that is not compiled to CSS?

Background information: I've taken the Bootstrap and FontAwesome base .scss files and combined them into a single master .scss file. I now want to get all the files in the @import statements into a single .scss file.

Another option I thought of was to just use a concat tool, but then wouldn't I have to manually specify each file to be concat-ed in the gulp file? Correct me if I'm wrong though.

Example of what I'm looking for:

//base.scss
@import foo;
@import bar;

Imports

//foo.scss
$my-font-size:20px;

And

//bar.scss
body {
  div {
    font-size:$my-font-size;
  }
}

To make

//final.scss
$my-font-size:20px;
body {
  div {
    font-size:$my-font-size;
  }
}

Notice that the @imports are included in the final.scss file, but there isn't any compilation from SCSS -> CSS.

like image 274
Anthony F. Avatar asked Oct 23 '15 16:10

Anthony F.


People also ask

Is @import deprecated in sass?

As of October 2021 @import will be deprecated, along with global built-in functions, and one year after that (no later than October 2022) @import support will be dropped. Make sure you go check out the newly refreshed Sass documentation for the latest updates.

What is the use of the @import function in sass?

The SASS @import function helps us to import multiple SASS or CSS stylesheets together such that they can be used together. Importing a SASS file using the @import rule allows access to mixins, variables, and functions to the other file in which the other file is imported.

How do you use gulp in sass?

Setting up a task Still using the command line, you can install Gulp-sass by running npm install gulp-sass --save-dev . After that, require Gulp-sass in your gulpfile. js. Put var sass = require('gulp-sass'); under the line you required gulp.

Does SCSS compile to CSS?

scss already imports all other scss files of template. So when any SCSS file in the scss folder is updated, it will be compiled to . css files. Now, you can start working with SCSS files.


1 Answers

I've stumbled upon the very same problem. This one is going to help You. Although has some flaws (forget about filenames prefixed with underscore - that's what i found till now).

the npm package that is build especially for this purpose

Disclaimer: I am not gonna explain how does the npm works, i assume author knows what is npm package if he wants to use gulp plugin. Please do not remove my answer just because i`m not unnecesarily explicitly describing what's obvious for person that aked a question. To prevent deletion of this answer as lacking context i'm quoting package description.

import resolve
resolve @import statements in stylesheets

What this does
What if you have some less or stylus files that you want to smash together into a master file, without compiling to css? Just use import-resolve and all your dreams will come true. All @import statements will be resolved and you'll be left with one file containing all your precious mixins, variables and declarations.

Using import-resolve

npm install import-resolve
// some-node-thing.js 
var importResolve = require('import-resolve');

// spits out a master dist file with all your wonderful stylesheet 
// things concatenated 
importResolve({
    "ext": "less",
    "pathToMain": "path/to/main.less",
    "output": "path/to/output.less"
});

// if you don't specify an output file, output accepts a callback parameter 
// and passes the concatenated file text 
var output = importResolve({
    "ext": "styl",
    "pathToMain": "path/to/main.styl"
}, function (output) {
    fs.writeFile('foo.styl', output, function (){
        console.log('did it myself.');
    });
});
like image 137
entio Avatar answered Oct 23 '22 13:10

entio