Selected answer does not offer a viable solution.
OP's practice seems irregular. A shared/common file normally lives under partials
, a standard boilerplate directory. You should then add partials
directory to your config import paths in order to resolve partials anywhere in your code.
When I encountered this issue for the first time, I figured SASS probably gives you a global variable similar to Node's __dirname
, which keeps an absolute path to current working directory (cwd
). Unfortunately, it does not and the reason why is because interpolation on an @import
directive isn't possible, hence you cannot do a dynamic import path.
According to SASS docs.
You need to set :load_paths
in your Sass config. Since OP uses Compass, I'll follow that with accordance to documentation here.
You can go with the CLI solution as purposed, but why? it's much more convenient to add it to config.rb
. It'd make sense to use CLI for overriding config.rb
(E.g., different build scenarios).
So, assuming your config.rb
is under project root, simply add the following line:
add_import_path 'sub_directory_a'
And now @import 'common';
will work just fine anywhere.
While this answers OP, there's more.
Appendix
You are likely to run into cases where you want to import a CSS file in an embedded manner, that is, not via the vanilla @import
directive CSS provides out of the box, but an actual merge of a CSS file content with your SASS. There's another question, which is answered inconclusively (the solution does not work cross-environment). The solution then, is to use this SASS extension.
Once installed, add the following line to your config: require 'sass-css-importer'
and then, somewhere in your code: @import 'CSS:myCssFile';
Notice the extension must be omitted for this to work.
However, we will run into the same issue when trying to import a CSS file from a non-default path and add_import_path
does not respect CSS files. So to solve that, you need to add, yet another line in your config, which is naturally similar:
add_import_path Sass::CssImporter::Importer.new('sub_directory_a')
Now everything will work nicely.
P.S.,
I noticed sass-css-importer
documentation indicates a CSS:
prefix is required in addition to omitting the .css
extension. I found out it works regardless. Someone started an issue, which remained unanswered thus far.
@import '../../css/functions'
– Fiscal