Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs-infra] Remove explicit mui-x dependency resolving #40257

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions docs/src/modules/sandbox/Dependencies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ import { LocalizationProvider as MuiPickersLocalizationProvider, KeyboardTimePic
'@emotion/styled': 'latest',
'@mui/material': 'latest',
'@mui/lab': 'latest',
'date-fns': 'latest',
});
});

Expand Down Expand Up @@ -130,7 +129,6 @@ import 'exceljs';
'@emotion/styled': 'latest',
'@mui/material': 'latest',
'@mui/lab': 'latest',
'date-fns': 'latest',
exceljs: 'latest',
});
});
Expand Down Expand Up @@ -199,7 +197,6 @@ import {
'@emotion/styled': 'latest',
'@mui/material': 'latest',
'@mui/lab': 'latest',
'date-fns': 'latest',
});
});

Expand Down Expand Up @@ -258,70 +255,6 @@ import * as Utils from '@mui/utils';
});
});

it('should handle date adapters', () => {
const source = `
import * as React from 'react';
import AdapterDateFns from '@mui/lab/AdapterDateFns';
import AdapterDayjs from '@mui/lab/AdapterDayjs';
import AdapterLuxon from '@mui/lab/AdapterLuxon';
import AdapterMoment from '@mui/lab/AdapterMoment';
`;

const { dependencies } = SandboxDependencies({
raw: source,
codeVariant: 'JS',
});

expect(dependencies).to.deep.equal({
react: 'latest',
'react-dom': 'latest',
'@emotion/react': 'latest',
'@emotion/styled': 'latest',
'@mui/material': 'latest',
'@mui/lab': 'latest',
'date-fns': 'latest',
dayjs: 'latest',
luxon: 'latest',
moment: 'latest',
});
});

it('should handle dependencies for @mui/x-date-pickers', () => {
const source = `
import * as React from 'react';
import TextField from '@mui/material/TextField';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
import { AdapterDateFnsJalali } from '@mui/x-date-pickers/AdapterDateFnsJalali';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { AdapterMomentHijri } from '@mui/x-date-pickers/AdapterMomentHijri';
import { AdapterMomentJalaali } from '@mui/x-date-pickers/AdapterMomentJalaali';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';`;

const { dependencies } = SandboxDependencies({
raw: source,
codeVariant: 'JS',
});

expect(dependencies).to.deep.equal({
react: 'latest',
'react-dom': 'latest',
'@emotion/react': 'latest',
'@emotion/styled': 'latest',
'@mui/material': 'latest',
'@mui/x-date-pickers': 'latest',
'date-fns': 'latest',
'date-fns-jalali': 'latest',
dayjs: 'latest',
luxon: 'latest',
moment: 'latest',
'moment-hijri': 'latest',
'moment-jalaali': 'latest',
});
});

it('should not have . as a dependency', () => {
const source = `import * as React from 'react';
import Box, { BoxProps } from '@mui/joy/Box';
Expand Down
50 changes: 13 additions & 37 deletions docs/src/modules/sandbox/Dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { CODE_VARIANTS } from 'docs/src/modules/constants';
import type { MuiProductId } from 'docs/src/modules/utils/getProductInfoFromUrl';

type RegExpMatchArrayWithGroupsOnly<T> = {
groups?: {
[key in keyof T]: string;
};
};
type RegExpMatchArrayWithGroups<T> = (RegExpMatchArray & RegExpMatchArrayWithGroupsOnly<T>) | null;

export default function SandboxDependencies(
demo: {
raw: string;
Expand Down Expand Up @@ -62,6 +55,8 @@ export default function SandboxDependencies(
}

function extractDependencies(raw: string) {
const muiDocConfig = (window as any).muiDocConfig;

function includePeerDependencies(
deps: Record<string, string>,
versions: Record<string, string>,
Expand All @@ -83,8 +78,10 @@ export default function SandboxDependencies(
}

// TODO: consider if this configuration could be injected in a "cleaner" way.
if ((window as any).muiDocConfig) {
newDeps = (window as any).muiDocConfig.csbIncludePeerDependencies(newDeps, { versions });
if (muiDocConfig) {
newDeps = muiDocConfig.csbIncludePeerDependencies(newDeps, {
versions,
});
}

return newDeps;
Expand All @@ -111,9 +108,9 @@ export default function SandboxDependencies(
};

// TODO: consider if this configuration could be injected in a "cleaner" way.
if ((window as any).muiDocConfig) {
if (muiDocConfig) {
const muiCommitRef = process.env.PULL_REQUEST_ID ? process.env.COMMIT_REF : undefined;
versions = (window as any).muiDocConfig.csbGetVersions(versions, { muiCommitRef });
versions = muiDocConfig.csbGetVersions(versions, { muiCommitRef });
}

const re = /^import\s'([^']+)'|import\s[\s\S]*?\sfrom\s+'([^']+)/gm;
Expand All @@ -126,35 +123,14 @@ export default function SandboxDependencies(
fullName.charAt(0) === '@' ? fullName.split('/', 2).join('/') : fullName.split('/', 1)[0];

if (!deps[name] && !name.startsWith('.')) {
deps[name] = versions[name] ? versions[name] : 'latest';
deps[name] = versions[name] ?? 'latest';
}

// e.g. date-fns
const dateAdapterMatch = fullName.match(
/^@mui\/(lab|x-date-pickers)\/(?<adapterName>Adapter.*)/,
) as RegExpMatchArrayWithGroups<{ adapterName: string }>;
if (dateAdapterMatch !== null) {
/**
* Mapping from the date adapter sub-packages to the npm packages they require.
* @example `@mui/x-date-pickers/AdapterDayjs` has a peer dependency on `dayjs`.
*/
const packageName = (
{
AdapterDateFns: 'date-fns',
AdapterDateFnsJalali: 'date-fns-jalali',
AdapterDayjs: 'dayjs',
AdapterLuxon: 'luxon',
AdapterMoment: 'moment',
AdapterMomentHijri: 'moment-hijri',
AdapterMomentJalaali: 'moment-jalaali',
} as Record<string, string>
)[dateAdapterMatch.groups?.adapterName || ''];
if (packageName === undefined) {
throw new TypeError(
`Can't determine required npm package for adapter '${dateAdapterMatch[1]}'`,
);
if (muiDocConfig) {
const resolvedDep = muiDocConfig?.postProcessImport(fullName);
if (resolvedDep) {
deps = { ...deps, ...resolvedDep };
}
deps[packageName] = 'latest';
}
}

Expand Down