修复编译

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,40 +1,18 @@
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('')
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,
const OPTIONS = {
target: 'esnext',
minify: false,
format: 'esm'
}
}
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)
if (ready) {
console.clear()
}
if (act === 'add' || act === 'change') {
let ext = file.slice(file.lastIndexOf('.') + 1)
function parse(origin, target, name) {
let ext = name.slice(name.lastIndexOf('.') + 1)
switch (ext) {
case 'css':
@ -44,35 +22,73 @@ export default function compile(root = '', isProd = false, es) {
case 'json':
case 'gif':
case 'webp':
console.log('复制 %s ...', blue(file))
fs.cp(filePath, target)
console.log('复制 %s ...', blue(name))
fs.cp(origin, target)
break
case 'js':
{
let code = fs.cat(filePath).toString()
console.log('编译 %s ...', blue(file))
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}\``
})
code = Es.transformSync(code, OPTIONS).code
} catch (err) {
console.log('compile scss: ', file)
console.error(err)
}
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')
OPTIONS.target = es
OPTIONS.minify = isProd
if (isProd) {
fs.rm(DIST_DIR, true)
let list = fs.ls(SOURCE_DIR, true)
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)
}
})
} 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', () => {
console.clear()
ready = true
if (isProd) {
process.exit()
}
})
}
}

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"
},