Skip to content

Commit c19f3e9

Browse files
committed
chore: 统一获取 alias
1 parent 5444f6c commit c19f3e9

File tree

6 files changed

+42
-92
lines changed

6 files changed

+42
-92
lines changed

.dumirc.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { defineConfig } from 'dumi';
2-
import * as Fs from 'fs';
32
import * as Path from 'path';
43
// @ts-ignore
54
import pkg from './package.json';
5+
import { getAlias } from './utils';
66

7-
const pkgs = Fs.readdirSync(Path.resolve(__dirname, 'packages'));
8-
const components = Fs.readdirSync(
9-
Path.resolve(__dirname, 'packages/components/src'),
10-
);
117
const pkgName = pkg.name.replace('-monorepo', '');
128

139
type ENV = 'development' | 'production';
@@ -66,23 +62,6 @@ export default defineConfig({
6662
__dirname,
6763
'packages/react-ui/dist/styles',
6864
),
69-
'@tool-pack/react-ui': Path.resolve(__dirname, 'packages/react-ui/src'),
70-
...pkgs.reduce(
71-
(prev, cur) => {
72-
prev['@pkg/' + cur] = Path.resolve(__dirname, `packages/${cur}/src`);
73-
return prev;
74-
},
75-
{} as Record<string, string>,
76-
),
77-
...components.reduce(
78-
(prev, cur) => {
79-
prev['~/' + cur] = Path.resolve(
80-
__dirname,
81-
`packages/components/src/${cur}`,
82-
);
83-
return prev;
84-
},
85-
{} as Record<string, string>,
86-
),
65+
...getAlias(),
8766
},
8867
});

internal/playground/vite.config.ts

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,20 @@
11
import react from '@vitejs/plugin-react';
2+
import { getAlias } from '../../utils';
23
import { defineConfig } from 'vite';
3-
import Path from 'path';
4-
import Fs from 'fs';
5-
6-
const pkgs = Fs.readdirSync(Path.resolve(__dirname, '../../packages'));
7-
const components = Fs.readdirSync(
8-
Path.resolve(__dirname, '../../packages/components/src'),
9-
);
104

115
/**
126
* Vite configuration
137
* https://vitejs.dev/config/
148
*/
159
export default defineConfig(() => {
1610
return {
17-
resolve: {
18-
alias: {
19-
...pkgs.reduce(
20-
(prev, cur) => {
21-
prev['@pkg/' + cur] = Path.resolve(
22-
__dirname,
23-
`../../packages/${cur}/src`,
24-
);
25-
return prev;
26-
},
27-
{} as Record<string, string>,
28-
),
29-
...components.reduce(
30-
(prev, cur) => {
31-
prev['~/' + cur] = Path.resolve(
32-
__dirname,
33-
`../../packages/components/src/${cur}`,
34-
);
35-
return prev;
36-
},
37-
{} as Record<string, string>,
38-
),
39-
'@tool-pack/react-ui': Path.resolve(
40-
__dirname,
41-
'../../packages/react-ui/src',
42-
),
43-
// '@pkg/components': Path.resolve('./.yalc/@tool-pack/react-ui'),
44-
},
45-
},
4611
plugins: [
4712
// https://github.com/vitejs/vite/tree/main/packages/plugin-react
4813
react(),
4914
],
15+
resolve: {
16+
alias: getAlias(),
17+
},
5018
css: {
5119
devSourcemap: true,
5220
},

packages/react-ui/vite.config.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { defineConfig, UserConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3-
import Path, { resolve } from 'path';
3+
import { getAlias } from '../../utils';
44
import pkg from './package.json';
5-
import Fs from 'fs';
6-
7-
const pkgs = Fs.readdirSync(Path.resolve(__dirname, '../../packages'));
8-
const components = Fs.readdirSync(
9-
Path.resolve(__dirname, '../../packages/components/src'),
10-
);
5+
import { resolve } from 'path';
116

127
function getBanner(format: string) {
138
const date = new Date();
@@ -66,37 +61,15 @@ export default defineConfig((): UserConfig => {
6661
target: 'modules',
6762
emptyOutDir: true,
6863
},
69-
resolve: {
70-
alias: {
71-
// '@pkg/*': Path.resolve(__dirname, '../../packages/*/src'),
72-
...pkgs.reduce(
73-
(prev, cur) => {
74-
prev['@pkg/' + cur] = Path.resolve(
75-
__dirname,
76-
`../../packages/${cur}/src`,
77-
);
78-
return prev;
79-
},
80-
{} as Record<string, string>,
81-
),
82-
...components.reduce(
83-
(prev, cur) => {
84-
prev['~/' + cur] = Path.resolve(
85-
__dirname,
86-
`../../packages/components/src/${cur}`,
87-
);
88-
return prev;
89-
},
90-
{} as Record<string, string>,
91-
),
92-
},
93-
},
9464
plugins: [
9565
// https://github.com/vitejs/vite/tree/main/packages/plugin-react
9666
react({
9767
jsxRuntime: 'classic',
9868
}),
9969
],
70+
resolve: {
71+
alias: getAlias(),
72+
},
10073
cacheDir: `./.cache`,
10174
};
10275
});

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"packages",
5656
"scripts",
5757
".dumirc.ts",
58-
".fatherrc.ts"
58+
".fatherrc.ts",
59+
"utils"
5960
],
6061
"exclude": ["node_modules", "dist", "tmp"]
6162
}

utils/getAlias.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Path from 'path';
2+
import Fs from 'fs';
3+
4+
export function getAlias(): Record<string, string> {
5+
const pkgsPath = Path.resolve(__dirname, '../packages');
6+
const componentsSrcPath = Path.resolve(pkgsPath, 'components/src');
7+
8+
const pkgs = Fs.readdirSync(pkgsPath);
9+
const componentsSrcDirs = Fs.readdirSync(componentsSrcPath);
10+
11+
return {
12+
'@tool-pack/react-ui': Path.resolve(pkgsPath, 'react-ui/src'),
13+
...pkgs.reduce(
14+
(prev, cur) => {
15+
prev['@pkg/' + cur] = Path.resolve(pkgsPath, `${cur}/src`);
16+
return prev;
17+
},
18+
{} as Record<string, string>,
19+
),
20+
...componentsSrcDirs.reduce(
21+
(prev, cur) => {
22+
prev['~/' + cur] = Path.resolve(componentsSrcPath, cur);
23+
return prev;
24+
},
25+
{} as Record<string, string>,
26+
),
27+
};
28+
}

utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './getAlias';

0 commit comments

Comments
 (0)