Skip to content

Commit cffc55d

Browse files
committed
Fix vite configuration for tutorial example.
1 parent bd7e28f commit cffc55d

1 file changed

Lines changed: 58 additions & 22 deletions

File tree

examples/tutorial/vite.config.ts

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
33
import path from 'path';
4+
import fs from 'fs';
45

56
// https://vitejs.dev/config/
6-
export default defineConfig({
7-
plugins: [react()],
8-
resolve: {
9-
preserveSymlinks: true,
10-
alias: [
11-
// The 2 next aliases are needed to avoid having multiple MUI instances
12-
{
13-
find: '@mui/material',
14-
replacement: path.resolve(
15-
__dirname,
16-
'node_modules/@mui/material'
17-
),
18-
},
19-
{
20-
find: '@mui/icons-material',
21-
replacement: path.resolve(
22-
__dirname,
23-
'node_modules/@mui/icons-material'
24-
),
25-
},
26-
],
27-
},
7+
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+
}
36+
37+
return {
38+
plugins: [react()],
39+
resolve: {
40+
preserveSymlinks: true,
41+
alias: [
42+
// The 2 next aliases are needed to avoid having multiple MUI instances
43+
{
44+
find: '@mui/material',
45+
replacement: path.resolve(
46+
__dirname,
47+
'node_modules/@mui/material'
48+
),
49+
},
50+
{
51+
find: '@mui/icons-material',
52+
replacement: path.resolve(
53+
__dirname,
54+
'node_modules/@mui/icons-material'
55+
),
56+
},
57+
...Object.keys(aliases).map(packageName => ({
58+
find: packageName,
59+
replacement: aliases[packageName],
60+
})),
61+
],
62+
},
63+
};
2864
});

0 commit comments

Comments
 (0)