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

build: Upgrade Storybook to 5.2 #267

Merged
merged 26 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
afaa09f
build(react): Updates storybook to 5.2.1
sahlhoff Oct 4, 2019
1531255
build(react): Upgrades storybook to 5.2.3; removes addon types
sahlhoff Oct 8, 2019
e129be6
chore(react): Updates sectionDecorator to use new type export
sahlhoff Oct 8, 2019
941d7de
build(react): Adds docgen addon to storybook
sahlhoff Oct 9, 2019
d6b1eca
build(react): Adds storysource and docgen dependencies
sahlhoff Oct 15, 2019
7c86a51
chore(react): Updates storybook config to use docgen and storysource
sahlhoff Oct 15, 2019
b414373
chore(react): Adds example of using csf and addParameter
sahlhoff Oct 15, 2019
226fb76
fix(react): Removes parser option in source-loader
sahlhoff Oct 16, 2019
3bf8dc9
build(react): Adds docgen-plugin to webpack config
sahlhoff Oct 16, 2019
11b209f
fix(react): Fixes type errors
sahlhoff Oct 23, 2019
7b6fffb
fix: Fix storybook rendering regressin
NicholasBoll Oct 23, 2019
9b48aaa
fix: Remove accidental removal of plugin
NicholasBoll Oct 23, 2019
ebfe126
chore: Update dependencies
NicholasBoll Oct 24, 2019
0dfec33
refactor: Simplify storybook webpack config
anicholls Oct 25, 2019
1453b4a
chore: Remove unused packages
anicholls Oct 25, 2019
b8fcf3b
build: Add correct source-loader config to webpack config
anicholls Oct 25, 2019
c403935
chore: Remove deprecated storybook addon
anicholls Oct 25, 2019
a26eedc
chore: Re-run yarn with master as basline
anicholls Oct 25, 2019
e139088
chore: Update react-scripts and storybook to fix browserslist erros
anicholls Oct 28, 2019
f06d867
chore: Update stories to include addParameters
sahlhoff Oct 28, 2019
b0c8acb
Merge branch 'master' into storybook-upgrade-5.2
anicholls Oct 28, 2019
aa04753
build: Replace CRA default postcss config with CKs original
sahlhoff Oct 29, 2019
c7ce65d
build: Pass in path to postcss loader
sahlhoff Oct 29, 2019
12fe5b1
build: Removes storybook presets
sahlhoff Oct 29, 2019
dbc302f
Merge branch 'master' into storybook-upgrade-5.2
anicholls Oct 30, 2019
57fdacf
Merge branch 'master' of github.com:Workday/canvas-kit into HEAD
NicholasBoll Nov 4, 2019
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
3 changes: 2 additions & 1 deletion .storybook/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'storybook-readme/register';
import 'prismjs/components/prism-tsx';

import '@storybook/addon-actions/register';
import '@storybook/addon-options/register';
import '@storybook/addon-viewport/register';
import '@storybook/addon-knobs/register';
import '@storybook/addon-docs/register';
import '@storybook/addon-storysource/register';
16 changes: 12 additions & 4 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {configure, addDecorator, addParameters, forceReRender} from '@storybook/react';
import {DocsPage, DocsContainer} from '@storybook/addon-docs/blocks';
import {withKnobs} from '@storybook/addon-knobs/react';
import {create} from '@storybook/theming';
import addons from '@storybook/addons';
Expand All @@ -12,7 +13,13 @@ import {InputProviderDecorator, FontsDecorator} from '../utils/storybook';
const req = require.context('../modules', true, /stories.*\.tsx?$/);

function loadStories() {
req.keys().forEach(req);
const allExports = [];
req.keys().forEach(fname => {
const story = req(fname);
if (story.default) allExports.push(story);
});

return allExports;
}

addDecorator(withKnobs);
Expand All @@ -28,9 +35,10 @@ addParameters({
mainBackground: commonColors.backgroundAlt,
}),
},
});

addParameters({
docs: {
container: DocsContainer,
page: DocsPage,
},
readme: {
codeTheme: 'github',
},
Expand Down
65 changes: 65 additions & 0 deletions .storybook/docgen-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const docGen = require('react-docgen-typescript');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

const docGenLoader = require('react-docgen-typescript-loader/dist/generateDocgenCodeBlock.js');
const ts = require('typescript');
const path = require('path');
const tsconfigPath = path.join(__dirname, './tsconfig.json');

const propFilter = prop => {
if (prop.parent) {
return !prop.parent.fileName.includes('node_modules');
}

return true;
};

class DocgenPlugin {
apply(compiler) {
const pathRegex = RegExp(`modules.+\.tsx`);
compiler.hooks.compilation.tap('DocgenPlugin', compilation => {
compilation.hooks.seal.tap('DocgenPlugin', modules => {
const modulesToProcess = [];
compilation.modules.forEach(module => {
// Skip ignored / external modules
if (!module.built || module.external || !module.rawRequest) {
return;
}
if (pathRegex.test(module.request)) {
modulesToProcess.push(module);
}
});
const tsProgram = ts.createProgram(modulesToProcess.map(v => v.userRequest), {
jsx: ts.JsxEmit.React,
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.Latest,
});
modulesToProcess.forEach(m => processModule(m, tsProgram));
});
});
}
}

processModule = (module, tsProgram) => {
if (!module) return;

const componentDocs = docGen
.withCustomConfig(tsconfigPath, {propFilter: propFilter})
.parseWithProgramProvider(module.userRequest, () => tsProgram);

if (!componentDocs.length) return;

let source = module._source._value;
source +=
'\n' +
docGenLoader
.default({
filename: module.userRequest,
source: module.userRequest,
componentDocs,
docgenCollectionName: 'STORYBOOK_REACT_CLASSES',
setDisplayName: true,
})
.substring(module.userRequest.length) +
'\n';
module._source._value = source;
};
module.exports = DocgenPlugin;
158 changes: 45 additions & 113 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,70 @@
const path = require('path');
const HappyPack = require('happypack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const DocgenPlugin = require('./docgen-plugin');

const modulesPath = path.resolve(__dirname, '../modules');
const utilsPath = path.resolve(__dirname, '../utils');
const postcssConfigPath = path.resolve(__dirname, './postcss.config');

const babelLoader = {
loader: 'babel-loader',
options: {
plugins: [
'@babel/plugin-transform-modules-commonjs',
[
'emotion',
{
autoLabel: true,
labelFormat: '[filename]__[local]',
},
],
],
},
};

const customRules = [
{
test: /\.tsx?$/,
exclude: /node_modules/,
include: [modulesPath, utilsPath],
loader: 'happypack/loader?id=ts',
},
{
test: /\.scss$/,
include: modulesPath,
use: [
{
loader: 'style-loader', // creates style nodes from JS strings
options: {
insertAt: {
before: '[data-emotion]',
},
},
},
{
loader: 'css-loader',
},
{
loader: 'postcss-loader',
options: {
config: {
path: require.resolve('./postcss.config.js'),
},
sourceMap: true,
},
},
{
loader: 'sass-loader', // compiles Sass to CSS
options: {
includePaths: [modulesPath],
sourceMap: true,
},
},
],
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader',
options: {
insertAt: {
before: '[data-emotion]',
},
},
},
'css-loader',
],
},
{
test: /\.(jpe?g|png|gif|svg|ttf)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
],
},
];

// TODO: We merge with Create React App's webpack.config.js since we include `react-scripts` as a devDep
// There is some config from CRA that we need in order to build Storybook correctly
// If you build without it, you get weird behavior like Button backgrounds disappearing
module.exports = async ({config}) => {
module.exports = ({config, mode}) => {
// Exclude all node_modules from babel-loader
config.module.rules
.find(rule => /mjs\|jsx/.test(rule.test.toString()))
.exclude.push(/node_modules/);

// Remove any scss/sass rules that ship with storybook
config.module.rules = config.module.rules.filter(rule => !/scss|sass/.test(rule.test.toString()));

// Filter out extraneous rules added by CRA (react-scripts)
// react-scripts automatically adds js/ts matchers for a `src` folder which we don't use so these rules are moot
config.module.rules = config.module.rules.filter(
rule => !/js\|mjs\|jsx\|ts\|tsx/.test(rule.test.toString())
);

// Add our custom rules
config.module.rules.push(...customRules);
// Override CRA postcss presets
config.module.rules.forEach(rule => {
if (rule.test.toString().includes('scss|sass')) {
delete rule.use[2].options.plugins;

rule.use[2].options.config = {
path: postcssConfigPath,
};
}
});

// Add `.ts` and `.tsx` as a resolvable extension.
config.resolve.extensions = ['.ts', '.tsx', '.js', '.jsx'];

config.plugins.push(
new HappyPack({
id: 'ts',
threads: 2,
loaders: [
babelLoader,
{
path: 'ts-loader',
query: {
happyPackMode: true,
configFile: path.join(__dirname, './tsconfig.json'),
// Load all module files and transpile using babel + ts
config.module.rules.push({
test: /\.(ts|tsx)$/,
include: [modulesPath, utilsPath],
loader: require.resolve('babel-loader'),
options: {
presets: [['react-app', {flow: false, typescript: true}]],
plugins: [
'@babel/plugin-transform-modules-commonjs',
[
'emotion',
{
autoLabel: true,
labelFormat: '[filename]__[local]',
},
},
],
],
}),
new ForkTsCheckerWebpackPlugin({
checkSyntacticErrors: true,
tsconfig: path.join(__dirname, 'tsconfig.json'),
eslint: true,
})
);
},
});

// Load the source code of story files to display in docs.
config.module.rules.push({
test: /stories.*\.tsx?$/,
include: [modulesPath],
loaders: [
{
loader: require.resolve('@storybook/source-loader'),
options: {parser: 'typescript'},
},
],
enforce: 'pre',
});

config.plugins.push(new DocgenPlugin());

return config;
};
1 change: 1 addition & 0 deletions modules/_labs/header/react/stories/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class SearchWithAutoComplete extends React.Component<
}

storiesOf('Labs/Header/React', module)
.addParameters({component: Header})
.addDecorator(withReadme(README))
.addDecorator(withKnobs)
.add('Global Header', () => (
Expand Down
1 change: 1 addition & 0 deletions modules/_labs/menu/react/stories/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class CustomMenuItem extends React.Component<MenuItemProps> {
}

storiesOf('Labs/Menu/React', module)
.addParameters({component: Menu})
.addDecorator(withReadme(README))
.add('Default', () => (
<div className="story">
Expand Down
1 change: 1 addition & 0 deletions modules/action-bar/react/stories/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import README from '../README.md';

storiesOf('Action Bar', module)
.addDecorator(withReadme(README))
.addParameters({component: ActionBar})
.add('Default', () => (
<div className="story">
<ActionBar>
Expand Down
2 changes: 2 additions & 0 deletions modules/avatar/react/stories/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const handleAvatarButtonClick = (e: React.SyntheticEvent) => {
storiesOf('Avatar/Default', module)
.addDecorator(withReadme(README))
.addDecorator(withKnobs)
.addParameters({component: Avatar})
.add('Light', () => (
<div className="story">
<h3>Extra-Extra Large</h3>
Expand Down Expand Up @@ -68,6 +69,7 @@ storiesOf('Avatar/Default', module)
storiesOf('Avatar/AvatarButton', module)
.addDecorator(withReadme(README))
.addDecorator(withKnobs)
.addParameters({component: AvatarButton})
.add('Light', () => (
<div className="story">
<h3>Extra-Extra Large</h3>
Expand Down
4 changes: 3 additions & 1 deletion modules/banner/react/stories/stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../../../../typings.d.ts" />
import * as React from 'react';
import {storiesOf} from '@storybook/react';
import {withKnobs, select, text} from '@storybook/addon-knobs/react';
import {withKnobs, select, text} from '@storybook/addon-knobs';
import withReadme from 'storybook-readme/with-readme';

import Banner from '../index';
Expand All @@ -12,6 +12,7 @@ const handleBannerClick = (e: React.SyntheticEvent) => {
};

storiesOf('Banner/Alert', module)
.addParameters({component: Banner})
.addDecorator(withReadme(README))
.addDecorator(withKnobs)
.add('Full', () => (
Expand Down Expand Up @@ -52,6 +53,7 @@ storiesOf('Banner/Alert', module)
));

storiesOf('Banner/Error', module)
.addParameters({component: Banner})
.addDecorator(withReadme(README))
.addDecorator(withKnobs)
.add('Full', () => (
Expand Down
4 changes: 4 additions & 0 deletions modules/button/react/stories/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const buttonContainer = {
};

storiesOf('Button', module)
.addParameters({component: Button})
.addDecorator(withReadme(README))
.add('Primary', () => (
<div className="story">
Expand Down Expand Up @@ -248,6 +249,7 @@ storiesOf('Button', module)
));

storiesOf('Button/Text', module)
.addParameters({component: TextButton})
.addDecorator(withReadme(README))
.add('Default', () => (
<div className="story">
Expand Down Expand Up @@ -339,6 +341,7 @@ storiesOf('Button/Text', module)
));

storiesOf('Button/Outline', module)
.addParameters({component: Button})
.addDecorator(withReadme(README))
.add('Primary', () => (
<div className="story">
Expand Down Expand Up @@ -551,6 +554,7 @@ storiesOf('Button/Outline', module)
));

storiesOf('Button/Dropdown', module)
.addParameters({component: DropdownButton})
.addDecorator(withReadme(README))
.add('Primary', () => (
<div className="story">
Expand Down
1 change: 1 addition & 0 deletions modules/button/react/stories/stories_deprecated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {deprecated_Button as Button} from '../index';
import README from '../README.md';

storiesOf('Button/Deprecated', module)
.addParameters({component: Button})
.addDecorator(withReadme(README))
.add('Primary', () => (
<div className="story">
Expand Down
Loading