Skip to content

Commit 34be88e

Browse files
[docs] Allow codesandbox deploy for demos in X (mui#23644)
1 parent 1e2e4dd commit 34be88e

File tree

3 files changed

+25
-59
lines changed

3 files changed

+25
-59
lines changed

docs/src/modules/utils/getDemoConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function getLanguageConfig(demoData) {
7979
}
8080
}
8181

82-
export default function getDemo(demoData) {
82+
export default function getDemoConfig(demoData) {
8383
const baseConfig = {
8484
title: 'Material demo',
8585
description: demoData.githubLocation,

docs/src/modules/utils/helpers.js

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const upperFirst = require('lodash/upperFirst');
22
const camelCase = require('lodash/camelCase');
3-
const { CODE_VARIANTS, LANGUAGES } = require('../constants');
3+
const { CODE_VARIANTS, SOURCE_CODE_REPO, LANGUAGES } = require('../constants');
44

55
function titleize(string) {
66
if (process.env.NODE_ENV !== 'production') {
@@ -75,32 +75,26 @@ function addTypeDeps(deps) {
7575
}
7676

7777
function includePeerDependencies(deps, versions) {
78-
Object.assign(deps, {
78+
let newDeps = {
79+
...deps,
7980
'react-dom': versions['react-dom'],
8081
react: versions.react,
81-
});
82+
};
8283

83-
if (
84-
deps['@material-ui/lab'] ||
85-
deps['@material-ui/pickers'] ||
86-
deps['@material-ui/x'] ||
87-
deps['@material-ui/x-grid'] ||
88-
deps['@material-ui/x-pickers'] ||
89-
deps['@material-ui/x-tree-view'] ||
90-
deps['@material-ui/data-grid']
91-
) {
92-
deps['@material-ui/core'] = versions['@material-ui/core'];
84+
if (newDeps['@material-ui/lab']) {
85+
newDeps['@material-ui/core'] = versions['@material-ui/core'];
9386
}
9487

95-
if (deps['@material-ui/x-grid-data-generator']) {
96-
deps['@material-ui/core'] = versions['@material-ui/core'];
97-
deps['@material-ui/icons'] = versions['@material-ui/icons'];
98-
deps['@material-ui/lab'] = versions['@material-ui/lab'];
88+
if (window.muiDocConfig) {
89+
newDeps = window.muiDocConfig.csbIncludePeerDependencies(newDeps, { versions });
9990
}
10091

101-
if (deps['@material-ui/pickers']) {
102-
deps['date-fns'] = 'latest';
92+
if (newDeps['@material-ui/pickers']) {
93+
newDeps['date-fns'] = 'latest';
94+
newDeps['@material-ui/core'] = versions['@material-ui/core'];
10395
}
96+
97+
return newDeps;
10498
}
10599

106100
/**
@@ -109,7 +103,7 @@ function includePeerDependencies(deps, versions) {
109103
* @return string - A valid version for a dependency entry in a package.json
110104
*/
111105
function getMuiPackageVersion(packageName, commitRef) {
112-
if (commitRef === undefined) {
106+
if (commitRef === undefined || SOURCE_CODE_REPO !== 'https://github.com/mui-org/material-ui') {
113107
// TODO: change 'next' to 'latest' once next is merged into master.
114108
return 'latest';
115109
}
@@ -122,16 +116,15 @@ function getMuiPackageVersion(packageName, commitRef) {
122116
* @param {object} options
123117
* @param {'JS' | 'TS'} [options.codeLanguage] -
124118
* @param {string} [options.muiCommitRef] - If specified use `@material-ui/*` packages from a specific commit.
125-
* @param {'next' | 'latest'} [options.reactVersion]
126119
* @returns {Record<string, 'latest'>} map of packages with their required version
127120
*/
128121
function getDependencies(raw, options = {}) {
129-
const { codeLanguage = CODE_VARIANTS.JS, muiCommitRef, reactVersion = 'latest' } = options;
122+
const { codeLanguage = CODE_VARIANTS.JS, muiCommitRef } = options;
130123

131-
const deps = {};
132-
const versions = {
133-
'react-dom': reactVersion,
134-
react: reactVersion,
124+
let deps = {};
125+
let versions = {
126+
'react-dom': 'latest',
127+
react: 'latest',
135128
'@material-ui/core': getMuiPackageVersion('core', muiCommitRef),
136129
'@material-ui/icons': getMuiPackageVersion('icons', muiCommitRef),
137130
'@material-ui/lab': getMuiPackageVersion('lab', muiCommitRef),
@@ -142,6 +135,10 @@ function getDependencies(raw, options = {}) {
142135
'@date-io/date-fns': 'v1',
143136
};
144137

138+
if (window.muiDocConfig) {
139+
versions = window.muiDocConfig.csbGetVersions(versions, { muiCommitRef });
140+
}
141+
145142
const re = /^import\s'([^']+)'|import\s[\s\S]*?\sfrom\s+'([^']+)/gm;
146143
let m;
147144
// eslint-disable-next-line no-cond-assign
@@ -161,7 +158,7 @@ function getDependencies(raw, options = {}) {
161158
}
162159
}
163160

164-
includePeerDependencies(deps, versions);
161+
deps = includePeerDependencies(deps, versions);
165162

166163
if (codeLanguage === CODE_VARIANTS.TS) {
167164
addTypeDeps(deps);

docs/src/modules/utils/helpers.test.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,6 @@ const suggestions = [
5656
});
5757
});
5858

59-
it('should support next dependencies', () => {
60-
expect(getDependencies(s1, { reactVersion: 'next' })).to.deep.equal({
61-
'@foo-bar/bip': 'latest',
62-
'@material-ui/core': 'latest',
63-
'prop-types': 'latest',
64-
'react-dom': 'next',
65-
react: 'next',
66-
});
67-
});
68-
6959
it('should support direct import', () => {
7060
const source = `
7161
import 'date-fns';
@@ -136,27 +126,6 @@ import lab from '@material-ui/lab';
136126
});
137127
});
138128

139-
it('should support the data-grid component', () => {
140-
const source = `
141-
import * as React from 'react';
142-
import { DataGrid } from '@material-ui/data-grid';
143-
import { useDemoData } from '@material-ui/x-grid-data-generator';
144-
`;
145-
146-
expect(getDependencies(source, { codeLanguage: 'TS' })).to.deep.equal({
147-
'@material-ui/core': 'latest',
148-
'@material-ui/lab': 'latest',
149-
'@material-ui/icons': 'latest',
150-
'@material-ui/data-grid': 'latest',
151-
'@material-ui/x-grid-data-generator': 'latest',
152-
'@types/react': 'latest',
153-
'@types/react-dom': 'latest',
154-
react: 'latest',
155-
'react-dom': 'latest',
156-
typescript: 'latest',
157-
});
158-
});
159-
160129
it('can use codesandbox deploys if a commit is given', () => {
161130
const source = `
162131
import * as Core from '@material-ui/core';

0 commit comments

Comments
 (0)