Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tech Stories
---

Improve local Storybook performance ([#10762](https://github.com/linode/manager/pull/10762))
13 changes: 12 additions & 1 deletion packages/manager/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { StorybookConfig } from '@storybook/react-vite';
import { mergeConfig } from 'vite';
import { getReactDocgenTSFileGlobs } from './utils';

const typeScriptFileGlobs = getReactDocgenTSFileGlobs();

const config: StorybookConfig = {
stories: [
Expand All @@ -22,6 +25,11 @@ const config: StorybookConfig = {
},
typescript: {
reactDocgenTypescriptOptions: {
// Speeds up Storybook build time
compilerOptions: {
allowSyntheticDefaultImports: false,
esModuleInterop: false,
},
Comment on lines +28 to +32

@hana-akamai hana-akamai Aug 9, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this upon further investigation storybookjs/storybook#24943 (comment)

// makes union prop types like variant and size appear as select controls
shouldExtractLiteralValuesFromEnum: true,
// makes string and boolean types that can be undefined appear as inputs and switches
Expand All @@ -31,8 +39,11 @@ const config: StorybookConfig = {
prop.parent
? !/node_modules\/(?!@mui)/.test(prop.parent.fileName)
: true,
// Only compile files that have stories for faster local development performance
include: /(development|test)/i.test(process.env.NODE_ENV ?? '')
? typeScriptFileGlobs
: undefined,
},

reactDocgen: 'react-docgen-typescript',
},
docs: {
Expand Down
27 changes: 27 additions & 0 deletions packages/manager/.storybook/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getReactDocgenTSFileGlobs } from './utils';

describe('getReactDocgenTSFileGlobs', () => {
const typeScriptFileGlobs = getReactDocgenTSFileGlobs();
it('should return component and feature globs for storybook files', () => {
expect(
typeScriptFileGlobs.some(
(file) => file === 'src/components/Button/**/*.{ts,tsx}'
)
).toBe(true);
expect(
typeScriptFileGlobs.some(
(file) => file === 'src/components/Paper.{ts,tsx}'
)
).toBe(true);
expect(
typeScriptFileGlobs.some(
(file) => file === 'src/features/TopMenu/**/*.{ts,tsx}'
)
).toBe(true);
expect(
typeScriptFileGlobs.some(
(file) => file === 'src/features/Longview/**/*.{ts,tsx}'
)
).toBe(false);
});
Comment thread
hana-akamai marked this conversation as resolved.
});
28 changes: 28 additions & 0 deletions packages/manager/.storybook/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import globby from 'globby';

const PATTERN = 'src/**/*.stories.tsx';

/**
* Find all storybook files, then return the glob containing the parent component/feature.
* To be used in main.ts to tell react-docgen-typescript which files to compile.
* https://github.com/linode/manager/pull/10762
*
* Example: src/components/Button/Button.stories.tsx -> src/components/Button/**\/*.{ts,tsx}
*/
export const getReactDocgenTSFileGlobs = () => {
const filesWithStories = globby.sync(PATTERN);
const files: string[] = [];

filesWithStories.forEach((file) => {
const execArr = /(src\/(components|features)\/[a-zA-Z]*(.|\/))/.exec(file);
if (execArr) {
const isDirectory = execArr[3] === '/';
const fileBlob = `${execArr[0]}${isDirectory ? '**/*.' : ''}{ts,tsx}`;
if (!files.includes(fileBlob)) {
Comment thread
hana-akamai marked this conversation as resolved.
files.push(fileBlob);
}
}
});

return files;
};