Skip to content

Conversation

jbadan
Copy link
Contributor

@jbadan jbadan commented Feb 15, 2020

Description

By removing dynamic requires, consumers will not be bringing in all of fundamental-styles with each component.

"...This means dynamic requires are supported but will cause all matching modules to be included in the bundle." https://webpack.js.org/guides/dependency-management/#require-with-expression

I decided to remove withStyles instead of repurposing it. Two reasons:

  1. passing cssFile: require('fundamental-styles/dist/button.css) to withStyles will cause the css to always be loaded and not respect disableStyles prop.
  2. passing cssFile: 'fundamental-styles/dist/button.css' and then calling it like require(cssFile) in withStyles does not work because of the following:

"Fully dynamic statements, such as import(foo), will fail because webpack requires at least some file location information.The import() must contain at least some information about where the module is located, so bundling can be limited to a specific directory or set of files.
Just in case someone wonders, the same rule applies for dynamic require statements"
https://stackoverflow.com/questions/44166393/uncaught-error-cannot-find-module-when-using-dynamic-import-for-javascript

On the remaining class components (which we need to convert at some point), I used componentDidMount and on functional components useEffect.

I also removed customStyles prop. Because we are not using a hoc anymore, we can just import our custom styles in our wrapper inside nui-widgets.

@netlify
Copy link

netlify bot commented Feb 15, 2020

Deploy preview for fundamental-react ready!

Built with commit 25ae8ff

https://deploy-preview-873--fundamental-react.netlify.com

@jbadan jbadan requested a review from a team February 15, 2020 01:08
@@ -23,6 +22,12 @@ class DatePicker extends Component {
this.calendarRef = React.createRef();
}

componentDidMount() {
if (!this.props.disableStyles) {
require('fundamental-styles/dist/popover.css');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This whole section can be removed if #858 is merged first. That rebuilds this with a popover so we get those styles from popover component.

@jbadan jbadan changed the title feat: replace withStyles wrapper with useEffect - reduce bundle size feat: replace withStyles wrapper with useEffect and remove customStyles property Feb 17, 2020
@@ -498,6 +503,4 @@ Shellbar.propDescriptions = {
subtitle: 'Displays an application context. Should be used rarely.'
};

export { Shellbar as __Shellbar };

export default withStyles(Shellbar, { cssFile: ['shellbar', 'product-switch'], fonts: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

should this file also require('fundamental-styles/dist/fonts.css'); since fonts was true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope - there's a bunch of other components imported into this component that already require fonts. That was an oversight to include them in the first place with withStyles

.add('custom styles', () => (
<Image
customStyles={require('../../utils/WithStyles/customStylesTest.css')}
photo='https://content.fortune.com/wp-content/uploads/2019/07/hippocorn.jpg'
Copy link
Contributor

Choose a reason for hiding this comment

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

just so we know, this is what we're losing :(
hippocorn

Copy link
Contributor

@greg-a-smith greg-a-smith left a comment

Choose a reason for hiding this comment

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

I think this looks good. 🚢

Copy link
Contributor

@skvale skvale left a comment

Choose a reason for hiding this comment

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

LGTM

@jbadan jbadan merged commit 6b72a7e into master Feb 17, 2020
@jbadan jbadan deleted the fix/with-styles branch February 17, 2020 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants