If I understand your qestion correctly, here is answer:
If the Sass transpiler is watching a directory (either through the command window or via an editor extension), you'll want to exclude changes to these files from transpilation, and Sass makes it easy to do so. Just prepend an underscore to their names.
Source: https://www.tutorialsteacher.com/sass/import-files-and-partials
Let's say you use:
"scss": "sass web/assets/scss/main.scss web/assets/css/main.css --style=compressed --watch"
When transpiller watching changes in main.scss where you importing files, then you get output in main.css. Then it does not matter if you import partials or not.
If you watch a directory, for example:
"scss": "sass web/assets/scss/ web/assets/css/ --style=compressed --watch"
Then, if you don't use partials, you will get every file separate in addition.
Answer from Bartlomiej Witkowski on Stack OverflowIf I understand your qestion correctly, here is answer:
If the Sass transpiler is watching a directory (either through the command window or via an editor extension), you'll want to exclude changes to these files from transpilation, and Sass makes it easy to do so. Just prepend an underscore to their names.
Source: https://www.tutorialsteacher.com/sass/import-files-and-partials
Let's say you use:
"scss": "sass web/assets/scss/main.scss web/assets/css/main.css --style=compressed --watch"
When transpiller watching changes in main.scss where you importing files, then you get output in main.css. Then it does not matter if you import partials or not.
If you watch a directory, for example:
"scss": "sass web/assets/scss/ web/assets/css/ --style=compressed --watch"
Then, if you don't use partials, you will get every file separate in addition.
You can use either @import or @use in your SASS files.
When you have a look at the current best practices in SASS, we have a folder structure like this (see: Sass Guidelines):
sass/
|
|β abstracts/
| |β _variables.scss # Sass Variables
| |β _functions.scss # Sass Functions
| |β _mixins.scss # Sass Mixins
| |β _placeholders.scss # Sass Placeholders
|
|β base/
| |β _reset.scss # Reset/normalize
| |β _typography.scss # Typography rules
| β¦ # Etc.
|
|β components/
| |β _buttons.scss # Buttons
| |β _carousel.scss # Carousel
| |β _cover.scss # Cover
| |β _dropdown.scss # Dropdown
| β¦ # Etc.
|
|β layout/
| |β _navigation.scss # Navigation
| |β _grid.scss # Grid system
| |β _header.scss # Header
| |β _footer.scss # Footer
| |β _sidebar.scss # Sidebar
| |β _forms.scss # Forms
| β¦ # Etc.
|
|β pages/
| |β _home.scss # Home specific styles
| |β _contact.scss # Contact specific styles
| β¦ # Etc.
|
|β themes/
| |β _theme.scss # Default theme
| |β _admin.scss # Admin theme
| β¦ # Etc.
|
|β vendors/
| |β _bootstrap.scss # Bootstrap
| |β _jquery-ui.scss # jQuery UI
| β¦ # Etc.
|
`β main.scss # Main Sass file
As you can see, all SASS files begin with an underscore _, except the main.scss. All partial files are imported by the main file. There are two benefits:
- You can build a modular structure
- You need to transpile only one SCSS file (
main.scss).
Example:
sass sass\main.scss css\main.css
Important:
The Sass team discourages the continued use of the @import rule. Sass will gradually phase it out over the next few years, and eventually remove it from the language entirely. Prefer the @use rule instead.
(see: SAass: @import)
When you use @import to import all partials into your main file, they share one namespace. So when you use $myvar in two partials, the last imported variable will override it. Because of this you should prefer @use now, which creates a separate namespace for each partial file, e.g. mixins.$myvar, reset.$myvar.
I know this is a silly question, but I'm having a hard time understanding this.
So, I know that the purpose of partials is that Sass will not compile them into CSS files. To me, this makes sense in the case of a _variables.scss file, wherein we put all of our variables for use in the rest of the SCSS files.
However, a lot of people organize their code by modularizing their styles into various partials. They might have files like _forms.scss and _typography.scss and then import those files into a main.scss file.
My question is, why bother making those files a partial? Why not make them normal SCSS files and import them that way, then in the console run sass --watch scss/main.scss:css/main.scss? I can understand their being, for example, a _variables.scss partial (because in normal CSS there are no variables), but I don't understand why we do that for all of our modularized styles.
There is no module with the namespace "v". - color: v.$mainHeaderColor;
Your receiving the error when compiling main.scss because the _layout.scss partial file doesn't know about the namespace "v" and wasn't able to load the members like $mainHeaderColor from the _variables.scss module. This happens because _layout.scss doesn't have a @use rule loading the members from "variables", therefore it can't access variables defined in that module.
A "quick" fix would be to introduce a @use rule in your _layout.scss file to load the variables from _variables.scss like this:
@use "./variables" as v;
/* _layout.scss */
h1 {
color: v.$mainHeaderColor;
}
Now there really isn't a need to use @forward with your current structure as its only two partial files being loaded into main.scss. If you want to use @forward I'd recommend reading further and setting up directories with index files.
To update your main file, this is how loading the files with @use at-rules into main.scss would look:
@use 'variables' as v;
@use 'layout';
/* main.scss */
/* Use the loaded members from '_variables.scss' inside the main file */
.demo {
color: v.$mainHeadColor;
}
Using @use rules along with @forward and index files
With the @import at-rule slowly being phased out of the main implementation of Sass, (dart-sass). It's time for us to adopt @use rules and learn how to utilize the cool features like index files with @forward and setting custom namespaces for loaded modules.
Since @import at-rules made everything globally accessible, this is where I've seen most of the confusion around usage of @use come into play. We have to make sure all files have appropriate knowledge of variables, mixins, functions etc if they aren't defined in the same partial or file.
If you have a main.scss Sass file that is going to be compiled with two (or more) imported partial files. The best way to utilize @use and @forward rules is by creating a directory at the same level as your main.scss file (or elsewhere just use the correct path) and create an index file, _index.scss to load the entire directory of partials with a single @use rule.
Note: All
@useat-rules must be placed at the top of the file.
This index file will be where you place your @forward rules, using @forward "<url>"; for each partial you want to load into the index file. Think of it as one big module that you can load elsewhere with a single @use rule in your main.scss file, thus giving you access to all of the partial files loaded into that index file within the /partials directory.
- main.scss
- partials/
- _index.scss
- _variables.scss
- _layout.scss
...
Going along with the example, lets say your _variables.scss partial file looked like this and wasn't relying on any other files:
$mainHeaderColor: #222;
$someOtherVar: #f06;
and if the _layout.scss partial wanted to use variables defined in _variables.scss, we would have to include a @use rule to let _layout.scss "know" about the loaded members.
@use "./variables" as v;
/* _layout.scss */
h1 {
/* now this works as expected */
color: v.$mainHeaderColor;
}
If you didn't want to define a namespace when loading with @use, simply write @use "<url>" as <namespace>; where the namespace being an asterisk * signals that a namespace won't be defined so you can use it like color: $mainHeaderColor instead of v.$mainHeaderColor. Note, this can leading to naming conflicts if your not careful.
Now to load these partials into the main.scss file which will be compiled, you could either use two separate @use rules for the _layout.scss and _variables.scss partials or create an index file (_index.scss) and load the /partials directory with a single @use at-rule giving you access to all the partials in the directory.
@forward "./variables";
@forward "./layout";
/* partials/_index.scss */
Voila! Now all that is left is to load the /partials directory into the main.scss file to be compiled and maybe used later on. The _layout.scss partial is loaded into main.scss through our single @use rule and we also have access to _variables.scss variables or any other partial file that was forwarded in the index file.
@use "./partials" as *;
/* main.scss */
h2 {
color: $mainHeaderColor;
background: $someOtherVar;
}
If you had a directory of mixins like ./mixins, then you would follow the same workflow of placing all partial files inside the /mixins directory and then create an _index.scss file to load the partials with @forward rules. To finally load the directory with a single @use rule giving you access to all of the partials loaded in a specific directory through an index file. I used the /partials directory as a "starting" point but it would make more sense to create a directory /vars, /mixins etc and place files in their respective directories for better structure/organization.
... and still no one has answered what the man asked.
I find it unbelievable that I've been trying for hours to find a solution to do this without using "@import".
The problem is so simple and yet no one simply answers with a straight solution. If we use an above example, he wants to have the variable $mainHeaderColor defined in the main.scss file, not in the _variables.scss partial. How then to @use the parent file since this would include all CSS from all child files?
I think the people who were "improving" SCSS seriously messed something up, as I am going back to @import as probably 99% of people.
Has anybody have simple solution?