Sass map.get() doesn't work. map-get() does. What gives? - function

Problem: map.get() doesn't work. map-get() does work.
I set up a map of color values and created a simple function to retrieve them.
While doing the retrieval, I followed the Sass documentation which states that you can retrieve a map value using the map.get() function. Using this or any other map.function results in an Error: There is no module with the namespace "map"..
Checking out the map module, I noticed an alternative syntax, map-get(), which does work.
What gives? Am I missing something, like importing the map module, so that I can use it in that form?
Check out my code below:
// Using npm dart `sass 1.26.11`.
$colors: ('primary': black, 'secondary': white);
// Doesn't work
#function color($color) {
#return map.get($colors, $color);
}
// Does work
#function color($color) {
#return map-get($colors, $color);
}
Question: What do I need to change to get the map.get() syntax to work?

I am having a similar issue as OP (using dart-sass v1.25.0), and only map-get works, map.get doesn't.
The documentation doesn't seem to be very clear on this, but the (Sass Module System: Draft 6) document on Github explains it better.
It seems like Sass is moving on to using #use in favour of #import for better compatibility with native CSS, and in order to get access to map.get you now must explicitly import the map module using the #use keyword.
So using OP's example, map.get should work:
#use "sass:map";
$colors: ('primary': black, 'secondary': white);
#function color($color) {
#return map.get($colors, $color);
}

Related

CSS #import with URL / Path specified as a custom property (variable)

I was thinking about a convenient way to create and use themes in HTML / CSS / JS. One possible solution I was thinking of was specifying the path to the theme file as a variable and importing the theme file in the main stylesheet via said variable so something along these lines:
:root {
--theme: url("default-theme.css");
}
#import var(--theme);
This way, if later the theme needs to be changed (for instance for Halloween or for the holiday season etc), the theme can be updated by either manually updating the value of the CSS variable or by using JS / TS to programmatically update the value.
I have tried a couple of different iterations of this including:
:root {
--theme: "default-theme.css";
}
#import var(--theme);
and
:root {
--theme: "default-theme.css";
}
#import url(var(--theme));
But sadly, nothing seems to work. I have also read through the documentation for CSS custom properties and CSS import and so far I have not really found anything.
Here is some editable sample code on StackBlitz. If anyone has any advice or suggestions or some wisdom to share, I will be extremely appreciative.

Confusion with pseudo code used in describing a function in Javascript

Only in Mozilla Dev. Network is a function declaration explained with the following pseudoCode:
function name([param,[, param,[..., param]]]) {
[statements]
}
Is there any special significance or reason why the parameter list is represented as a nested list instead of just listing out the parameters as can be seen in any other function declaration on the Web?
Why not just show the declaration simply like:
function name(param1, param2, paramN...,) {
[statements]
}
Am I looking into this too much? Or is it just the Mozilla way of explaining the declaration?
The syntax shows when a parameter is optional

SASS using a function within a variable definition [duplicate]

I'm trying Zurb Foundation 5.
So, I've created a new project and try changing settings. When I changed, for example, $row-width: rem-calc(1170); in my-project/scss/settings.scss, it compiled (in my-project/stylesheets/app.css) into:
.row {
max-width: rem-calc(1170);
}
It seems like it doesn't know about rem-calc function.
How to make it calculate rem-calc properly?
Your function doesn't exist. You must declare it (or import it from another file) before you use it. Sass does not throw errors for non-existent functions because they have a similar syntax to CSS functions. So it assumes that if it isn't a Sass function that it must be a CSS function.
Related: Test whether a Sass function is defined

Tell UglifyJS to skip a particular area of code

Is there a way to tell UglifyJS to skip a particular section of code, perhaps using comments like this:
// uglifyjs:skipStart
filter = function(item){ /* some crazy filter logic that will repeat 500,000 times */ }
// uglifyjs:skipEnd
My use case has to do with avoiding the minification of a function that will be inlined and parsed in a custom way for performance gain. Minification breaks the simplified parser.
Unfortunately, there is not a way to do this.
The Global Definitions of Uglify Compressor is a feature you can use in order to conditionally drop code.
global_defs: {
DEBUG: false
}
the compressor will assume that's a constant defintion and will discard code like this as being unreachable:
if (DEBUG) {
...
}
http://lisperator.net/uglifyjs/compress

How do I use LESS variables in CSS comments?

Is it possible to use LESS variables in CSS comments for mixins? I need that for spriting.
For example (not working / only the image path gets replaced):
.sprite (#width) {
/** sprite: sprite-#{width}; sprite-image: url('../img/#{width}/sprite-#{width}.png'); sprite-layout: vertical */
.picture {
background-image: url('../img/#{width}/picture.png'); /** sprite-ref: sprite-#{width}; */
}
}
.sprite(800);
Bonus question: Can I prevent the linebreak between background-image and the sprite-comment after compiling with lessc?
no, you can't do variables in comments.
what about adding a property 'comment' ignored by browsers.
you could try to use an escaped string e.g.
prop: ~"url('blah'); /* comment */";
but it produces 2 semicolons (valid CSS) and is pretty hacky.
I stumbled upon this question because I wanted to have automated semantic comments that are fed into Kentico (a .NET CMS). Because the accepted answer seemed a bit unsatisfying I tried a few things on my own and managed to somehow produce a better solution. Maybe this is due to changes in the less syntax since then...
// DEFINITIONS
#A: (~"A Standard");
#X-1: (~"1 General");
// BASIC DEFINITIONS
#start: (~"/*# ");
#end: (~" #*/");
#sep: (~" / ");
// FORMULA
#{start} #{A} #{sep} #{X-1} #{end}
* { text-decoration: none; }
The output is then:
/*# A Standard / 1 General #*/ * {
text-decoration: none;
}
The only thing that's bothering me is the missing new line after the comment. Unfortunately I'm using the dotless Compiler which is not able to evaluate js-functions. I added an empty comment at the end, that did the trick for me.
Should you use the browser-version, you can use the following variable to insert a new line:
#nl: (~`"\n"`)
...resulting in:
#{start} #{A} #{sep} #{X-1} #{end} #{nl}
I have a good solution:
create a file and name it start-comment.css, inside the file add exactly this content /*, then create another file end-comment.css, inside add only this */, and finally create another file e.g- description.txt in there, add all the content that you want in the comment, now in your .less file add the next code:
#import (inline) "start-comment.css";
#import (inline) "description.txt";
#import (inline) "end-comment.css";
at the end you will get something like this:
/*
(The content that i add in my text file)
*/