From 1636135acc70be1981955c1b998cf529c407e3b0 Mon Sep 17 00:00:00 2001 From: Alexey Padalko Date: Tue, 3 Sep 2019 23:24:15 +0300 Subject: [PATCH] build: monkey patch redux connect for sb components --- .storybook/patchedReactRedux.js | 33 +++++++++++++++++++++++++++++++++ .storybook/webpack.config.js | 18 ++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 .storybook/patchedReactRedux.js diff --git a/.storybook/patchedReactRedux.js b/.storybook/patchedReactRedux.js new file mode 100644 index 00000000000..4d976e4bdfc --- /dev/null +++ b/.storybook/patchedReactRedux.js @@ -0,0 +1,33 @@ +export { + Provider, + connectAdvanced, + ReactReduxContext, + batch, + useDispatch, + useSelector, + useStore, + shallowEqual, +} from '../node_modules/react-redux' +import { connect as originalConnect } from '../node_modules/react-redux' + +function defaultMergeProps(stateProps, dispatchProps, ownProps) { + return { ...stateProps, ...dispatchProps, ...ownProps } +} + +/** + * connect - Patches redux connect function to use `defaultMergeProps` that prioritizes `ownProps` over `stateProps` + * + * @param {*} mapStateToProps mapStateToProps + * @param {*} mapDispatchToProps mapDispatchToProps + * @param {*} [mergeProps=defaultMergeProps] mergeProps + * @param {*} rest options + * @returns {*} + */ +export function connect( + mapStateToProps, + mapDispatchToProps, + mergeProps = defaultMergeProps, + ...rest +) { + return originalConnect(mapStateToProps, mapDispatchToProps, mergeProps, ...rest) +} diff --git a/.storybook/webpack.config.js b/.storybook/webpack.config.js index d9b52da1f20..138a0406ffd 100644 --- a/.storybook/webpack.config.js +++ b/.storybook/webpack.config.js @@ -1,12 +1,22 @@ require('@babel/register') +const webpack = require('webpack') const baseConfig = require('../webpack/webpack.config.base') +const path = require('path') -module.exports = ({ config }) => ({ - ...config, - resolve: { +const createResolveConfig = config => { + const resolveConfig = { ...config.resolve, ...baseConfig.default.resolve, - }, + } + + resolveConfig.alias['react-redux'] = path.resolve(__dirname, './patchedReactRedux') + return resolveConfig +} + +module.exports = ({ config }) => ({ + ...config, + resolve: createResolveConfig(config), + module: { ...config.module, rules: [