Skip to content

Commit 7a4513a

Browse files
committed
feat:export file in npm
1 parent faa2280 commit 7a4513a

File tree

1 file changed

+99
-51
lines changed

1 file changed

+99
-51
lines changed

esbuild.config.js

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

0 commit comments

Comments
 (0)