一条简单的命令就可以将 stylus 语法转换为 scss 语法

2020-02-26 20:16:49

一条简单的命令就可以将 stylus 语法转换为 scss 语法

因为早期有个项目用到了 stylus,stylus 开发起来很爽,但 stylus 基于缩进的代码在修改的时候不是很方便,加上所在团队开发使用的都是 SCSS ,为了便于维护和统一,准备将项目中的 stylus 替换成 SCSS。手动转换 stylus 浪费时间,且出错率大,当时在想也许别人也有这样的需求呢,所以就做了这样一个项目。请各位大佬动动你们发财的小手,给我点个 star,不胜感激。^_^

stylus-converter 配置

converter 配置

参数说明类型可选值默认值
quote转换中遇到字符串时,使用的引号类型string' / "'
conver转换类型,例如转换成 scss 语法stringscssscss
autoprefixer是否自动添加前缀,stylus 在转换 css 语法的时候,有些语法会自动添加前缀例如 @keyframesbooleantrue / falsetrue
indentVueStyleBlock在 .vue 文件中转换 stylus 时,可以添加一定数量的缩进,默认不添加缩进。numbernumber0

cli 配置

参数简写说明可选值默认值
--quote-q转换中遇到字符串时,使用的引号类型single / dobulesingle
--input-i输入名称,可以是文件或者是文件夹的路径--
--output-o输出名称,可以是文件或者是文件夹的路径--
--conver-c转换类型,例如转换成 scss 语法scssscss
--directory-d输入和输出路径是否是个目录yes / nono
--autoprefixer-p是否添加前缀yes / noyes
--indentVueStyleBlock-v在 .vue 文件中转换 stylus 时,可以添加一定数量的缩进,默认不添加缩进。number0

如何处理单行注释。

1. 先 fork 项目再 clone 项目到本地 git clone git@github.com:<your github>/stylus-converter.git 2. 进入项目目录 cd stylus-converter 3. 安装项目依赖 npm install 4. 进入 `node_modules/stylus/lib/lexer.js` 文件第 581 行。 5. 修改下列代码。 // 修改前 if ('/' == this.str[0] && '/' == this.str[1]) {   var end = this.str.indexOf('\n');   if (-1 == end) end = this.str.length;   this.skip(end);   return this.advance(); }  // 修改后 if ('/' == this.str[0] && '/' == this.str[1]) {   var end = this.str.indexOf('\n');   const str = this.str.substring(0, end)   if (-1 == end) end = this.str.length;   this.skip(end);   return new Token('comment', new nodes.Comment(str, suppress, true)) }

使用示例

// 下载 stylus-converter npm install -g stylus-converter // 运行 cli 转换文件 stylus-conver -i test.styl -o test.scss // 运行 cli 转换目录 // 先进入项目目录 mv src src-temp stylus-conver -d yes -i src-temp -o src

转换文件比较

转换前的 stylus 源码

handleParams(args...)   args @media screen and (max-width: 500px) and (min-width: 100px), (max-width: 500px) and (min-height: 200px)   .foo     color: #100 .foo   for i in 1..4     @media (min-width: 2 * (i + 7) px)

转换后的 SCSS 源码

@function handleParams($args...) {   @return $args; } @media screen and (max-width: 500px) and (min-width: 100px), (max-width: 500px) and (min-height: 200px) {   .foo {     color: #100;   } } .foo {   @for $i from 1 through 4 {     @media (min-width: 2 * ($i + 7) px) {       width: 100px * $i;     }   } }

如果你不想你转换的 @keyframes 添加默认前缀,请设置 options.autoprefixer = false

转换前的 .vue 文件

<template>   <div class="page-home">     <el-button>click me</el-button>   </div> </template> <style lang="stylus"> .page-home   .el-button     margin: 10px auto </style>

转换后的 .vue 文件

<template>   <div class="page-home">     <el-button>click me</el-button>   </div> </template> <style lang="scss"> .page-home {   .el-button {     margin: 10px auto;   } } </style>

搭建开发环境

1. 先 fork 项目再 clone 项目到本地 git clone git@github.com:<your github>/stylus-converter.git 2. 进入项目目录 cd stylus-converter 3. 安装项目依赖 npm install 4. 打包编译源文件 npm run build 5. 本地调试 cli npm link


  • 2019-05-21 12:44:31

    对嵌套RecyclerView的优化

    RecyclerView 是一个更高级的 ListView ,它可以重用view避免额外创建太多的view从而带来流畅的滚动性能。RecyclerView通过叫做 View pool 的东西持有不再可见的 view ,让它可被回收

  • 2019-05-25 14:54:50

    commit your changes or stash them before you can merge

    Your local changes to the following files would be overwritten by merge:         protected/config/main.php Please, commit your changes or stash them before you can merge. --------------------- 作者:陈小峰_iefreer 来源:CSDN 原文:https://blog.csdn.net/iefreer/article/details/7679631 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 2019-05-27 10:43:34

    IntelliJ IDEA中C盘文件过大怎么办

    当我在D:\ 安装完IDEA9.0之后,建立了一个工程,发现C:\Users\Administrator\.IntelliJIdea90 竟然增大到了500+M,并且随着使用在逐渐增大,这样占用系统盘资源是非常让人不爽的,那么如何将其修改到其他路径呢?

  • 2019-05-28 13:33:20

    BRVAH+MTRVA,复杂?不存在的

    言归正传,这样的一个首页,我们需要做怎么样的基础工作呢?或者说,碰到以后更复杂的页面我们应该怎么做?这里小提示下,不要再用什么类似ScrollView的这种东西了,诶,好像说的有点绝对,尽量不要用,这不是谷歌想要看到的,5.0谷歌推出了RecyclerView,从它的整个设计架构来看,简直就是为这而生的。而RecyclerView的视图是通过Adapter来渲染的。原始的Adapter,让人很蛋疼,重复工作太多,我们应该要有封装的思想,把最需要的部分提供出来,其它不用管。

  • 2019-05-29 14:19:19

    解决Git中fatal: refusing to merge unrelated histories

    Git的报错 在使用Git的过程中有时会出现一些问题,那么在解决了每个问题的时候,都需要去总结记录下来,下次不再犯。 一、fatal: refusing to merge unrelated histories 今天在使用Git创建项目的时候,在两个分支合并的时候,出现了下面的这个错误。

  • 2019-05-29 15:47:51

    撤销commit

    在git push的时候,有时候我们会想办法撤销git commit的内容