-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@import files as modules #353
Comments
We're definitely thinking about possible ways to accomplish this. We're planning on replacing |
@nex3 Do you have a list of what needs to be supported? I'll be glad to draw up a proposal. |
Nothing really written down. There are a number of issues in the tracker that are related. Off the top of my head:
@chriseppstein Can you think of any I'm missing? |
Disabling css output should be done with a flag imho. Eg @import "file.scss" !noinclude; Non-transitive imports.. Python solves this with Namespacing.. yes. I would find it weird by default. Especially since I don't believe there's any namespacing currently in scss?
I think this would be good syntax for namespacing in general. So you could do:
so to sum it up:
-> imports file.css and file2.scss, both are compiled in.
-> translates to "native css import":
-> errors because you cannot natively import file2.scss
-> imports file.scss and file2.scss and makes all their rules available. I am not sure whether !noinclude should have to be specified for every file in the rule. I think css would not like that but then again there aren't exactly many bang-rules in css.
-> imports file.css accessible under the namespace foo, and file2.scss accessible under the namespace bar. I don't currently know whether importing a file "as another" should imply !noinclude (actual keyword name to be decided, i dont really like noinclude). I would go with "least surprise", but I can't actually figure out what behaviour would be the least surprising. I might expect importing "as" to be "obviously, I want this namespaced, so don't go compiling this for me". But then again, I might expect "I have to specify !noinclude in other cases, why not now?" non-transitive imports undefined atm. I'd recommend an |
https://github.com/import This poor guy must be getting spammed. Sorry mate. |
@adys, there're some good ideas there. I'd require the namespace to be always named in order to use namespaces. So:
...is automatically translated to "native import", while:
...are "namespaced" imports. Personally I think namespaces should be the default as one of the main purposes of preprocessors is to allow modularization. As it is we need to create fake namespaces in mixin/variable names to avoid collisions. But I understand the concern about backwards compatibility and can live with it as optional. :) I think !noinclude is unnecessary. If people want to selectively include selectors, mixins or variables, they can create a new file, import a namespace and include only what is wanted. No need for new syntax and rules. |
This doesn't make sense. You say: and then you say: It's either one or the other :P I'm comparing to python/the first example in this thread here where @import "foo" would make everything available as "foo->x". It's a bad idea imho because you could be importing foo.sass, foo.scss and foo.css in the same file. |
"I'd require the namespace to be always named in order to use namespaces" ^ This is a "let's make it fit in a compatible way" proposal. "I think namespaces should be the default" ^ This is "how I really think things should be if I was implementing SASS from scratch". That said, I don't know Ruby so I can only give ideas. :) |
@nex3 Any comments on the proposal? |
Haven't had time to look through it in-depth yet. |
It's been more than year. This isn't going to happen. Would you consider pull requests to add this feature? |
@zakdances Have patience. This is the main feature we have planned for Sass 4.0. We would like to see proposals for how this would work and get feedback on such proposals (Like @noprompt's #749). Just plowing forward and writing code without an agreement for what we are building seems likely to waste a lot of time. |
@chriseppstein Understood. Should new proposals be appended here or filed as a new issue? Did you know that each time you use I'm just surprised that functionality like Maybe I'm using |
@zakdances A gist linked to this issue would be a good way to make a proposal. Yes, I'm very aware of how |
Thank you @chriseppstein for that article link. I've been really struggling with CSS organization in larger projects lately so this is honestly helps a great deal. For anyone else reading this, there's also a |
Hi. Does this topic covers namespaced variable access? Not a few modules encourage customizing their behavior by setting global variables.
Instead of above, I wish if I could do something like..
No matter how, in the future, can I expect ways to get rid of global variables? |
If the namespace feature comes out and can have own variables, I also wish multiple instance to work, like object-oriented class and instance variable.
|
Is there an update on when import v2/Sass 4.0 will be released? Can't find a timeline or recent update anywhere online... |
Why not make the semantics of this very similar to how ES2015 module imports work? @import $grid from './grid'; //Imports everything from ./grid as $grid.
@import $utils from './utils'; //If we're namespacing you can assume no output css from these modules. So default behaviour when namespacing can be @import-once?
$grid.$settings: (
columns: 12,
gutter-width: 20px
); //Alter config variable - is this global now for anywhere the module is used (like commonjs import) or specific to the current file?
.test {
@include $grid.columns(3); //Mixin
}
.item {
@extend $utils.%clearfix; //Extend placeholder
font-size: $utils.rem(18px); //Function
}
//Could also allow import of only what you need from a module
@import ( rem, %clearfix ) from './utils';
@import ( $positive, $negative, $neutral ) from './colors';
@import ( columns ) from './grid';
//Import and rename
@import ( columns as columns2 ) from './grid2';
//Perhaps as with commonjs you can even give control over what gets exported from a file to the file itself?
//Eg. in './utils' above
//Not exported
@function map-extend($map, $maps...) {
/*...*/
}
//Exported
@export @function rem($value) {
/*...*/
}
@export %clearfix {
/*...*/
}
Just a thought, seems javascript modules semantics could fit quite well for Sass too. |
Alternatively if you don't want dot notation (Sass seems to be largely avoiding it) you could even decide that the importing an entire library under a variable eg. That would eliminate the need for a new dot-notation or some other way to access items in an imported module 'collection' - the usage of all those things could stay the same as it is currently. @import ( rem, %clearfix ) from './utils';
@import ( $positive, $negative, $neutral ) from './colors';
@import ( columns ) from './grid';
//Import and rename
@import ( columns as columns2 ) from './grid2'; Exports: //Exported
@export @function rem($value) {
/*...*/
}
@export %clearfix {
/*...*/
}
//re-export (useful for libraries/index files). Can optionally use rename syntax too
@export ( columns as grid-columns ) from './grid' The no-output use-case is also covered, since you're only importing what you need in the first place. |
4 years since 2013. 4 years you've been saying that this or this feature is going to be add to Sass 4.0. When does this incredible 4.0 version is going to be released ? Has this version been abandoned ? |
@NitroBAY yeah I'm hoping something would come out of this... was thinking about using sass import-extend, but sass import can't do 'selective' import & etc :( |
The biggest advantage of that for me is it would make the sharing of variables and mixins between files so much cleaner. Today, when I want to share a variable between 2 files, I need to put the variable in a partial and then import that partial in both files. There are cases though, when the variable logically belongs to one of the modules and putting it into external partial feels like a hack and fragments the code. It feels dirty to declare some very component specific variable in a globally shared partial, just to be able to use it in another file. |
I support @simonbuerger proposal. I was thinking about ES6-way too, and even wanted to describe proposal here, but @simonbuerger catched already whole thing. ES6 imports and exports are nice, and from all I've seen so far, seems to be the most elegant and understandable. So I definitely vote for them. I also think that For instance, if module A imports some mixins, and then you import module A in your module into If we will have The only questions remained untouched (or I missed it) seems to be CSS content of imported file. I see few options:
Also it seems that named exports remained uncovered. I think this can be done by adding name to |
Also worth mention, that this whole The whole idea with In same issue also mentioned (cite of nex3):
Issue provides solution in form of // A.scss
.foo {...}
.bar {...}
// B.scss
@use "A";
.baz { @extend .foo; } I'm not sure that it should be possible at all. It sounds like a black magic, when you extend something, which actually does not exist. I think it should be made in more transparent way. Declare in your Sass file placeholder selectors, like Then import those placeholders as Most important part here is that
One more reason why idea with If you use @use "A"; and then With imports and exports of CSS you make it explicit. Import A CSS as Now we can be explicit: @include ACSS()
.baz { @extend .foo; } // will extend A .foo
@include BCSS()
.bar { @extend .foo; } // will extend A and B .foo (because they both available)
// Now there will be a bit of slightly unusual for Sass stuff, but you can catch the idea
.baz2 { @extend ACSS.foo; } // will extend only A .foo
.bar2 { @extend BCSS.foo; } // will extend only B .foo In worst case, if we don't want to limit users to placeholders-only (this can be troublesome), we can add option to imported CSS mixin. Something like: @include ACSS({ print: false })
.baz { @extend .foo; } // will extend A .foo This won't print CSS, but will make it available as context for extension. |
Lastly, what bothers me the most: will we be able to make such large amount of For instance, even right now it is possible to encapsulate every CSS in global mixin manually (see my brief example at #739 (comment)). Just for clarity, here is how it can be done now: let's say, we add our button styles to And, in fact, it covers plenty of scenarios. It already brings encapsulation we need without the need to throw everything into the global scope, except mixin itself. However, there is a trouble with it: despite you're calling So, won't we bash into same trouble here? |
The plan is to get rid of the notion of a global namespace, and have only names from files explicit referenced with |
Closing this in favor of #1094. |
I love SASS, but there's something that always bugged me about how @import works. I'd like that files were real 'packages' (or 'modules') that, when imported, don't drop all names into the global scope -- instead, they use the import name as the namespace for the names contained in the file. This would make it a lot more modular and modern.
The idea in a nutshell:
Here, foo.scss contains a mixin bar. To use it we call 'foo.bar' -- prefixing it with the 'foo' namespace. The idea requires also to support renaming the imported package on import:
I've used the keyword 'as' from Python, but it could be something else; it doesn't matter. After imported using a custom name, we use it as:
This originated here:
http://stackoverflow.com/questions/10164995/css-preprocessor-with-modules-and-sane-scope
The text was updated successfully, but these errors were encountered: