Skip to content

Commit e0bf55a

Browse files
committed
feat: 优化 hbuilderx-cli 配置
1 parent b827bdc commit e0bf55a

File tree

5 files changed

+61
-26
lines changed

5 files changed

+61
-26
lines changed

packages/hbuilderx-cli/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"extension",
2020
"webview"
2121
],
22+
"exports": {
23+
"./types": {
24+
"types": "./dist/types.d.ts"
25+
}
26+
},
27+
"types": "./dist/types.d.ts",
2228
"bin": {
2329
"hx-cli": "./dist/index.js"
2430
},
@@ -30,7 +36,7 @@
3036
},
3137
"scripts": {
3238
"dev": "tsdown --watch",
33-
"build": "tsdown",
39+
"build": "rimraf ./dist && tsdown",
3440
"lint": "eslint --fix",
3541
"prepublishOnly": "pnpm build"
3642
},
@@ -40,6 +46,7 @@
4046
"archiver": "^7.0.1",
4147
"chokidar": "^5.0.0",
4248
"cosmiconfig": "^9.0.0",
49+
"glob": "^13.0.0",
4350
"meow": "^14.0.0",
4451
"picocolors": "^1.1.1"
4552
},

packages/hbuilderx-cli/src/pack.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,43 @@ import fs from 'node:fs';
33
import path from 'node:path';
44
import { readJson } from '@tomjs/node';
55
import archiver from 'archiver';
6+
import { glob } from 'glob';
67
import colors from 'picocolors';
78
import { logger } from './util';
89

910
export async function packExtension(opts: CliOptions) {
10-
const packFiles = Array.isArray(opts.packFiles) ? opts.packFiles : ['dist', 'package.json', 'license'];
11-
if (!packFiles.includes('package.json')) {
12-
packFiles.push('package.json');
13-
}
14-
1511
const cwd = opts.cwd || process.cwd();
1612
const pkgPath = path.join(cwd, 'package.json');
1713
if (!fs.existsSync(pkgPath)) {
1814
logger.error(`未找到 ${colors.green(pkgPath)}`);
1915
return;
2016
}
2117
const pkg = await readJson(pkgPath);
22-
if (!checkPackage(pkg)) {
18+
if (!checkPackageFields(pkg)) {
2319
return;
2420
}
2521
const mainFile = path.join(cwd, pkg.main);
2622
if (!fs.existsSync(mainFile)) {
2723
logger.error(`未找到 ${colors.bold('package.json')}${colors.bold('main')} 字段值路径: ${colors.green(mainFile)}`);
2824
return;
2925
}
26+
27+
const packFilters: string[] = Array.isArray(pkg.files) && pkg.files.length
28+
? pkg.files
29+
: [
30+
'dist',
31+
'resources',
32+
'package.json',
33+
'LICENSE',
34+
];
35+
36+
const pkgFiles = await glob(packFilters.filter(s => !s.startsWith('!')), {
37+
cwd,
38+
ignore: packFilters.filter(s => s.startsWith('!')).map(s => s.slice(1)).concat(['node_modules/**']),
39+
});
40+
41+
console.log(pkgFiles);
42+
3043
logger.info(`${colors.blue(pkg.name)} 插件打包开始`);
3144
const archive = archiver('zip', {
3245
zlib: { level: 9 },
@@ -51,7 +64,7 @@ export async function packExtension(opts: CliOptions) {
5164
const output = fs.createWriteStream(path.join(cwd, `${pkg.name}.zip`));
5265
archive.pipe(output);
5366

54-
packFiles.forEach((file) => {
67+
pkgFiles.forEach((file) => {
5568
const filePath = path.join(cwd, file);
5669
if (!fs.existsSync(filePath)) {
5770
return;
@@ -67,7 +80,7 @@ export async function packExtension(opts: CliOptions) {
6780
archive.finalize();
6881
}
6982

70-
function checkPackage(pkg: any) {
83+
function checkPackageFields(pkg: any) {
7184
if (!pkg) {
7285
logger.error('package.json 文件格式错误');
7386
return false;

packages/hbuilderx-cli/src/types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
/**
2+
* cli 参数
3+
*/
14
export interface CliOptions {
25
/**
36
* 监听文件变化
47
*/
58
watch?: boolean;
69
/**
7-
* 打包插件
10+
* 打包插件,根据 package.json 中的 files 字段打包,如果未设置,默认取 dist,resources,package.json,LICENSE 文件夹或文件。
811
*/
912
pack?: boolean;
10-
/**
11-
* 打包插件文件,默认 dist、package.json、license
12-
*/
13-
packFiles?: string[];
1413
/**
1514
* 当前工作目录/根目录
1615
*/

packages/hbuilderx-cli/tsdown.config.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@ import { defineConfig } from 'tsdown';
22

33
export default defineConfig((options) => {
44
const isDev = !!options.watch;
5-
return {
6-
entry: ['src/index.ts'],
7-
format: ['esm'],
8-
target: ['node20'],
9-
shims: true,
10-
clean: true,
11-
dts: false,
12-
publint: true,
13-
fixedExtension: false,
14-
env: {
15-
NODE_ENV: isDev ? 'development' : 'production',
5+
return [
6+
{
7+
entry: ['src/index.ts'],
8+
format: ['esm'],
9+
target: ['node20'],
10+
shims: true,
11+
clean: false,
12+
dts: false,
13+
publint: true,
14+
fixedExtension: false,
15+
env: {
16+
NODE_ENV: isDev ? 'development' : 'production',
17+
},
1618
},
17-
};
19+
{
20+
entry: ['src/types.ts'],
21+
format: ['esm'],
22+
clean: false,
23+
dts: true,
24+
publint: true,
25+
fixedExtension: false,
26+
},
27+
];
1828
});

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)