Skip to content

Commit ff5021a

Browse files
committed
chore: replace the export of the development environment through script packaging
1 parent 57acc90 commit ff5021a

File tree

6 files changed

+93
-35
lines changed

6 files changed

+93
-35
lines changed

packages/ui/package.json

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,20 @@
55
"private": false,
66
"exports": {
77
".": {
8-
"types": "./dist/index.d.ts",
9-
"import": "./dist/index.js",
10-
"require": "./dist/index.js"
11-
},
12-
"./utils": {
13-
"types": "./dist/lib/index.d.ts",
14-
"import": "./dist/lib/index.js",
15-
"require": "./dist/lib/index.js"
16-
},
17-
"./*": {
18-
"types": "./dist/components/*/index.d.ts",
19-
"import": "./dist/components/*/index.js",
20-
"require": "./dist/components/*/index.js"
8+
"import": "./src/index.ts"
219
}
2210
},
2311
"main": "./dist/index.js",
2412
"module": "./dist/index.js",
2513
"types": "./dist/index.d.ts",
2614
"files": ["dist"],
2715
"scripts": {
28-
"build": "tsdown",
16+
"build": "set:exports:prod && pnpm run build:components && pnpm run build:registry",
17+
"build:components": "tsdown",
2918
"build:registry": "shadcn build -o ../../playground/public/r",
30-
"dev": "tsdown --watch ./src/index.ts",
31-
"register": "tsx script/registry.ts"
19+
"dev": "tsx script/fix-pkg.ts dev",
20+
"register": "tsx script/registry.ts",
21+
"set:exports:prod": "tsx script/fix-pkg.ts prod"
3222
},
3323
"dependencies": {
3424
"@radix-ui/react-accordion": "1.2.11",
@@ -42,6 +32,7 @@
4232
"@radix-ui/react-separator": "1.1.7",
4333
"@radix-ui/react-slot": "1.2.3",
4434
"@radix-ui/react-tabs": "1.1.12",
35+
"@soybean-react-ui/tailwind-plugin": "workspace:*",
4536
"clsx": "2.1.1",
4637
"embla-carousel-react": "8.6.0",
4738
"lucide-react": "0.525.0",

packages/ui/script/fix-pkg.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import fs from 'node:fs';
2+
3+
import { getPath, lintFile } from './shared';
4+
5+
// eslint-disable-next-line n/prefer-global/process
6+
const mode = process.argv[2]; // node scripts/update-exports.ts dev
7+
8+
if (mode !== 'dev' && mode !== 'prod') {
9+
console.error('❌ 请传入参数:dev 或 prod');
10+
// eslint-disable-next-line n/prefer-global/process
11+
process.exit(1);
12+
}
13+
14+
const pkgPath = getPath('package.json');
15+
16+
// 读取原始 package.json 内容
17+
const raw = fs.readFileSync(pkgPath, 'utf-8');
18+
const pkg = JSON.parse(raw);
19+
20+
// 替换 exports 字段
21+
if (mode === 'dev') {
22+
pkg.exports = {
23+
'.': {
24+
import: './src/index.ts'
25+
}
26+
};
27+
} else if (mode === 'prod') {
28+
pkg.exports = {
29+
'.': {
30+
import: './dist/index.js',
31+
require: './dist/index.js',
32+
types: './dist/index.d.ts'
33+
},
34+
'./*': {
35+
import: './dist/components/*/index.js',
36+
require: './dist/components/*/index.js',
37+
types: './dist/components/*/index.d.ts'
38+
},
39+
'./utils': {
40+
import: './dist/lib/index.js',
41+
require: './dist/lib/index.js',
42+
types: './dist/lib/index.d.ts'
43+
}
44+
};
45+
}
46+
47+
try {
48+
// 写入修改后的 package.json
49+
fs.writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
50+
console.log(`✅ "exports" field has been updated to "${mode}" mode.`);
51+
52+
lintFile(pkgPath);
53+
} catch (error) {
54+
console.error(`❌ Failed to write package.json: ${error}`);
55+
// eslint-disable-next-line n/prefer-global/process
56+
process.exit(1);
57+
}

packages/ui/script/registry.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { exec } from 'node:child_process';
22
import fs from 'node:fs/promises';
3-
import path from 'node:path';
43

54
import { rimraf } from 'rimraf';
65

76
import { registerType } from './registry-type';
87
import { getRegistryUi } from './registry-ui';
98
import { registryUtils } from './registry-utils';
9+
import { getPath, lintFile } from './shared';
10+
11+
// eslint-disable-next-line n/prefer-global/process
12+
const registryPath = getPath(`registry.json`);
13+
14+
// eslint-disable-next-line n/prefer-global/process
15+
const targetPath = getPath(`../../playground/public/r`);
1016

1117
const registry = {
1218
homepage: 'https://ui-playground.ohh-889.com/',
@@ -28,12 +34,11 @@ const registry = {
2834

2935
async function writeRegistry() {
3036
const registryJson = JSON.stringify(registry, null, 2);
31-
// eslint-disable-next-line n/prefer-global/process
32-
await fs.writeFile(path.join(process.cwd(), `registry.json`), registryJson);
3337

34-
// eslint-disable-next-line n/prefer-global/process
35-
const targetPath = path.join(process.cwd(), `../../playground/public/r`);
38+
await fs.writeFile(registryPath, registryJson);
39+
3640
console.log('🧹 Deleting:', targetPath);
41+
3742
rimraf.sync(targetPath);
3843
}
3944

@@ -56,7 +61,11 @@ async function main() {
5661
console.log('🔨 Starting registry build...');
5762
await writeRegistry();
5863

64+
lintFile(registryPath);
65+
5966
await buildRegistry();
67+
68+
lintFile(targetPath);
6069
console.log('✅ Registry build completed');
6170
} catch (error) {
6271
console.error('❌ Build failed with error:');

packages/ui/script/shared.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { execSync } from 'node:child_process';
2+
import path from 'node:path';
3+
4+
export function lintFile(filePath: string) {
5+
execSync(`eslint --fix "${filePath}"`, { stdio: 'inherit' });
6+
console.log(`✨ ESLint fix applied to ${filePath}`);
7+
}
8+
9+
export function getPath(filePath: string) {
10+
// eslint-disable-next-line n/prefer-global/process
11+
return path.join(process.cwd(), filePath);
12+
}

playground/package.json

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717
"bugs": {
1818
"url": "https://github.com/Ohh-889/soybean-react-ui/issues"
1919
},
20-
"keywords": [
21-
"UI",
22-
"tailwind-css",
23-
"shadcn-ui",
24-
"react"
25-
],
20+
"keywords": ["UI", "tailwind-css", "shadcn-ui", "react"],
2621
"scripts": {
2722
"build": "next build",
2823
"build:registry": "pnpm --filter soybean-react-ui register",
2924
"build:ui": "pnpm --filter soybean-react-ui build",
3025
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts",
3126
"deploy": "pnpm run build:registry && pnpm run build:ui && pnpm run worker:build && opennextjs-cloudflare deploy",
32-
"dev": "pnpm run dev:playground & pnpm run dev:ui",
27+
"dev": "pnpm run dev:ui && pnpm run dev:playground",
3328
"dev:playground": "next dev --turbopack",
3429
"dev:ui": "pnpm --filter soybean-react-ui dev",
3530
"lint": "next lint",
@@ -56,10 +51,5 @@
5651
"tailwindcss": "4.1.11",
5752
"tailwindcss-animate": "^1.0.7",
5853
"wrangler": "^4.24.3"
59-
},
60-
"pnpm": {
61-
"overrides": {
62-
"esbuild": "0.25.5"
63-
}
6454
}
6555
}

playground/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"module": "esnext",
88
"moduleResolution": "bundler",
99
"paths": {
10-
"@/*": ["./src/*"],
11-
"soybean-react-ui/*": ["../../packages/ui/src/*"]
10+
"@/*": ["../packages/ui/src/*"]
1211
},
1312
"resolveJsonModule": true,
1413
"allowJs": true,

0 commit comments

Comments
 (0)