-
Notifications
You must be signed in to change notification settings - Fork 215
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
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 1531255
build(react): Upgrades storybook to 5.2.3; removes addon types
sahlhoff e129be6
chore(react): Updates sectionDecorator to use new type export
sahlhoff 941d7de
build(react): Adds docgen addon to storybook
sahlhoff d6b1eca
build(react): Adds storysource and docgen dependencies
sahlhoff 7c86a51
chore(react): Updates storybook config to use docgen and storysource
sahlhoff b414373
chore(react): Adds example of using csf and addParameter
sahlhoff 226fb76
fix(react): Removes parser option in source-loader
sahlhoff 3bf8dc9
build(react): Adds docgen-plugin to webpack config
sahlhoff 11b209f
fix(react): Fixes type errors
sahlhoff 7b6fffb
fix: Fix storybook rendering regressin
NicholasBoll 9b48aaa
fix: Remove accidental removal of plugin
NicholasBoll ebfe126
chore: Update dependencies
NicholasBoll 0dfec33
refactor: Simplify storybook webpack config
anicholls 1453b4a
chore: Remove unused packages
anicholls b8fcf3b
build: Add correct source-loader config to webpack config
anicholls c403935
chore: Remove deprecated storybook addon
anicholls a26eedc
chore: Re-run yarn with master as basline
anicholls e139088
chore: Update react-scripts and storybook to fix browserslist erros
anicholls f06d867
chore: Update stories to include addParameters
sahlhoff b0c8acb
Merge branch 'master' into storybook-upgrade-5.2
anicholls aa04753
build: Replace CRA default postcss config with CKs original
sahlhoff c7ce65d
build: Pass in path to postcss loader
sahlhoff 12fe5b1
build: Removes storybook presets
sahlhoff dbc302f
Merge branch 'master' into storybook-upgrade-5.2
anicholls 57fdacf
Merge branch 'master' of github.com:Workday/canvas-kit into HEAD
NicholasBoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const docGen = require('react-docgen-typescript'); | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discovered from strothj/react-docgen-typescript-loader#37