From 946a8202305c9acba99b87f5086de8623e2053eb Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 14 Mar 2023 15:39:00 +0800 Subject: [PATCH] 1.0.0 --- .gitignore | 14 ++++++++++ Readme.md | 17 ++++++++++++ index.js | 45 ++++++++++++++++++++++++++++++++ lib/main.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/utils.js | 26 ++++++++++++++++++ package.json | 22 ++++++++++------ 6 files changed, 190 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 Readme.md create mode 100755 index.js create mode 100644 lib/main.js create mode 100644 lib/utils.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8967361 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.vscode +node_modules/ + +*.sublime-project +*.sublime-workspace +package-lock.json + +._* + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..70d4747 --- /dev/null +++ b/Readme.md @@ -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`配置是最新的。 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100755 index 0000000..4deacd0 --- /dev/null +++ b/index.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node + +/** + * {} + * @author yutent + * @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 +} diff --git a/lib/main.js b/lib/main.js new file mode 100644 index 0000000..c18ff4d --- /dev/null +++ b/lib/main.js @@ -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() + } + }) +} diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..e3a9ee6 --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,26 @@ +import scss from '@bytedo/sass' +import fs from 'iofs' + +const OPTIONS = { + indentType: 'space', + indentWidth: 2 +} + +/** + * 编译scss为css + * @param file 文件路径或scss代码 + * @param mini 是否压缩 + */ +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) + } +} diff --git a/package.json b/package.json index e949d46..44b1280 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,17 @@ { - "name": "create-wcui", - "version": "0.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "name": "@bd/wcui-cli", + "type": "module", + "version": "1.0.0", + "bin": { + "wcui-cli": "index.js" }, - "author": "", - "license": "MIT" + "dependencies": { + "@bytedo/sass": "^1.58.3", + "esbuild": "^0.17.11", + "iofs": "^1.5.2", + "kolorist": "^1.6.0" + }, + "engines": { + "node": ">=16.14.0" + } }