Why does sass fail to compile with grunt command error of “index is 2 but list is only 1 item long for `nth'”?

社会主义新天地 提交于 2019-11-29 17:41:11

As already pointed out, the mixin is attempting to call the 2nd element of a list that only has 1 element in it. This is because this mixin (and its related mixins) were written for an older version of Sass where mixins and functions freely had access to global variables. To remedy this, you'll need to add the !global flag to any instance where the global variable is being referenced.

This is an issue throughout the entire library, it isn't isolated to just the mixin provided by the OP. I've cleaned up the related retina-sprite-add() mixin so that you can see what needs to be changed and carry it over to the rest of the mixins.

The Original:

$retina-sprite-names     : 0;
$retina-sprite-sprites   : 0;
$retina-sprite-urls      : 0;
$retina-sprite-sprites2x : 0;
$retina-sprite-urls2x    : 0;

@mixin retina-sprite-add($name, $path, $path2x) {
  $retina-sprite-spacing: 2px !default;

  $sprite   : sprite-map($path, $spacing: $retina-sprite-spacing);
  $sprite2x : sprite-map($path2x, $spacing: $retina-sprite-spacing);

  $retina-sprite-names     : append($retina-sprite-names, $name);

  $retina-sprite-sprites   : append($retina-sprite-sprites, $sprite);
  $retina-sprite-urls      : append($retina-sprite-urls, sprite-url($sprite));

  $retina-sprite-sprites2x : append($retina-sprite-sprites2x, $sprite2x);
  $retina-sprite-urls2x    : append($retina-sprite-urls2x, sprite-url($sprite2x));
}

Updated for 3.4:

$retina-sprite-names     : 0;
$retina-sprite-sprites   : 0;
$retina-sprite-urls      : 0;
$retina-sprite-sprites2x : 0;
$retina-sprite-urls2x    : 0;

@mixin retina-sprite-add($name, $path, $path2x) {
  $retina-sprite-spacing: 2px !default;

  $sprite   : sprite-map($path, $spacing: $retina-sprite-spacing);
  $sprite2x : sprite-map($path2x, $spacing: $retina-sprite-spacing);

  $retina-sprite-names     : append($retina-sprite-names, $name) !global;

  $retina-sprite-sprites   : append($retina-sprite-sprites, $sprite) !global;
  $retina-sprite-urls      : append($retina-sprite-urls, sprite-url($sprite)) !global;

  $retina-sprite-sprites2x : append($retina-sprite-sprites2x, $sprite2x) !global;
  $retina-sprite-urls2x    : append($retina-sprite-urls2x, sprite-url($sprite2x)) !global;
}

@include retina-sprite-add('foo', 'foo.png', 'foox2.png');
@include retina-sprite-add('bar', 'bar.png', 'barx2.png');

.foo {
  debug: $retina-sprite-names;
}

Should now output:

.foo {
  debug: 0 "foo" "bar";
}

Of course, this also explains why the $index variable starts with the 2nd index.

You are trying to access 2nd element of a list that only contains one element hence the compiler throws an error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!