Stylus 是一个CSS的预处理框架,2010年产生,来自Node.js社区,主要用来给Node项目进行CSS预处理支持,所以 Stylus 是一种新型语言,可以创建健壮的、动态的、富有表现力的CSS。比较年轻,其本质上做的事情与 SASS/LESS 等类似,应该是有很多借鉴,所以近似脚本的方式去写CSS代码。

Stylus默认使用 .styl 的作为文件扩展名,支持多样性的CSS语法。

Stylus功能上更为强壮,和js联系更加紧密。

1.styleus 全局安装

安装前你要先安装nodejs,最近nodejs比较火,可以从网上找很多资料查看怎么安装

npm install stylus -g
这里我们就安装好了stylus
Usage: stylus [options] [command] [< in [> out]]
              [file|dir ...]
Commands:
  help <prop>     Opens help info for <prop> in
                  your default browser. (OS X only)
Options:

  -u, --use <path>        Utilize the stylus plugin at <path>
  -i, --interactive       Start interactive REPL
  -w, --watch             Watch file(s) for changes and re-compile
  -o, --out <dir>         Output to <dir> when passing files
  -C, --css <src> [dest]  Convert CSS input to Stylus
  -I, --include <path>    Add <path> to lookup paths
  -c, --compress          Compress CSS output
  -d, --compare           Display input along with output
  -f, --firebug           Emits debug infos in the generated css that
                          can be used by the FireStylus Firebug plugin
  -l, --line-numbers      Emits comments in the generated CSS
                          indicating the corresponding Stylus line
  -V, --version           Display the version of Stylus
  -h, --help              Display help information  
生成css

命令行中

建立一个stylusExample/,再在里面建立 src 目录专门存放 stylus 文件,在里面建立 example.styl 文件。然后在stylusExample 目录下面执行下面命令

$ stylus --compress src/

输出compiled src/example.css ,这个时候表示你生成成功了,带上--compress参数表示你生成压缩的CSS文件。

$ stylus --css css/example.css css/out.styl CSS转换成styl
$ stylus help box-shadow CSS属性的帮助
$ stylus --css test.css 输出基本名一致的.styl文件

grunt生成

grunt生成 就比较爽歪歪了,具体grunt怎么玩儿,俺写了个教程Grunt教程-前端自动化 可以学习以下。

stylusExample 目录下创建两个文件,这两个文件是grunt必备文件。

package.json:用于保存项目元数据。
Gruntfile.js:用于配置或定义任务、加载 Grunt 插件。

package.json 内容

{
  "name": "test",
  "version": "1.0.0",
  "description": "测试的例子",
  "repository": {
    "type": "git",
    "url": ""
  }
}

然后安装必备插件,这些插件让stylus文件变更了自动生成,生成到相对应的文件目录中。如果报错你需要在命令行前面加上sudo,给它最大的执行权限。

npm install grunt --save-dev
npm install grunt-contrib-watch --save-dev :监视文件变动,做出相应动作。
npm install grunt-contrib-stylus --save-dev :stylus编写styl输出css

安装出现这样的警告 npm WARN package.json test@1.0.0 No README data 可以不理会,如果你看着不舒服,你需要在stylusExample目录下面建立一个 README.md 文件并输入内容。也可命令执行 echo "#stylus 学习" >> README.md

插件执行完毕之后 package.json 文件变成了这样样子滴。

{
  "name": "test",
  "version": "1.0.0",
  "description": "测试的例子",
  "repository": {
    "type": "git",
    "url": ""
  },
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-contrib-stylus": "^0.21.0",
    "grunt-contrib-watch": "^0.6.1"
  }
}

这个时候你需要使用这些插件儿完成你的任务需要在Gruntfile.js里面写你的执行任务。

/// 包装函数
module.exports = function(grunt) {
    // 任务配置,所有插件的配置信息
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        stylus:{
            build: {
                options: {
                    linenos: false,
                    compress: true
                },
                files: [{
                    'css/index.css': ['src/index.styl']
                }]
            }
        },
        // watch插件的配置信息
        watch: {
            another: {
                files: ['css/*','src/*.styl'],
                tasks: ['stylus'],
                options: {
                    livereload: 1337
                }
            }
        }
    });
    // 告诉grunt我们将使用插件
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-stylus');
    // 告诉grunt当我们在终端中输入grunt时需要做些什么
    grunt.registerTask('default', ['watch']);
};

注意读懂上面的哦,目录高对哦,这些没有必要提醒哦,这个时候你可以好好耍一下stylus

来源: https://segmentfault.com/a/1190000002712872