Skip to content

Commit 6c9f585

Browse files
committed
share getAliases logic between simple and tutorial
1 parent cffc55d commit 6c9f585

4 files changed

Lines changed: 56 additions & 71 deletions

File tree

examples/simple/getAliases.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import { type Alias } from 'vite';
4+
5+
export const getAliases = (): Alias[] => {
6+
// In codesandbox, we won't have the packages folder
7+
// We ignore errors in this case
8+
try {
9+
const packages = fs.readdirSync(
10+
path.resolve(__dirname, '../../packages')
11+
);
12+
return packages
13+
.map(dirName => {
14+
if (dirName === 'create-react-admin') return null;
15+
const packageJson = JSON.parse(
16+
fs.readFileSync(
17+
path.resolve(
18+
__dirname,
19+
'../../packages',
20+
dirName,
21+
'package.json'
22+
),
23+
'utf8'
24+
)
25+
);
26+
return {
27+
find: packageJson.name,
28+
replacement: path.resolve(
29+
__dirname,
30+
`../../packages/${packageJson.name}/src`
31+
),
32+
};
33+
})
34+
.filter(Boolean) as Alias[];
35+
} catch (error) {
36+
console.warn(
37+
'Unable to read packages folder to get aliases. This is expected if you are running this example in codesandbox.',
38+
error
39+
);
40+
return [];
41+
}
42+
};

examples/simple/vite.config.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import react from '@vitejs/plugin-react';
2-
import path from 'path';
3-
import fs from 'fs';
42
import { defineConfig } from 'vite';
3+
import { getAliases } from './getAliases';
54

65
/**
76
* https://vitejs.dev/config/
@@ -10,30 +9,7 @@ import { defineConfig } from 'vite';
109
export default defineConfig(async () => {
1110
// In codesandbox, we won't have the packages folder
1211
// We ignore errors in this case
13-
const aliases: Record<string, string> = {};
14-
try {
15-
const packages = fs.readdirSync(
16-
path.resolve(__dirname, '../../packages')
17-
);
18-
for (const dirName of packages) {
19-
if (dirName === 'create-react-admin') continue;
20-
const packageJson = JSON.parse(
21-
fs.readFileSync(
22-
path.resolve(
23-
__dirname,
24-
'../../packages',
25-
dirName,
26-
'package.json'
27-
),
28-
'utf8'
29-
)
30-
);
31-
aliases[packageJson.name] = path.resolve(
32-
__dirname,
33-
`../../packages/${packageJson.name}/src`
34-
);
35-
}
36-
} catch {}
12+
const aliases = getAliases();
3713

3814
return {
3915
plugins: [react()],
@@ -43,10 +19,7 @@ export default defineConfig(async () => {
4319
find: /^@mui\/icons-material\/(.*)/,
4420
replacement: '@mui/icons-material/esm/$1',
4521
},
46-
...Object.keys(aliases).map(packageName => ({
47-
find: packageName,
48-
replacement: aliases[packageName],
49-
})),
22+
...aliases,
5023
],
5124
},
5225
server: {
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"compilerOptions": {
3-
"composite": true,
4-
"module": "ESNext",
5-
"moduleResolution": "Node",
6-
"allowSyntheticDefaultImports": true
7-
},
8-
"include": ["vite.config.ts"]
2+
"compilerOptions": {
3+
"composite": true,
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
6+
"allowSyntheticDefaultImports": true
7+
},
8+
"include": ["vite.config.ts", "../simple/getAliases.ts"]
99
}

examples/tutorial/vite.config.ts

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
1-
import { defineConfig } from 'vite';
1+
import { Alias, defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33
import path from 'path';
4-
import fs from 'fs';
4+
import { getAliases } from '../simple/getAliases';
55

66
// https://vitejs.dev/config/
77
export default defineConfig(async () => {
8-
// In codesandbox, we won't have the packages folder
9-
// We ignore errors in this case
10-
const aliases: Record<string, string> = {};
11-
try {
12-
const packages = fs.readdirSync(
13-
path.resolve(__dirname, '../../packages')
14-
);
15-
for (const dirName of packages) {
16-
if (dirName === 'create-react-admin') continue;
17-
const packageJson = JSON.parse(
18-
fs.readFileSync(
19-
path.resolve(
20-
__dirname,
21-
'../../packages',
22-
dirName,
23-
'package.json'
24-
),
25-
'utf8'
26-
)
27-
);
28-
aliases[packageJson.name] = path.resolve(
29-
__dirname,
30-
`../../packages/${packageJson.name}/src`
31-
);
32-
}
33-
} catch {
34-
/* empty */
35-
}
8+
const aliases: Alias[] = getAliases();
369

3710
return {
3811
plugins: [react()],
@@ -54,10 +27,7 @@ export default defineConfig(async () => {
5427
'node_modules/@mui/icons-material'
5528
),
5629
},
57-
...Object.keys(aliases).map(packageName => ({
58-
find: packageName,
59-
replacement: aliases[packageName],
60-
})),
30+
...aliases,
6131
],
6232
},
6333
};

0 commit comments

Comments
 (0)