88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
/*
|
|
* @Author: zhaojinfeng 121016171@qq.com
|
|
* @Date: 2023-06-19 10:38:07
|
|
* @LastEditors: zhaojinfeng 121016171@qq.com
|
|
* @LastEditTime: 2023-07-21 14:47:27
|
|
* @FilePath: \vue3\bin\build.ts
|
|
* @Description:
|
|
*
|
|
*/
|
|
/* eslint-env node */
|
|
import { resolve } from 'node:path'
|
|
import { outputFile, readdir, remove } from 'fs-extra'
|
|
import { build } from 'vite'
|
|
import { renderFile } from 'ejs'
|
|
import consola from 'consola'
|
|
import Unocss from 'unocss/vite'
|
|
|
|
async function buildAComponent(name: string) {
|
|
await build({
|
|
build: {
|
|
outDir: `es/${name}`,
|
|
lib: {
|
|
name: 'thzxcx',
|
|
entry: [resolve(process.cwd(), 'packages', name, 'index.vue'), resolve(process.cwd(), 'extra', 'unocss.ts')],
|
|
fileName: 'index',
|
|
},
|
|
rollupOptions: {
|
|
output: [
|
|
{
|
|
dir: `es/${name}`,
|
|
entryFileNames: '[name].mjs',
|
|
format: 'es',
|
|
},
|
|
{
|
|
dir: `lib/${name}`,
|
|
format: 'cjs',
|
|
// 不用打包成.mjs
|
|
entryFileNames: '[name].js',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
plugins: [Unocss()],
|
|
})
|
|
await Promise.all([remove(`es/${name}/index2.mjs`), remove(`es/${name}/unocss.mjs`), remove(`lib/${name}/index2.mjs`), remove(`lib/${name}/unocss.mjs`)])
|
|
consola.success(`${name}构建成功!`)
|
|
}
|
|
|
|
function dirPathToName(dirPath: string) {
|
|
let component = ''
|
|
|
|
dirPath.split('-').forEach((name) => {
|
|
const first = name[0]
|
|
component += first.toUpperCase() + name.slice(1)
|
|
})
|
|
return component
|
|
}
|
|
|
|
async function renderDTSwithESM(dir: string[]) {
|
|
const text = await renderFile(resolve(process.cwd(), './ejs/index.esm.ts.ejs'), { dir, dirPathToName })
|
|
return Promise.all([
|
|
outputFile(resolve(process.cwd(), './es/index.mjs'), text),
|
|
outputFile(resolve(process.cwd(), './es/index.d.ts'), text),
|
|
])
|
|
}
|
|
async function renderDTSwithUMD(dir: string[]) {
|
|
const text = await renderFile(resolve(process.cwd(), './ejs/index.umd.ts.ejs'), { dir, dirPathToName })
|
|
return Promise.all([
|
|
outputFile(resolve(process.cwd(), './lib/index.d.ts'), text),
|
|
])
|
|
}
|
|
|
|
(async function () {
|
|
const dir = await readdir(resolve(process.cwd(), './packages'))
|
|
consola.info('扫描到的目录:', dir)
|
|
|
|
await Promise.all([
|
|
remove(resolve(process.cwd(), 'es')),
|
|
remove(resolve(process.cwd(), 'lib')),
|
|
])
|
|
|
|
await Promise.all(dir.map(buildAComponent))
|
|
return Promise.all([
|
|
renderDTSwithESM(dir),
|
|
renderDTSwithUMD(dir),
|
|
])
|
|
})()
|