Search code examples
compass-sasssass

How to import scss file in compass only if it exists?


I need to have special scss file that is different for every installation of the project, so I don't want to include it in git archive. But everything should work even if this file doesn't exist.

Is there any way to @import scss file only if it exists, ignoring file not found error?


Solution

  • You'll need to make use of the "import-path" option to do this, where the path you're importing contains the fallback files you want to import (in our case, we want an empty file).

    Option 1: Vanilla Sass

    File structure

    ├── fallback
        ├── _example.scss // <-- our blank file
    ├── _example.scss // <-- the file for the user to customize (optional)
    ├── style.scss
    

    In style.scss:

    @import "example";
    /* the rest of our styles */
    

    When you run your sass command, you would write it like this:

    sass --watch ./css:./sass --load-path ./sass/fallback
    

    Note that the fallback directory does not have to be inside your sass directory, it be anywhere on the filesystem you like.

    See also: How does SCSS locate partials?

    Option 2: Compass Extension

    You may find that creating a Compass extension is a little more convenient if you're using this technique in multiple projects. It will automatically setup the load-path for you. You can learn more about creating extensions here: http://compass-style.org/help/tutorials/extensions/