一条简单的命令就可以将 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-12-29 15:01:37

    修改laravel分页的样式

    首先获取到数据,paginate方法 能够自动判定当前页面正确的数量限制和偏移数。默认情况下,当前页数由HTTP 请求所带的 ?page 参数来决定。当然,该值由 Laravel 自动检测,并自动插入由分页器生成的链接。

  • 2019-12-29 15:05:57

    php 数组分页 array_slice()函数用法

    今天用到一个函数,非常好用,分享给大家 array_slice() -从数组中取出一段 也就是说用这个函数可以和sql语句一样实现分页,原理是将查询出的数组,取出从指定下标开始到指定长度的数组

  • 2019-12-30 10:17:21

    router-link传递参数,query

    在vue-router中,有两大对象被挂载到了实例this; $route(只读、具备信息的对象); $router(具备功能的函数) 查询字符串: 去哪里 ? <router-link :to="{name:'detail',query:{id:1}}"> xxx </router-link>

  • 2019-12-30 16:48:41

    vue provide/inject详解和用法

    父子组件交互方式多种,props、vuex、 、 emit、localStorage还有就是这个provide/inject了。它适合层级比较深的组件,比如子,子孙,子孙后代的组件有好几个用到父组件的某个属性,就可以用到这个provide/inject,它可以避免写大量繁琐的传值代码 我这里为什么要使用它? 我一个知识库详情父组件中包含了大量的子组件,每个子组件都需要父组件的知识库ID,这时候我不想写大量props,就用到provide/inject进行传值了