css - 使用 grunt-contrib-less 定义变量

标签 css gruntjs less

除非我误会了this documentation可以在 Gruntfile 中定义一个全局 less 变量,并在 .less 文件中引用它(假装有一个原因)。

less: {
  modVar: {
    options: {
      plugins: [
        new (require('less-plugin-autoprefix'))
      ],
      modifyVars: {
        root: "<%= grunt.option('distRoot') %><%= grunt.option('distTarget') %>"
      },
      root: "<%= grunt.option('distRoot') %><%= grunt.option('distTarget') %>",
    },
    files: {
      '<%= yeoman.dist %>/styles/main.less' : '<%= yeoman.app %>/styles/{,*}*.less'
    }
  },

(上面)是我的 Gruntfile 条目,我正在尝试在我的 .less 之一中引用变量 root code> 文件 (main.less),但每次在我的控制台中抛出错误时,都会在 main.less 中显示“@root is undefined”。我是否误解了 grunt-contrib-less 的可能性?或者我只是做错了什么?

最佳答案

好吧,我明白了。必须像这样调整我的 gruntfile 中的 less 任务(基于文档/搜索问题部分):

less: {
  options: {
    compress: true,
    yuicompress: true,
    optimization: 2,
    modifyVars: {
      root: '"<%= grunt.option(\'distRoot\') %><%= grunt.option(\'distTarget\') %>"'
    }
  },
  /* blah, blah, blah...other less stuff.... */
}

因此,我不需要为此变量修改创建一个完全独立的任务,而是只需在 less 任务的选项部分中抛出 modifyVars 选项。更重要的是(因为我之前尝试过),是将变量的新值用单引号括起来尽管它们已经被双引号引起来了。弄清楚这个问题真是令人头疼。

您的变量应如下所示:

 '"myWorkingValue"'

像这样:

 "myNonWorkingValue"

并且,为了完整起见,请像这样引用 .less 文件中的变量:

@{myModifiedVariable}

此时您应该已经设置好了。

关于css - 使用 grunt-contrib-less 定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35955206/

相关文章:

angularjs - compass 过滤掉 -moz 供应商前缀

css - 使用 CSS 文件而不是 LESS 文件下载 Bootstrap

javascript - 关闭按钮在 Chrome 中不起作用

javascript - 如何删除 Bootstrap 上两列之间的间隙

html - 如何将我的无序列表设置在中间而不是内嵌(见图)?

javascript - 如何定义基于其他任务的 grunt 任务?

javascript - 如何创建顶级 grunt 文件并将其导入位于子文件夹中的另一个 grunt 文件?

css - 少覆盖类和子

css - 防止编译未使用的 LESS 类

css - Divs 对齐 - Bootstrap