-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathesbuild.config.js
107 lines (102 loc) · 2.2 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
import path from 'node:path'
import fs from 'node:fs'
import esbuild from 'esbuild'
import { rootPath } from '#common/utils/file/getRootPath.js'
import { logger } from '#common/utils/logger/logger.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 copyDocs() {
// 创建docs
const docs = ['README.md', 'README_CN.md', 'README_JP.md']
docs.forEach((doc) => {
fs.copyFileSync(
path.resolve(rootPath, doc),
path.resolve(rootPath, `pl-cli/${doc}`)
)
})
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]
})
fs.writeFileSync(
path.resolve(rootPath, 'pl-cli/package.json'),
JSON.stringify(newPackageJson)
)
}
/**
* 构建完成之后的流程
*/
function afterBuild() {
copyDocs()
rewritePackageFile()
}
/**
* 主进程
*/
async function main() {
await clean()
await esbuild
.build(esbuildConfig)
.then(() => {
// 构建完成后执行的操作
afterBuild()
logger.info('[LP]脚本打包完成,请查看目录[ pl-cli ].')
process.exit(0)
})
.catch((e) => {
logger.error('[LP]脚本打包失败', e)
process.exit(1)
})
}
await main()