Skip to content

Commit 636926a

Browse files
fix: fix build vue-next types (Tencent#5174)
* feat: builds tdesign-vue-next types * feat: build:vue-next-tsc script * chore: remove * refactor: remove tsconfig.components.json * fix: internal/builds prebuild * refactor: typesRoot * refactor: tsconfig.json include common * refactor: glob
1 parent e37511d commit 636926a

File tree

15 files changed

+187
-30
lines changed

15 files changed

+187
-30
lines changed

internal/builds/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@tdesign/internal-builds",
3+
"private": true,
4+
"author": "tdesign",
5+
"license": "MIT",
6+
"scripts": {
7+
"prebuild": "rollup -c rollup.config.ts",
8+
"build:vue-next-tsc": "node dist/tdesign-vue-next/index.js"
9+
},
10+
"devDependencies": {
11+
"@rollup/plugin-node-resolve": "^16.0.0",
12+
"@rollup/plugin-typescript": "^12.1.2",
13+
"fs-extra": "^11.3.0",
14+
"glob": "^11.0.1",
15+
"rollup": "^2.79.1",
16+
"rollup-plugin-delete": "^2.1.0",
17+
"rollup-plugin-multi-input": "^1.4.1"
18+
}
19+
}

internal/builds/rollup.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineConfig } from 'rollup';
2+
import json from '@rollup/plugin-json';
3+
import del from 'rollup-plugin-delete';
4+
import commonjs from '@rollup/plugin-commonjs';
5+
import typescript from '@rollup/plugin-typescript';
6+
import nodeResolve from '@rollup/plugin-node-resolve';
7+
// @ts-ignore
8+
import multiInput from 'rollup-plugin-multi-input';
9+
10+
export default defineConfig({
11+
input: ['src/**/*.ts'],
12+
output: [
13+
{
14+
dir: 'dist',
15+
format: 'cjs',
16+
},
17+
],
18+
plugins: [
19+
multiInput(),
20+
typescript({
21+
resolveJsonModule: true,
22+
allowSyntheticDefaultImports: true,
23+
}),
24+
nodeResolve({ preferBuiltins: true }),
25+
commonjs(),
26+
json(),
27+
del({ targets: 'dist' }),
28+
],
29+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { glob } from 'glob';
2+
import { run } from '@tdesign/internal-utils';
3+
import { readFile, copy, writeFile, remove } from 'fs-extra';
4+
import { resolve, getWorkSpaceRoot } from '@tdesign/internal-utils';
5+
6+
const generateSourceTypes = async () => {
7+
// 1. 编译 tsc
8+
await run('tsc --outDir dist/types -p tsconfig.json --emitDeclarationOnly');
9+
10+
const workSpaceRoot = await getWorkSpaceRoot();
11+
const typesRoot = resolve(workSpaceRoot, 'dist/types');
12+
13+
// 2. 删除 style 目录
14+
const styleDirPaths = await glob(`${resolve(typesRoot, 'packages/**/style')}`);
15+
await Promise.all(
16+
styleDirPaths.map(async (styleDirPath) => {
17+
await remove(styleDirPath);
18+
}),
19+
);
20+
21+
// 3. 复制 common 到 packages 下
22+
await copy(resolve(typesRoot, 'packages/common'), resolve(typesRoot, 'packages/components/common'));
23+
};
24+
25+
const generateTargetTypes = async (target: 'es' | 'esm' | 'lib' | 'cjs') => {
26+
const workSpaceRoot = await getWorkSpaceRoot();
27+
const typesRoot = resolve(workSpaceRoot, 'dist/types');
28+
29+
// 1. 复制 packages/components 到 packages/tdesign-vue-next/target 下
30+
const targetDir = resolve(workSpaceRoot, `packages/tdesign-vue-next/${target}`);
31+
await copy(resolve(typesRoot, `packages/components`), targetDir);
32+
33+
// 2. 替换 @tdesign/common-js 为 tdesign-vue-next/common/js
34+
const dtsPaths = await glob(`${resolve(targetDir, '**/*.d.ts')}`);
35+
const rewrite = dtsPaths.map(async (filePath) => {
36+
const content = await readFile(filePath, 'utf8');
37+
await writeFile(filePath, content.replace(/@tdesign\/common-js/g, `tdesign-vue-next/${target}/common/js`), 'utf8');
38+
});
39+
await Promise.all(rewrite);
40+
};
41+
42+
const removeSourceTypes = async () => {
43+
const workSpaceRoot = await getWorkSpaceRoot();
44+
const distTypesRoot = resolve(workSpaceRoot, 'dist');
45+
await remove(distTypesRoot);
46+
};
47+
48+
export const buildTypes = async () => {
49+
await removeSourceTypes();
50+
await generateSourceTypes();
51+
const targets = ['es', 'esm', 'lib', 'cjs'] as const;
52+
await Promise.all(
53+
targets.map(async (target) => {
54+
await generateTargetTypes(target);
55+
}),
56+
);
57+
await removeSourceTypes();
58+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { buildTypes } from './build-types';
2+
3+
buildTypes();

internal/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './src/paths';
2+
export * from './src/paths-v2';
23
export * from './src/package-json';
4+
export * from './src/exec';

internal/utils/package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,33 @@
1616
"import": "./dist/es/paths.js",
1717
"default": "./dist/cjs/paths.js"
1818
},
19+
"./paths-v2": {
20+
"types": "./types/paths-v2.d.ts",
21+
"import": "./dist/es/paths-v2.js",
22+
"default": "./dist/cjs/paths-v2.js"
23+
},
1924
"./package-json": {
2025
"types": "./types/package-json.d.ts",
2126
"import": "./dist/es/package-json.js",
2227
"default": "./dist/cjs/package-json.js"
28+
},
29+
"./exec": {
30+
"types": "./types/exec.d.ts",
31+
"import": "./dist/es/exec.js",
32+
"default": "./dist/cjs/exec.js"
2333
}
2434
},
2535
"scripts": {
26-
"build": "rollup -c rollup.config.ts && pnpm build:tsc",
36+
"prebuild": "rollup -c rollup.config.ts && pnpm build:tsc",
2737
"build:tsc": "tsc --declaration --emitDeclarationOnly --rootDir . --outDir types --resolveJsonModule --esModuleInterop ./src/*.ts index.ts"
2838
},
2939
"devDependencies": {
40+
"@pnpm/find-workspace-dir": "^1000.1.0",
3041
"@rollup/plugin-node-resolve": "^16.0.0",
3142
"@rollup/plugin-typescript": "^12.1.2",
43+
"execa": "^8.0.1",
3244
"rollup": "^2.79.1",
3345
"rollup-plugin-delete": "^2.1.0",
3446
"rollup-plugin-multi-input": "^1.4.1"
3547
}
36-
}
48+
}

internal/utils/rollup.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { defineConfig } from 'rollup';
2-
import typescript from '@rollup/plugin-typescript';
3-
import nodeResolve from '@rollup/plugin-node-resolve';
42
import json from '@rollup/plugin-json';
53
import del from 'rollup-plugin-delete';
4+
import commonjs from '@rollup/plugin-commonjs';
5+
import typescript from '@rollup/plugin-typescript';
6+
import nodeResolve from '@rollup/plugin-node-resolve';
67
// @ts-ignore
78
import multiInput from 'rollup-plugin-multi-input';
89

@@ -24,7 +25,8 @@ export default defineConfig({
2425
resolveJsonModule: true,
2526
allowSyntheticDefaultImports: true,
2627
}),
27-
nodeResolve(),
28+
nodeResolve({ preferBuiltins: true }),
29+
commonjs(),
2830
json(),
2931
del({ targets: 'dist' }),
3032
],

internal/utils/src/exec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { execaCommand, SyncOptions } from 'execa';
2+
import { getWorkSpaceRoot } from './paths-v2';
3+
4+
const run = async (command: string, options: SyncOptions = {}) => {
5+
const workspaceDir = await getWorkSpaceRoot();
6+
const { stdout, stderr } = await execaCommand(command, {
7+
cwd: workspaceDir,
8+
stdio: 'inherit',
9+
...options,
10+
});
11+
12+
return { stdout, stderr };
13+
};
14+
15+
export { run };

internal/utils/src/paths-v2.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// paths.ts 是用于之前处理路径的,但因为是直接使用的相对路径,在根目录下使用(比如根目录下执行 rollup 打包)路径是没问题的
2+
// 但如果在非根目录下使用,就会有问题,但又不能直接废弃,因此这里重新写一份
3+
import { findWorkspaceDir } from '@pnpm/find-workspace-dir';
4+
import { posix } from 'path';
5+
6+
const resolve = posix.resolve;
7+
8+
// root
9+
export const getWorkSpaceRoot = async () => {
10+
return await findWorkspaceDir(process.cwd());
11+
};
12+
// packages
13+
export const getPackagesRoot = async () => {
14+
return resolve(await getWorkSpaceRoot(), 'packages');
15+
};
16+
// packages/components
17+
export const getComponentsRoot = async () => {
18+
return resolve(await getPackagesRoot(), 'components');
19+
};
20+
// packages/tdesign-vue-next
21+
export const getTdesignVueNextRoot = async () => {
22+
return resolve(await getPackagesRoot(), 'tdesign-vue-next');
23+
};

internal/utils/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './src/paths';
2+
export * from './src/paths-v2';
23
export * from './src/package-json';
4+
export * from './src/exec';

internal/utils/types/src/exec.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { SyncOptions } from 'execa';
2+
declare const run: (
3+
command: string,
4+
options?: SyncOptions,
5+
) => Promise<{
6+
stdout: string;
7+
stderr: string;
8+
}>;
9+
export { run };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare const getWorkSpaceRoot: () => Promise<string>;
2+
export declare const getPackagesRoot: () => Promise<string>;
3+
export declare const getComponentsRoot: () => Promise<string>;
4+
export declare const getTdesignVueNextRoot: () => Promise<string>;

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"scripts": {
1717
"pnpm:devPreinstall": "pnpm run init",
18-
"postinstall": "husky install && pnpm -C internal/utils build",
18+
"postinstall": "husky install && pnpm -C internal/utils prebuild && pnpm -C internal/builds prebuild",
1919
"init": "git submodule init && git submodule update",
2020
"start": "pnpm dev",
2121
"dev": "pnpm -C packages/tdesign-vue-next/site dev",
@@ -25,12 +25,8 @@
2525
"generate:usage": "node script/generate-usage/index.js",
2626
"generate:component": "node script/init/index.js",
2727
"generate:coverage-badge": "pnpm test:unit-coverage && tsx script/test/generate-coverage.js",
28-
"build": "cross-env NODE_ENV=production rollup -c script/rollup.config.js && pnpm build:tsc",
29-
"build:tsc": "run-p build:tsc-*",
30-
"build:tsc-es": "tsc --outDir packages/tdesign-vue-next/es/ -p tsconfig.components.json",
31-
"build:tsc-esm": "tsc --outDir packages/tdesign-vue-next/esm/ -p tsconfig.components.json",
32-
"build:tsc-lib": "tsc --outDir packages/tdesign-vue-next/lib/ -p tsconfig.components.json",
33-
"build:tsc-cjs": "tsc --outDir packages/tdesign-vue-next/cjs/ -p tsconfig.components.json",
28+
"build:vue-next-tsc": "pnpm -C internal/builds build:vue-next-tsc",
29+
"build": "cross-env NODE_ENV=production rollup -c script/rollup.config.js && pnpm build:vue-next-tsc",
3430
"lint:fix": "eslint --ext .vue,.js,.ts,.tsx ./packages --max-warnings 0 --fix --cache",
3531
"lint": "pnpm lint:tsc && eslint --ext .vue,.js,.ts,.tsx ./packages --max-warnings 0 --cache",
3632
"lint:tsc": "tsc --noEmit",

tsconfig.components.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@
5151
],
5252
"exclude": [
5353
"node_modules",
54-
"packages/common",
5554
"dist",
5655
"lib",
5756
"cjs",
5857
"esm",
5958
"es",
6059
"global.d.ts"
6160
]
62-
}
61+
}

0 commit comments

Comments
 (0)