├── .gitignore ├── LICENSE ├── README.md ├── bin └── wxss.js ├── lib └── watch.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | npm-debug.log* 4 | yarn-debug.log* 5 | yarn-error.log* 6 | 7 | # Editor directories and files 8 | .idea 9 | .vscode 10 | *.suo 11 | *.ntvs* 12 | *.njsproj 13 | *.sln 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 echo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 小程序中将less编译成wxss 2 | 3 | ## 安装 4 | 5 | ```console 6 | $ npm install -g wxss-cli 7 | ``` 8 | ## 使用 9 | 10 | > minicode 为小程序开发根目录 11 | 12 | ``` 13 | $ wxss ./minicode 14 | ``` 15 | 16 | ### 如果觉得不错,请动动您的小拇指,star一下 17 | 18 | ## License 19 | 20 | MIT 21 | -------------------------------------------------------------------------------- /bin/wxss.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fs = require('fs') 4 | const path = require('path') 5 | const program = require('commander') 6 | const chalk = require('chalk') 7 | const watch = require('../lib/watch.js') 8 | 9 | program 10 | .version(require('../package').version) 11 | .usage('') 12 | 13 | program 14 | .arguments('') 15 | .action(dir => { 16 | fs.stat(dir, (err, stats) => { 17 | if (err) console.log(chalk.yellow(err)) 18 | else { 19 | console.log(chalk.bgMagenta.white(' wxss is running... ')) 20 | watch(dir) 21 | } 22 | }) 23 | }) 24 | 25 | program 26 | .on('--help', () => { 27 | console.log() 28 | console.log(` Run ${chalk.cyan(`wxss --help`)} for detailed usage of given command.`) 29 | console.log() 30 | }) 31 | 32 | program.parse(process.argv) -------------------------------------------------------------------------------- /lib/watch.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | const chokidar = require('chokidar') 4 | const chalk = require('chalk') 5 | const less = require('less') 6 | 7 | function convertExt(fpath) { 8 | return fpath.replace(/(.css|.less)$/, '.wxss') 9 | } 10 | 11 | function writeFile(fpath) { 12 | let content = fs.readFileSync(fpath, 'utf-8') 13 | less.render(content, { 14 | filename: path.resolve(fpath) 15 | }).then(({css}) => { 16 | fs.writeFileSync(convertExt(fpath), css) 17 | }).catch(error => { 18 | console.log() 19 | console.log(chalk.bgRed.white(error)) 20 | }) 21 | } 22 | 23 | function log(msg, color) { 24 | console.log(chalk[color](msg)) 25 | } 26 | 27 | module.exports = dpath => { 28 | let len = dpath.length - 1 29 | if (dpath[len] === '\\') dpath = dpath.substring(0, len) 30 | const watch = chokidar.watch([`${dpath}/**/*.css`, `${dpath}/**/*.less`]) 31 | watch 32 | .on('add', fpath => { 33 | writeFile(fpath) 34 | log(`Add ${fpath} success!`, 'green') 35 | }) 36 | .on('change', fpath => { 37 | writeFile(fpath) 38 | log(`Change ${fpath} success!`, 'blue') 39 | }) 40 | .on('unlink', fpath => { 41 | fs.unlinkSync(convertExt(fpath)) 42 | log(`Unlink ${fpath} success!`, 'red') 43 | }) 44 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wxss-cli", 3 | "version": "1.0.4", 4 | "description": "compile less or scss into wxss", 5 | "bin": { 6 | "wxss": "bin/wxss.js" 7 | }, 8 | "main": "index.js", 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/echo008/wxss-cli.git" 15 | }, 16 | "keywords": [ 17 | "less", 18 | "scss", 19 | "wxss" 20 | ], 21 | "author": "Echo", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/echo008/wxss-cli/issues" 25 | }, 26 | "homepage": "https://github.com/echo008/wxss-cli#readme", 27 | "dependencies": { 28 | "chalk": "^2.4.1", 29 | "chokidar": "^2.0.4", 30 | "commander": "^2.16.0", 31 | "less": "^3.0.4" 32 | } 33 | } 34 | --------------------------------------------------------------------------------