From 946c240c18b837f5778c263d718d0d875d9fc69f Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 20 Apr 2023 15:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.js | 132 +++++++++++++++++++++++++++++---------------------- package.json | 2 +- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/lib/main.js b/lib/main.js index c959ea0..a3f2abc 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,78 +1,94 @@ import fs from 'iofs' -import { join, resolve, dirname, parse } from 'path' +import { join } from 'path' import Es from 'esbuild' import chokidar from 'chokidar' import { red, blue } from 'kolorist' import { compileScss } from './utils.js' -const noc = Buffer.from('') +const OPTIONS = { + target: 'esnext', + minify: false, + format: 'esm' +} -export default function compile(root = '', isProd = false, es) { +function parse(origin, target, name) { + let ext = name.slice(name.lastIndexOf('.') + 1) + + switch (ext) { + case 'css': + case 'jpg': + case 'png': + case 'svg': + case 'json': + case 'gif': + case 'webp': + console.log('复制 %s ...', blue(name)) + fs.cp(origin, target) + break + + case 'js': + { + let code = fs.cat(origin).toString() + console.log('编译 %s ...', blue(name)) + try { + code = Es.transformSync(code, OPTIONS).code + } catch (err) { + console.log('compile scss: ', name) + console.error(err) + } + + code = code.replace(/css`([\w\W]*?)`/g, function (m, scss) { + scss = compileScss(scss) + return `css\`${scss}\`` + }) + fs.echo(code, target) + } + break + } +} + +export default function compile(root = '', isProd = false, es = 'esnext') { // const SOURCE_DIR = join(root, 'src') const DIST_DIR = join(root, 'dist') - const OPTIONS = { - target: es || 'esnext', - minify: isProd, - format: 'esm' - } + OPTIONS.target = es + OPTIONS.minify = isProd if (isProd) { fs.rm(DIST_DIR, true) - } - let ready = false - chokidar - .watch(SOURCE_DIR) - .on('all', (act, filePath) => { - let file = filePath.slice(SOURCE_DIR.length) - let target = join(DIST_DIR, file) + let list = fs.ls(SOURCE_DIR, true) - if (ready) { - console.clear() - } - - 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)) - try { - code = code.replace(/css`([\w\W]*?)`/g, function (m, scss) { - scss = compileScss(scss) - return `css\`${scss}\`` - }) - code = Es.transformSync(code, OPTIONS).code - } catch (err) { - console.log('compile scss: ', file) - console.error(err) - } - fs.echo(code, target) - } - break + list.forEach(it => { + if (fs.isfile(it)) { + if (it.endsWith('.DS_Store') || it.includes('/.')) { + return } + let name = it.slice(SOURCE_DIR.length) + let target = join(DIST_DIR, name) + parse(it, target, name) } }) - .on('ready', () => { - console.clear() - ready = true - if (isProd) { - process.exit() - } - }) + } else { + let ready = false + + chokidar + .watch(SOURCE_DIR) + .on('all', (act, filePath) => { + let name = filePath.slice(SOURCE_DIR.length) + let target = join(DIST_DIR, name) + + if (ready) { + console.clear() + } + + if (act === 'add' || act === 'change') { + parse(filePath, target, name) + } + }) + .on('ready', () => { + ready = true + }) + } } diff --git a/package.json b/package.json index 4fce0b6..94498bb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bd/wcui-cli", "type": "module", - "version": "1.2.1", + "version": "1.2.2", "bin": { "wcui-cli": "index.js" },