-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathesbuild.config.js
127 lines (120 loc) · 2.73 KB
/
esbuild.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import path from 'node:path'
import fs, { mkdirSync } from 'node:fs'
import esbuild from 'esbuild'
import { rootPath } from '#common/utils/file/getRootPath.js'
// 读取 package.json 文件内容
const packageJson = JSON.parse(
fs.readFileSync(path.resolve(rootPath, 'package.json'), 'utf-8')
)
const esbuildConfig = {
entryPoints: ['bin/lk.js', 'bin/lf.js', 'bin/lc.js'],
outdir: 'pl-cli/bin',
platform: 'node',
target: ['node20'],
format: 'esm',
bundle: true,
minify: true,
packages: 'external',
define: {
'process.env.VERSION': JSON.stringify(packageJson.version)
}
}
const buildBinConfig = {
lk: 'bin/lk.js',
lf: 'bin/lf.js',
lc: 'bin/lc.js'
}
const publishExcludeFields = [
'scripts',
'devDependencies',
'imports',
'main',
'config',
'packageManager'
]
// 清理文件
function clean() {
return new Promise((resolve) => {
fs.rm(path.resolve(rootPath, 'pl-cli'), { recursive: true }, (err) => {
if (err) resolve()
else resolve()
})
})
}
/**
* 创建入口文件 暴露三个接口
*/
function createIndex() {
const dirs = buildBinConfig
const expTem = `export * from `
const content = Object.values(dirs).reduce((p, file) => {
return `${p + expTem}'${file}'\n`
}, '')
const filePath = 'pl-cli/index.js'
fs.writeFileSync(filePath, content)
}
/**
* 复制文档
*/
function copyDocs() {
// 复制文件
const docs = fs.readdirSync(path.resolve(rootPath, 'docs'))
// 创建docs
mkdirSync(path.resolve(rootPath, 'pl-cli/docs'), { recursive: true })
docs.forEach((doc) => {
fs.copyFileSync(
path.resolve(rootPath, 'docs', doc),
path.resolve(rootPath, 'pl-cli/docs/', doc)
)
})
fs.copyFileSync(
path.resolve(rootPath, 'README.md'),
path.resolve(rootPath, 'pl-cli/README.md')
)
fs.copyFileSync(
path.resolve(rootPath, 'LICENSE'),
path.resolve(rootPath, 'pl-cli/LICENSE')
)
}
/**
* 重写包文件
*/
function rewritePackageFile() {
const newPackageJson = Object.assign(packageJson, {
bin: buildBinConfig
})
publishExcludeFields?.forEach((key) => {
delete newPackageJson[key]
})
Object.assign(newPackageJson, { main: 'index.js' })
fs.writeFileSync(
path.resolve(rootPath, 'pl-cli/package.json'),
JSON.stringify(newPackageJson)
)
}
/**
* 构建完成之后的流程
*/
function afterBuild() {
copyDocs()
rewritePackageFile()
createIndex()
}
/**
* 主进程
*/
async function main() {
await clean()
await esbuild
.build(esbuildConfig)
.then(() => {
// 构建完成后执行的操作
afterBuild()
console.log('[LP]脚本打包完成,查看目录[pl-cli].')
})
.catch((e) => {
console.error('[LP]脚本打包失败', e)
process.exit(1)
})
}
await main()