master
yutent 2023-03-14 15:39:00 +08:00
parent f1b393737e
commit 946a820230
6 changed files with 190 additions and 8 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
.vscode
node_modules/
*.sublime-project
*.sublime-workspace
package-lock.json
._*
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride

17
Readme.md Normal file
View File

@ -0,0 +1,17 @@
# create-wcui
一个快速创建wcui项目的小工具
[![fite](https://badgen.net/npm/v/create-wcui)](https://www.npmjs.com/package/create-wcui)
### 使用方式
```bash
npm create wcui
# 或者
npm create wcui@latest
```
注意: `@latest`可以确保你初始化的`wcui`配置是最新的。

45
index.js Executable file
View File

@ -0,0 +1,45 @@
#!/usr/bin/env node
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2022/09/28 15:12:45
*/
import fs from 'iofs'
import { join, normalize } from 'path'
import { red, blue } from 'kolorist'
import compile from './lib/main.js'
const WORK_SPACE = process.cwd()
const IS_WINDOWS = process.platform === 'win32'
const NODE_VERSION = +process.versions.node.split('.').slice(0, 2).join('.')
let args = process.argv.slice(2)
if (NODE_VERSION < 16.6) {
console.log(red('Error: 你当前的环境不满足 @bd/core 构建工具的要求'))
console.log(
'@bd/core 需要Node.js版本在 %s 以上, \n你当前的Node.js版本为: %s',
blue('v16.6.0'),
red(process.version),
'\n\n'
)
process.exit()
}
switch (args[0]) {
case 'dev':
compile(WORK_SPACE)
break
case 'build':
compile(WORK_SPACE, true)
break
case 'build-es6':
compile(WORK_SPACE, true, 'es6')
break
}

74
lib/main.js Normal file
View File

@ -0,0 +1,74 @@
import fs from 'iofs'
import { join, resolve, dirname, parse } from 'path'
import Es from 'esbuild'
import chokidar from 'chokidar'
import { red, blue } from 'kolorist'
import { compileScss } from './utils.js'
const noc = Buffer.from('')
export default function compile(root = '', isProd = false, es) {
//
const SOURCE_DIR = join(root, 'src')
const DIST_DIR = join(root, 'dist')
const OPTIONS = {
target: es || 'esnext',
minify: isProd,
format: 'esm'
}
if (isProd) {
fs.rm(DIST_DIR, true)
}
let ready = false
chokidar
.watch(SOURCE_DIR)
.on('all', (act, filePath) => {
if (isProd || ready) {
let file = filePath.slice(SOURCE_DIR.length)
let target = join(DIST_DIR, file)
if (act === 'add' || act === 'change') {
let ext = file.slice(file.lastIndexOf('.') + 1)
switch (ext) {
case 'css':
case 'jpg':
case 'png':
case 'svg':
case 'json':
case 'gif':
case 'webp':
console.log('复制 %s ...', blue(file))
fs.cp(filePath, target)
break
case 'js':
{
let code = fs.cat(filePath).toString()
console.log('编译 %s ...', blue(file))
code = Es.transformSync(code, OPTIONS).code.replace(
/css`([\w\W]*?)`/g,
function (m, scss) {
scss = compileScss(scss)
return `css\`${scss}\``
}
)
fs.echo(code, target)
}
break
}
}
}
})
.on('ready', () => {
ready = true
if (isProd) {
process.exit()
}
})
}

26
lib/utils.js Normal file
View File

@ -0,0 +1,26 @@
import scss from '@bytedo/sass'
import fs from 'iofs'
const OPTIONS = {
indentType: 'space',
indentWidth: 2
}
/**
* 编译scss为css
* @param file <String> 文件路径或scss代码
* @param mini <Boolean> 是否压缩
*/
export function compileScss(file, mini = true) {
let style = mini ? 'compressed' : 'expanded'
try {
if (fs.isfile(file)) {
return scss.compile(file, { style, ...OPTIONS }).css.trim()
} else {
return scss.compileString(file, { style, ...OPTIONS }).css.trim()
}
} catch (err) {
console.log('compile scss: ', file)
console.error(err)
}
}

View File

@ -1,11 +1,17 @@
{ {
"name": "create-wcui", "name": "@bd/wcui-cli",
"version": "0.0.0", "type": "module",
"description": "", "version": "1.0.0",
"main": "index.js", "bin": {
"scripts": { "wcui-cli": "index.js"
"test": "echo \"Error: no test specified\" && exit 1"
}, },
"author": "", "dependencies": {
"license": "MIT" "@bytedo/sass": "^1.58.3",
"esbuild": "^0.17.11",
"iofs": "^1.5.2",
"kolorist": "^1.6.0"
},
"engines": {
"node": ">=16.14.0"
}
} }