修复编译

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 fs from 'iofs'
import { join, resolve, dirname, parse } from 'path' import { join } from 'path'
import Es from 'esbuild' import Es from 'esbuild'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import { red, blue } from 'kolorist' import { red, blue } from 'kolorist'
import { compileScss } from './utils.js' import { compileScss } from './utils.js'
const noc = Buffer.from('') const OPTIONS = {
target: 'esnext',
export default function compile(root = '', isProd = false, es) { minify: false,
//
const SOURCE_DIR = join(root, 'src')
const DIST_DIR = join(root, 'dist')
const OPTIONS = {
target: es || 'esnext',
minify: isProd,
format: 'esm' format: 'esm'
} }
if (isProd) { function parse(origin, target, name) {
fs.rm(DIST_DIR, true) let ext = name.slice(name.lastIndexOf('.') + 1)
}
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)
switch (ext) { switch (ext) {
case 'css': case 'css':
@ -44,35 +22,73 @@ export default function compile(root = '', isProd = false, es) {
case 'json': case 'json':
case 'gif': case 'gif':
case 'webp': case 'webp':
console.log('复制 %s ...', blue(file)) console.log('复制 %s ...', blue(name))
fs.cp(filePath, target) fs.cp(origin, target)
break break
case 'js': case 'js':
{ {
let code = fs.cat(filePath).toString() let code = fs.cat(origin).toString()
console.log('编译 %s ...', blue(file)) console.log('编译 %s ...', blue(name))
try { 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) { code = code.replace(/css`([\w\W]*?)`/g, function (m, scss) {
scss = compileScss(scss) scss = compileScss(scss)
return `css\`${scss}\`` return `css\`${scss}\``
}) })
code = Es.transformSync(code, OPTIONS).code
} catch (err) {
console.log('compile scss: ', file)
console.error(err)
}
fs.echo(code, target) fs.echo(code, target)
} }
break 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', () => { .on('ready', () => {
console.clear()
ready = true ready = true
if (isProd) {
process.exit()
}
}) })
}
} }

View File

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