Skip to content

Commit

Permalink
[docs] Limit date-fns package to v2 in codesandbox (#11463)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy authored Dec 21, 2023
1 parent 380f1bb commit d7149ef
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
1 change: 1 addition & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ module.exports = {
'**/build/**',
'docs/.next/**',
],
spec: ['packages/**/*.test.{js,ts,tsx}', 'docs/src/modules/**/*.test.{js,ts,tsx}'],
};
9 changes: 2 additions & 7 deletions docs/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'docs/src/modules/components/bootstrap';
// --- Post bootstrap -----
import pages from 'docsx/data/pages'; // DO NOT REMOVE
import { postProcessImport } from 'docsx/src/modules/utils/postProcessImport';
import * as React from 'react';
import { loadCSS } from 'fg-loadcss/src/loadCSS';
import NextHead from 'next/head';
Expand Down Expand Up @@ -41,11 +42,6 @@ ponyfillGlobal.muiDocConfig = {
if (newDeps['@mui/x-data-grid-generator']) {
newDeps['@mui/icons-material'] = versions['@mui/icons-material'];
}

if (newDeps['@mui/x-date-pickers'] || newDeps['@mui/x-date-pickers-pro']) {
newDeps.dayjs = versions.dayjs;
}

return newDeps;
},
csbGetVersions: (versions, { muiCommitRef }) => {
Expand All @@ -59,12 +55,11 @@ ponyfillGlobal.muiDocConfig = {
'@mui/x-date-pickers-pro': getMuiPackageVersion('x-date-pickers-pro', muiCommitRef),
'@mui/x-charts': getMuiPackageVersion('x-charts', muiCommitRef),
'@mui/x-tree-view': getMuiPackageVersion('x-tree-view', muiCommitRef),
'date-fns': 'latest',
dayjs: 'latest',
exceljs: 'latest',
};
return output;
},
postProcessImport,
};

// Client-side cache, shared for the whole session of the user in the browser.
Expand Down
38 changes: 38 additions & 0 deletions docs/src/modules/utils/postProcessImport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from 'chai';
import { DATE_ADAPTER_VERSIONS, ADAPTER_TO_LIBRARY, postProcessImport } from './postProcessImport';

describe('postProcessImport', () => {
const ADAPTERS = ['AdapterDateFns', 'AdapterDayjs', 'AdapterLuxon', 'AdapterMoment'];

describe('@mui/lab imports', () => {
ADAPTERS.forEach((adapter) => {
it('should provide correct adapter', () => {
const resolvedDep = postProcessImport(`@mui/lab/${adapter}`);

const expectedLibrary = ADAPTER_TO_LIBRARY[adapter];
expect(resolvedDep).to.deep.equal({
[expectedLibrary]: DATE_ADAPTER_VERSIONS[expectedLibrary],
});
});
});
});

describe('@mui/x-date-pickers imports', () => {
const ALL_ADAPTERS = [
...ADAPTERS,
'AdapterDateFnsJalali',
'AdapterMomentHijri',
'AdapterMomentJalaali',
];
ALL_ADAPTERS.forEach((adapter) => {
it('should provide correct adapter', () => {
const resolvedDep = postProcessImport(`@mui/x-date-pickers/${adapter}`);

const expectedLibrary = ADAPTER_TO_LIBRARY[adapter];
expect(resolvedDep).to.deep.equal({
[expectedLibrary]: DATE_ADAPTER_VERSIONS[expectedLibrary],
});
});
});
});
});
40 changes: 40 additions & 0 deletions docs/src/modules/utils/postProcessImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export const DATE_ADAPTER_VERSIONS: Record<string, string> = {
'date-fns': '^2.30.0',
'date-fns-jalali': '^2.30.0-0',
dayjs: '^1.11.10',
luxon: '^3.4.4',
moment: '^2.29.4',
'moment-hijri': '^2.1.2',
'moment-jalaali': '^0.10.0',
} as const;

export const ADAPTER_TO_LIBRARY: Record<string, string> = {
AdapterDateFns: 'date-fns',
AdapterDateFnsJalali: 'date-fns-jalali',
AdapterDayjs: 'dayjs',
AdapterLuxon: 'luxon',
AdapterMoment: 'moment',
AdapterMomentHijri: 'moment-hijri',
AdapterMomentJalaali: 'moment-jalaali',
};

const PICKERS_ADAPTER_REGEX = /^@mui\/(lab|x-date-pickers)\/(?<adapterName>Adapter.*)/;

export const postProcessImport = (importName: string): Record<string, string> | null => {
// e.g. date-fns
const dateAdapterMatch = PICKERS_ADAPTER_REGEX.exec(importName);
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 = ADAPTER_TO_LIBRARY[dateAdapterMatch.groups?.adapterName || ''];
if (packageName === undefined) {
throw new TypeError(
`Can't determine required npm package for adapter '${dateAdapterMatch[1]}'`,
);
}
return { [packageName]: DATE_ADAPTER_VERSIONS[packageName] ?? 'latest' };
}
return null;
};
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
"size:why": "yarn size:snapshot --analyze --accurateBundles",
"t": "TZ=UTC node test/cli.js",
"test": "lerna run test --parallel",
"test:coverage": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/**/*.test.{js,ts,tsx}' --exclude '**/node_modules/**' && nyc report -r lcovonly",
"test:coverage:html": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha 'packages/**/*.test.{js,ts,tsx}' --exclude '**/node_modules/**' && nyc report --reporter=html",
"test:coverage": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha --exclude '**/node_modules/**' && nyc report -r lcovonly",
"test:coverage:html": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha --exclude '**/node_modules/**' && nyc report --reporter=html",
"test:karma": "cross-env NODE_ENV=test TZ=UTC karma start test/karma.conf.js",
"test:unit": "cross-env NODE_ENV=test TZ=UTC mocha 'packages/**/*.test.{js,ts,tsx}' -n expose_gc",
"test:unit": "cross-env NODE_ENV=test TZ=UTC mocha -n expose_gc",
"test:e2e": "cross-env NODE_ENV=production yarn test:e2e:build && concurrently --success first --kill-others \"yarn test:e2e:run\" \"yarn test:e2e:server\"",
"test:e2e:build": "webpack --config test/e2e/webpack.config.js",
"test:e2e:dev": "concurrently \"yarn test:e2e:build --watch\" \"yarn test:e2e:server\"",
Expand Down Expand Up @@ -84,7 +84,7 @@
"@mnajdova/enzyme-adapter-react-18": "^0.2.0",
"@mui/icons-material": "^5.15.0",
"@mui/material": "^5.15.0",
"@mui/monorepo": "https://github.com/mui/material-ui.git#master",
"@mui/monorepo": "https://github.com/LukasTy/material-ui.git#fix-date-fns-version",
"@mui/utils": "^5.15.0",
"@next/eslint-plugin-next": "^14.0.4",
"@octokit/plugin-retry": "^6.0.1",
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1877,9 +1877,9 @@
react-is "^18.2.0"
react-transition-group "^4.4.5"

"@mui/monorepo@https://github.com/mui/material-ui.git#master":
"@mui/monorepo@https://github.com/LukasTy/material-ui.git#fix-date-fns-version":
version "5.14.19"
resolved "https://github.com/mui/material-ui.git#a07b4925255aa3fc7ae1255130e182e4573c58d7"
resolved "https://github.com/LukasTy/material-ui.git#9f10bb771d8d5631e5d7cfa5c50d381c9c1f435a"

"@mui/private-theming@^5.15.0":
version "5.15.0"
Expand Down

0 comments on commit d7149ef

Please sign in to comment.