Skip to content

Commit

Permalink
chore(storybook): Update and simplify Storybook configuration (Rocket…
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Aug 9, 2023
1 parent 6ae86ac commit ab73172
Show file tree
Hide file tree
Showing 44 changed files with 1,539 additions and 307 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
diff --git a/dist/generateDocgenCodeBlock.js b/dist/generateDocgenCodeBlock.js
index 0993ac13e4b2aae6d24cf408d6a585b4ddeb7337..1405896291288eb1322d6c42144afd3b4fbd1abf 100644
--- a/dist/generateDocgenCodeBlock.js
+++ b/dist/generateDocgenCodeBlock.js
@@ -34,7 +34,7 @@ function insertTsIgnoreBeforeStatement(statement) {
* ```
*/
function setDisplayName(d) {
- return insertTsIgnoreBeforeStatement(typescript_1.default.createExpressionStatement(typescript_1.default.createBinary(typescript_1.default.createPropertyAccess(typescript_1.default.createIdentifier(d.displayName), typescript_1.default.createIdentifier("displayName")), typescript_1.default.SyntaxKind.EqualsToken, typescript_1.default.createLiteral(d.displayName))));
+ return insertTsIgnoreBeforeStatement(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(d.displayName), typescript_1.default.factory.createIdentifier("displayName")), typescript_1.default.SyntaxKind.EqualsToken, typescript_1.default.factory.createStringLiteral(d.displayName))));
}
/**
* Set a component prop description.
@@ -65,7 +65,7 @@ function createPropDefinition(propName, prop, options) {
*
* @param defaultValue Default prop value or null if not set.
*/
- const setDefaultValue = (defaultValue) => typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral("defaultValue"),
+ const setDefaultValue = (defaultValue) => typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("defaultValue"),
// Use a more extensive check on defaultValue. Sometimes the parser
// returns an empty object.
defaultValue !== null &&
@@ -75,12 +75,19 @@ function createPropDefinition(propName, prop, options) {
(typeof defaultValue.value === "string" ||
typeof defaultValue.value === "number" ||
typeof defaultValue.value === "boolean")
- ? typescript_1.default.createObjectLiteral([
- typescript_1.default.createPropertyAssignment(typescript_1.default.createIdentifier("value"), typescript_1.default.createLiteral(defaultValue.value)),
+ ? typescript_1.default.factory.createObjectLiteralExpression([
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier("value"), typeof defaultValue.value === "string"
+ ? typescript_1.default.factory.createStringLiteral(defaultValue.value)
+ : // eslint-disable-next-line no-nested-ternary
+ typeof defaultValue.value === "number"
+ ? typescript_1.default.factory.createNumericLiteral(defaultValue.value)
+ : defaultValue.value
+ ? typescript_1.default.factory.createTrue()
+ : typescript_1.default.factory.createFalse()),
])
- : typescript_1.default.createNull());
+ : typescript_1.default.factory.createNull());
/** Set a property with a string value */
- const setStringLiteralField = (fieldName, fieldValue) => typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral(fieldName), typescript_1.default.createLiteral(fieldValue));
+ const setStringLiteralField = (fieldName, fieldValue) => typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral(fieldName), typescript_1.default.factory.createStringLiteral(fieldValue));
/**
* ```
* SimpleComponent.__docgenInfo.props.someProp.description = "Prop description.";
@@ -101,7 +108,7 @@ function createPropDefinition(propName, prop, options) {
* ```
* @param required Whether prop is required or not.
*/
- const setRequired = (required) => typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral("required"), required ? typescript_1.default.createTrue() : typescript_1.default.createFalse());
+ const setRequired = (required) => typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("required"), required ? typescript_1.default.factory.createTrue() : typescript_1.default.factory.createFalse());
/**
* ```
* SimpleComponent.__docgenInfo.props.someProp.type = {
@@ -113,7 +120,7 @@ function createPropDefinition(propName, prop, options) {
*/
const setValue = (typeValue) => Array.isArray(typeValue) &&
typeValue.every((value) => typeof value.value === "string")
- ? typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral("value"), typescript_1.default.createArrayLiteral(typeValue.map((value) => typescript_1.default.createObjectLiteral([
+ ? typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("value"), typescript_1.default.factory.createArrayLiteralExpression(typeValue.map((value) => typescript_1.default.factory.createObjectLiteralExpression([
setStringLiteralField("value", value.value),
]))))
: undefined;
@@ -130,9 +137,9 @@ function createPropDefinition(propName, prop, options) {
if (valueField) {
objectFields.push(valueField);
}
- return typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral(options.typePropName), typescript_1.default.createObjectLiteral(objectFields));
+ return typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral(options.typePropName), typescript_1.default.factory.createObjectLiteralExpression(objectFields));
};
- return typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral(propName), typescript_1.default.createObjectLiteral([
+ return typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral(propName), typescript_1.default.factory.createObjectLiteralExpression([
setDefaultValue(prop.defaultValue),
setDescription(prop.description),
setName(prop.name),
@@ -158,10 +165,10 @@ function createPropDefinition(propName, prop, options) {
* @param relativeFilename Relative file path of the component source file.
*/
function insertDocgenIntoGlobalCollection(d, docgenCollectionName, relativeFilename) {
- return insertTsIgnoreBeforeStatement(typescript_1.default.createIf(typescript_1.default.createBinary(typescript_1.default.createTypeOf(typescript_1.default.createIdentifier(docgenCollectionName)), typescript_1.default.SyntaxKind.ExclamationEqualsEqualsToken, typescript_1.default.createLiteral("undefined")), insertTsIgnoreBeforeStatement(typescript_1.default.createStatement(typescript_1.default.createBinary(typescript_1.default.createElementAccess(typescript_1.default.createIdentifier(docgenCollectionName), typescript_1.default.createLiteral(`${relativeFilename}#${d.displayName}`)), typescript_1.default.SyntaxKind.EqualsToken, typescript_1.default.createObjectLiteral([
- typescript_1.default.createPropertyAssignment(typescript_1.default.createIdentifier("docgenInfo"), typescript_1.default.createPropertyAccess(typescript_1.default.createIdentifier(d.displayName), typescript_1.default.createIdentifier("__docgenInfo"))),
- typescript_1.default.createPropertyAssignment(typescript_1.default.createIdentifier("name"), typescript_1.default.createLiteral(d.displayName)),
- typescript_1.default.createPropertyAssignment(typescript_1.default.createIdentifier("path"), typescript_1.default.createLiteral(`${relativeFilename}#${d.displayName}`)),
+ return insertTsIgnoreBeforeStatement(typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createTypeOfExpression(typescript_1.default.factory.createIdentifier(docgenCollectionName)), typescript_1.default.SyntaxKind.ExclamationEqualsEqualsToken, typescript_1.default.factory.createStringLiteral("undefined")), insertTsIgnoreBeforeStatement(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createElementAccessExpression(typescript_1.default.factory.createIdentifier(docgenCollectionName), typescript_1.default.factory.createStringLiteral(`${relativeFilename}#${d.displayName}`)), typescript_1.default.SyntaxKind.EqualsToken, typescript_1.default.factory.createObjectLiteralExpression([
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier("docgenInfo"), typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(d.displayName), typescript_1.default.factory.createIdentifier("__docgenInfo"))),
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier("name"), typescript_1.default.factory.createStringLiteral(d.displayName)),
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier("path"), typescript_1.default.factory.createStringLiteral(`${relativeFilename}#${d.displayName}`)),
]))))));
}
/**
@@ -180,15 +187,15 @@ function insertDocgenIntoGlobalCollection(d, docgenCollectionName, relativeFilen
* @param options Generator options.
*/
function setComponentDocGen(d, options) {
- return insertTsIgnoreBeforeStatement(typescript_1.default.createStatement(typescript_1.default.createBinary(
+ return insertTsIgnoreBeforeStatement(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(
// SimpleComponent.__docgenInfo
- typescript_1.default.createPropertyAccess(typescript_1.default.createIdentifier(d.displayName), typescript_1.default.createIdentifier("__docgenInfo")), typescript_1.default.SyntaxKind.EqualsToken, typescript_1.default.createObjectLiteral([
+ typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(d.displayName), typescript_1.default.factory.createIdentifier("__docgenInfo")), typescript_1.default.SyntaxKind.EqualsToken, typescript_1.default.factory.createObjectLiteralExpression([
// SimpleComponent.__docgenInfo.description
- typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral("description"), typescript_1.default.createLiteral(d.description)),
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("description"), typescript_1.default.factory.createStringLiteral(d.description)),
// SimpleComponent.__docgenInfo.displayName
- typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral("displayName"), typescript_1.default.createLiteral(d.displayName)),
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("displayName"), typescript_1.default.factory.createStringLiteral(d.displayName)),
// SimpleComponent.__docgenInfo.props
- typescript_1.default.createPropertyAssignment(typescript_1.default.createLiteral("props"), typescript_1.default.createObjectLiteral(Object.entries(d.props).map(([propName, prop]) => createPropDefinition(propName, prop, options)))),
+ typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createStringLiteral("props"), typescript_1.default.factory.createObjectLiteralExpression(Object.entries(d.props).map(([propName, prop]) => createPropDefinition(propName, prop, options)))),
]))));
}
function generateDocgenCodeBlock(options) {
@@ -196,7 +203,7 @@ function generateDocgenCodeBlock(options) {
const relativeFilename = path_1.default
.relative("./", path_1.default.resolve("./", options.filename))
.replace(/\\/g, "/");
- const wrapInTryStatement = (statements) => typescript_1.default.createTry(typescript_1.default.createBlock(statements, true), typescript_1.default.createCatchClause(typescript_1.default.createVariableDeclaration(typescript_1.default.createIdentifier("__react_docgen_typescript_loader_error")), typescript_1.default.createBlock([])), undefined);
+ const wrapInTryStatement = (statements) => typescript_1.default.factory.createTryStatement(typescript_1.default.factory.createBlock(statements, true), typescript_1.default.factory.createCatchClause(typescript_1.default.factory.createVariableDeclaration(typescript_1.default.factory.createIdentifier("__react_docgen_typescript_loader_error")), typescript_1.default.factory.createBlock([])), undefined);
const codeBlocks = options.componentDocs.map((d) => wrapInTryStatement([
options.setDisplayName ? setDisplayName(d) : null,
setComponentDocGen(d, options),
@@ -208,7 +215,7 @@ function generateDocgenCodeBlock(options) {
const printer = typescript_1.default.createPrinter({ newLine: typescript_1.default.NewLineKind.LineFeed });
const printNode = (sourceNode) => printer.printNode(typescript_1.default.EmitHint.Unspecified, sourceNode, sourceFile);
// Concat original source code with code from generated code blocks.
- const result = codeBlocks.reduce((acc, node) => `${acc}\n${printNode(node)}`,
+ const result = codeBlocks.reduce((acc, node) => `${acc}\n${printNode(node)}`,
// Use original source text rather than using printNode on the parsed form
// to prevent issue where literals are stripped within components.
// Ref: https://github.com/strothj/react-docgen-typescript-loader/issues/7
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"node": "14.21.3",
"yarn": "1.22.19"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"resolutions": {
"@storybook/react-docgen-typescript-plugin@1.0.2-canary.6.9d540b91e815f8fc2f8829189deb00553559ff63.0": "patch:@storybook/react-docgen-typescript-plugin@npm%3A1.0.2-canary.6.9d540b91e815f8fc2f8829189deb00553559ff63.0#./.yarn/patches/@storybook-react-docgen-typescript-plugin-npm-1.0.2-canary.6.9d540b91e815f8fc2f8829189deb00553559ff63.0-b31cc57c40.patch"
}
}
14 changes: 0 additions & 14 deletions packages/fuselage-toastbar/.storybook/main.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/** @type {import('@storybook/react/types').StorybookConfig} */
module.exports = {
import type { StorybookConfig } from '@storybook/react/types';

const config: StorybookConfig = {
core: {
builder: 'webpack5',
},
features: {
postcss: false,
},
typescript: {
reactDocgen: 'react-docgen-typescript-plugin',
},
addons: ['@storybook/addon-essentials', 'storybook-dark-mode'],
stories: ['../src/**/*.stories.tsx', '../src/**/stories.tsx'],
};

export default config;
17 changes: 0 additions & 17 deletions packages/fuselage-toastbar/.storybook/manager.ts

This file was deleted.

38 changes: 28 additions & 10 deletions packages/fuselage-toastbar/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { DarkModeProvider } from '@rocket.chat/layout';
import { DocsPage, DocsContainer } from '@storybook/addon-docs';
import type { DecoratorFunction } from '@storybook/addons';
import { addParameters } from '@storybook/react';
import '@rocket.chat/icons/dist/rocketchat.css';
import '@rocket.chat/fuselage-polyfills';
import type { ElementType, ReactElement } from 'react';
import React, { Suspense } from 'react';
import type { Parameters } from '@storybook/addons';
import type { DecoratorFn } from '@storybook/react';
import { themes } from '@storybook/theming';
import { Suspense } from 'react';
import { useDarkMode } from 'storybook-dark-mode';

import manifest from '../package.json';
import ToastBarProvider from '../src/ToastBarProvider';
import logo from './logo.svg';

import '@rocket.chat/fuselage/dist/fuselage.css';
import '@rocket.chat/icons/dist/rocketchat.css';
import '@rocket.chat/fuselage-polyfills';

addParameters({
export const parameters: Parameters = {
backgrounds: {
grid: {
cellSize: 4,
Expand All @@ -25,10 +29,24 @@ addParameters({
options: {
storySort: ([, a], [, b]) => a.kind.localeCompare(b.kind),
},
});
darkMode: {
dark: {
...themes.dark,
brandTitle: manifest.name,
brandImage: logo,
brandUrl: manifest.homepage,
},
light: {
...themes.normal,
brandTitle: manifest.name,
brandImage: logo,
brandUrl: manifest.homepage,
},
},
};

export const decorators: DecoratorFunction<ReactElement>[] = [
(Story: ElementType): ReactElement => {
export const decorators: DecoratorFn[] = [
(Story) => {
const dark = useDarkMode();

return (
Expand Down
1 change: 0 additions & 1 deletion packages/fuselage-toastbar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"lint-staged": "~13.2.1",
"npm-run-all": "^4.1.5",
"prettier": "~2.8.7",
"react-docgen-typescript-plugin": "~1.0.5",
"rimraf": "~3.0.2",
"storybook-dark-mode": "~1.1.2",
"ts-jest": "~29.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage-toastbar/src/ToastBarProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactNode, ReactElement } from 'react';
import React, { useState, memo } from 'react';
import { useState, memo } from 'react';

import type { ToastBarPayload } from './ToastBarContext';
import { ToastBarContext } from './ToastBarContext';
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage-toastbar/src/ToastBarTimed.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ToastBar } from '@rocket.chat/fuselage';
import type { ReactElement } from 'react';
import React, { useEffect } from 'react';
import { useEffect } from 'react';

import type { ToastBarPayload } from './ToastBarContext';
import { useToastBarDismiss } from './ToastBarContext';
Expand Down
1 change: 0 additions & 1 deletion packages/fuselage-toastbar/src/ToastBarZone.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import styled from '@rocket.chat/styled';
import type { ReactNode, ReactElement } from 'react';
import React from 'react';

import type { ToastBarPayload } from './ToastBarContext';

Expand Down
4 changes: 3 additions & 1 deletion packages/fuselage-toastbar/tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"module": "CommonJS",
"rootDir": "./src",
"outDir": "./dist/cjs"
},
"include": ["./src"],
"exclude": ["**/*.spec.ts"]
}
6 changes: 6 additions & 0 deletions packages/fuselage-toastbar/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"rootDir": "./src",
"outDir": "./dist/esm"
},
"include": ["./src"],
"exclude": ["**/*.spec.ts"]
}
18 changes: 8 additions & 10 deletions packages/fuselage-toastbar/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "./src",
"target": "es5",
"module": "ESNext",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist/esm",
"strict": true,
"rootDirs": ["./src", "./.storybook"],
"target": "ES5",
"module": "CommonJS",
"outDir": "./dist",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"jsx": "react-jsx"
}
},
"include": ["./src", "./.storybook/**/*"],
"exclude": ["./dist"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = (api) => ({
},
},
],
'@babel/preset-typescript',
[
'@babel/preset-react',
{
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/dist
/node_modules
/storybook-static
!/.babelrc.js
!/.jest
!/.storybook
/.storybook/jest-results.json
1 change: 0 additions & 1 deletion packages/fuselage/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/dist
/node_modules
/storybook-static
/.storybook/jest-results.json
/**/*.mdx
Loading

0 comments on commit ab73172

Please sign in to comment.