css - 如何让 grunt-autoprefixer 为未编译的 LESS 文件添加前缀?

标签 css gruntjs less gruntfile

我正在使用 LESS,我想在我的 LESS 标记上运行 grunt-autoprefixer 而无需编译 LESS,因为我的 LESS 是在我的 CMS 上自动编译的。

这可能吗?

这是我的 Gruntfile.js

module.exports = function(grunt) {

grunt.initConfig({
  concat: {
    sqs_template: {
      src: ['js/one.js', 'js/two.js'],
      dest: 'sqs_template/scripts/app.js',
    },
  }, 
  autoprefixer: {
        options: {
            remove: true,
        },
      multiple_files: {
      expand: true,
      flatten: true,
      src: 'css/*.less', 
      dest: 'sqs_template/css/',
    },
  },
    watch: {
      js: {
        files: ['js/**/*.js'],
        tasks: ['concat'],
      },
      css: {
        files: ['css/**/*.less'],
        tasks: ['autoprefixer'],
      },
    },
});

grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-watch');


};

这是我尝试添加前缀的 LESS 的示例:

//------------------------------------*\

// SOME CSS

//------------------------------------*/

// Variables
@site-width: 960px;
@color: #333333;

// Mixins
.some-font {
    font-family: sans-serif;
    font-size: 1.5rem;
    font-weight: bold;
}

.some-container {
    display: flex;
    max-width: @site-width;
    margin: 0 auto;

    .some-container-inner {
        p {
            .some-font;
            color: @color;
        }
    }
}

我希望 grunt-autoprefixer 只做我的 LESS 前缀,然后将其放入 sqs_template 文件夹。现在,grunt-autoprefixer 出错了,因为它似乎无法识别变量或混入。

这可能吗?

谢谢!

最佳答案

您可以使用 mixins 自己为所需的值添加前缀,而不是自动添加前缀。有 LESS-Prefix包含常见嫌疑人的 mixin 的库。

作为一个简单的例子,如果你正在使用 LESS-Prefix(或者你自己有一个类似的 mixin),你可以只包含 mixin 文件并键入以 为前缀的 CSS-properties .

div {
    .box-shadow(0px 0px 10px rgba(255,0,0,.5));
}

通过这个 mixin 传递值:

.box-shadow(@args) {
    -webkit-box-shadow: @args;
    box-shadow: @args;
}

你会得到这样的结果:

div {
    -webkit-box-shadow: 0px 0px 10px rgba(255,0,0,.5);
    box-shadow: 0px 0px 10px rgba(255,0,0,.5);
}

关于css - 如何让 grunt-autoprefixer 为未编译的 LESS 文件添加前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573828/

相关文章:

css - 在我的 Rails 应用程序中使用 Twitter Bootstrap,缺少标记

html - 对齐两个 Bootstrap 4 Navbars

html - 悬停时从渐变背景过渡到非渐变背景

javascript - 将中间人变量传递给 grunt 文件

javascript - Grunt、Require、Node、OMG 我从哪里开始

html - 子容器的box-shadow叠加滚动条

css - 在网页中的 div 中使用 REM 单位

javascript - 从外部目录启动 grunt

css - 将 LESS 嵌套 CSS 转换为标准 CSS

typescript - 如何在 typescript 文件中要求/导入 .less 文件