修复编译

master
yutent 2023-04-20 15:19:25 +08:00
parent 661542fa41
commit 946c240c18
2 changed files with 75 additions and 59 deletions

View File

@ -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
})
}
}

View File

@ -1,7 +1,7 @@
{
"name": "@bd/wcui-cli",
"type": "module",
"version": "1.2.1",
"version": "1.2.2",
"bin": {
"wcui-cli": "index.js"
},