当前位置:数据分析 > 使用scss开发微信小程序

使用scss开发微信小程序

  • 发布:2023-09-30 19:14

微信小程序的wxss、阿里旗下淘宝、支付宝小程序的acss等等语法很类似原生css,但是在web开发里用惯了动态css语言,再写回原生css很不习惯,尤其是父子样式的嵌套写法非常繁琐。

因此,我希望能有一个自动化构建方案,能够简单地将scss转换成小程序的样式语言。

方案1

以前写微信小程序的依赖库时用过,使用gulp编译,将源码和编译后的代码分别放到src和dist两个目录。gulp会处理src下面的所有文件,将其中的scss转换成css,并将其他所有文件原封不动挪到dist下相应位置。

这里就不详细说了,代码参考Wux。

方案2

非常简单直接,使用Webstorm/IDEA的File Watchers功能实时转换。

安装Ruby和sass

确保命令行输入sass -v能出现版本号,安装过程略。

安装File Watchers

到插件市场上搜索并安装(已安装则跳过)

var path = require("path")var fs = require("fs")const { exec } = require('child_process')const basePath = path.resolve(__dirname, '../')function mapDir(dir, callback, finish) { fs.readdir(dir, function(err, files) { if (err) { console.error(err) return } files.forEach((filename, index) => { let pathname = path.join(dir, filename) fs.stat(pathname, (err, stats) => { // 读取文件信息 if (err) { console.log('获取文件stats失败') return } if (stats.isDirectory()) { mapDir(pathname, callback, finish) } else if (stats.isFile()) { if (!['.scss'].includes(path.extname(pathname))) { return } callback(pathname) } }) if (index === files.length - 1) { finish && finish() } }) }) } mapDir( basePath, function (file) { const newFileWithoutExt = path.basename(file, '.scss') if (newFileWithoutExt.startsWith('_')) { return // 按照scss规则,下划线开头的文件不会生成css } // exec可以让nodejs执行外部命令 exec(`sass --no-cache --sourcemap=none --default-encoding utf-8 --style expanded ${file}:${newFileWithoutExt}.acss`, { cwd: path.dirname(file) // 不写这个会导致生成的文件出现在根目录 }, (err, stdout, stderr) => { if (err) { console.log(err) return } console.log(`stdout: ${stdout}`) }) }, function() { // console.log('xxx文件目录遍历完了') } )

登录后复制

在package.json里添加一条script:

"scripts": {    "scss": "node build/scss-convert",
  }
登录后复制

推荐教程:《微信小程序》

以上就是使用scss开发微信小程序的详细内容,更多请关注其它相关文章!

相关文章

热门推荐