/* * @Author: zhaojinfeng 121016171@qq.com * @Date: 2023-06-19 10:38:07 * @LastEditors: zhaojinfeng 121016171@qq.com * @LastEditTime: 2023-07-21 04:26:58 * @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' function buildAComponent(name: string) { build({ build: { outDir: `es/${name}`, lib: { name: 'tianheng-design', entry: resolve(process.cwd(), 'packages', name, 'index.vue'), fileName: 'index', }, rollupOptions: { output: [ { dir: `es/${name}`, entryFileNames: '[name].mjs', format: 'es', }, { dir: `lib/${name}`, format: 'cjs', // 不用打包成.mjs entryFileNames: '[name].js', }, ], }, }, }) } 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), ]) })()