Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

chore(docs): make HMR working properly #1370

Merged
merged 10 commits into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 docs/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { hot } from 'react-hot-loader/root'
import { Provider, themes } from '@stardust-ui/react'

import { mergeThemes } from 'src/lib'
Expand Down Expand Up @@ -40,4 +41,4 @@ class App extends React.Component<any, ThemeContextData> {
}
}

export default App
export default hot(App)
Original file line number Diff line number Diff line change
Expand Up @@ -400,36 +400,6 @@ class ComponentExample extends React.Component<ComponentExampleProps, ComponentE
) : null
}

renderError = () => {
return (
<SourceRender.Consumer>
{({ error }) =>
error && (
<Segment inverted color="red">
<pre style={{ whiteSpace: 'pre-wrap' }}>{error.toString()}</pre>
</Segment>
)
}
</SourceRender.Consumer>
)
}

renderHTML = () => {
const { showCode } = this.state
if (!showCode) return null

return (
<SourceRender.Consumer>
{props => (
<div {...props}>
<Divider fitted />
<CodeSnippet fitted label="Rendered HTML" mode="html" value={props.markup} />
</div>
)}
</SourceRender.Consumer>
)
}

renderVariables = () => {
const { showVariables } = this.state
if (!showVariables) return undefined
Expand Down Expand Up @@ -537,39 +507,53 @@ class ComponentExample extends React.Component<ComponentExampleProps, ComponentE
<SourceRender
babelConfig={babelConfig}
source={currentCode}
render={this.renderElement}
renderHtml={showCode}
resolver={importResolver}
themeName={themeName}
wrap={this.renderElement}
unstable_hot
>
<Provider.Consumer
render={({ siteVariables }) => {
return (
<Segment
className={`rendered-example ${this.getKebabExamplePath()}`}
styles={{
padding: '2rem',
color: siteVariables.bodyColor,
backgroundColor: siteVariables.bodyBackground,
...(showTransparent && {
backgroundImage:
'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAKUlEQVQoU2NkYGAwZkAD////RxdiYBwKCv///4/hGUZGkNNRAeMQUAgAtxof+nLDzyUAAAAASUVORK5CYII=")',
backgroundRepeat: 'repeat',
}),
}}
>
<SourceRender.Consumer>{({ element }) => element}</SourceRender.Consumer>
</Segment>
)
}}
/>
<Segment styles={{ padding: 0 }}>
{this.renderSourceCode()}
{this.renderError()}
{this.renderHTML()}
{this.renderVariables()}
</Segment>
<div style={{ paddingBottom: '10px' }} />
{({ element, error, markup }) => (
<>
<Provider.Consumer
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need Provider.Consumer more there

render={({ siteVariables }) => {
return (
<Segment
className={`rendered-example ${this.getKebabExamplePath()}`}
styles={{
padding: '2rem',
color: siteVariables.bodyColor,
backgroundColor: siteVariables.bodyBackground,
...(showTransparent && {
backgroundImage:
'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAKUlEQVQoU2NkYGAwZkAD////RxdiYBwKCv///4/hGUZGkNNRAeMQUAgAtxof+nLDzyUAAAAASUVORK5CYII=")',
backgroundRepeat: 'repeat',
}),
}}
>
{element}
</Segment>
)
}}
/>
<Segment styles={{ padding: 0 }}>
{this.renderSourceCode()}
{error && (
<Segment inverted color="red">
<pre style={{ whiteSpace: 'pre-wrap' }}>{error.toString()}</pre>
</Segment>
)}
{showCode && (
<div>
<Divider fitted />
<CodeSnippet fitted label="Rendered HTML" mode="html" value={markup} />
</div>
)}
{this.renderVariables()}
</Segment>
<div style={{ paddingBottom: '10px' }} />
</>
)}
</SourceRender>
</KnobProvider>
</Flex.Item>
Expand Down
35 changes: 14 additions & 21 deletions docs/src/components/ExternalExampleLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,21 @@ class ExternalExampleLayout extends React.Component<

return (
<Provider key={this.state.renderId} theme={theme}>
<Provider.Consumer
render={({ siteVariables }) => (
<SourceRender
babelConfig={babelConfig}
source={exampleSource.js}
renderHtml={false}
resolver={importResolver}
>
<SourceRender.Consumer>
{({ element, error }) => (
<>
{element}
{/* This block allows to see issues with examples as visual regressions. */}
{error && (
<div style={{ fontSize: '5rem', color: 'red' }}>{error.toString()}</div>
)}
</>
)}
</SourceRender.Consumer>
</SourceRender>
<SourceRender
babelConfig={babelConfig}
source={exampleSource.js}
renderHtml={false}
resolver={importResolver}
unstable_hot
>
{({ element, error }) => (
<>
{element}
{/* This block allows to see issues with examples as visual regressions. */}
{error && <div style={{ fontSize: '5rem', color: 'red' }}>{error.toString()}</div>}
</>
)}
/>
</SourceRender>
</Provider>
)
}
Expand Down
46 changes: 6 additions & 40 deletions docs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,12 @@ import * as ReactDOM from 'react-dom'
import { AppContainer } from 'react-hot-loader'
import App from './app'

// ----------------------------------------
// Rendering
// ----------------------------------------

const mountNode = document.createElement('div')
document.body.appendChild(mountNode)

const render = NewApp =>
ReactDOM.render(
<AppContainer>
<NewApp />
</AppContainer>,
mountNode,
)

// ----------------------------------------
// HMR
// ----------------------------------------

if (__DEV__) {
// When the application source code changes, re-render the whole thing.
if (module.hot) {
module.hot.accept('./app', () => {
// restore scroll
const { scrollLeft, scrollTop } = document.scrollingElement!
ReactDOM.unmountComponentAtNode(mountNode)

try {
render(require('./app').default)
document.scrollingElement!.scrollTop = scrollTop
document.scrollingElement!.scrollLeft = scrollLeft
} catch (e) {
console.error(e)
}
})
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This stuff will be done by react-hot-loader


// ----------------------------------------
// Start the app
// ----------------------------------------

render(App)
ReactDOM.render(
<AppContainer>
<App />
</AppContainer>,
mountNode,
)
36 changes: 24 additions & 12 deletions docs/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ import AccessibilityBehaviors from './views/AccessibilityBehaviors'
import FocusZone from './views/FocusZone'
import FocusTrapZone from './views/FocusTrapZone'
import AutoFocusZone from './views/AutoFocusZone'
import ChatPanePrototype from './prototypes/chatPane'
import ChatMessagesPrototype from './prototypes/chatMessages'
import AsyncShorthandPrototype from './prototypes/AsyncShorthand'
import EmployeeCardPrototype from './prototypes/employeeCard'
import MeetingOptionsPrototype from './prototypes/meetingOptions'
import SearchPagePrototype from './prototypes/SearchPage'
import MentionsPrototype from './prototypes/mentions'
import DropdownsPrototype from './prototypes/dropdowns'
import PopupsPrototype from './prototypes/popups'
import IconViewerPrototype from './prototypes/IconViewer'
import MenuButtonPrototype from './prototypes/MenuButton'
import AlertsPrototype from './prototypes/alerts'
Copy link
Member Author

@layershifter layershifter May 23, 2019

Choose a reason for hiding this comment

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

We're using Webpack 4, these imports will be tree-shaken in production

This change is required: to prevent errors related to HMR like this:
image


const Router = () => (
<BrowserRouter basename={__BASENAME__}>
Expand All @@ -35,74 +47,74 @@ const Router = () => (
exact
key="/prototype-chat-pane"
path="/prototype-chat-pane"
component={require('./prototypes/chatPane/index').default}
component={ChatPanePrototype}
/>,
<DocsLayout
exact
key="/prototype-chat-messages"
path="/prototype-chat-messages"
component={require('./prototypes/chatMessages/index').default}
component={ChatMessagesPrototype}
/>,
,
<DocsLayout
exact
key="/prototype-async-shorthand"
path="/prototype-async-shorthand"
component={require('./prototypes/AsyncShorthand/index').default}
component={AsyncShorthandPrototype}
/>,
<DocsLayout
exact
key="/prototype-employee-card"
path="/prototype-employee-card"
component={require('./prototypes/employeeCard/index').default}
component={EmployeeCardPrototype}
/>,
<DocsLayout
exact
key="/prototype-meeting-options"
path="/prototype-meeting-options"
component={require('./prototypes/meetingOptions/index').default}
component={MeetingOptionsPrototype}
/>,
<DocsLayout
exact
key="/prototype-search-page"
path="/prototype-search-page"
component={require('./prototypes/SearchPage/index').default}
component={SearchPagePrototype}
/>,
<DocsLayout
exact
key="/prototype-mentions"
path="/prototype-mentions"
component={require('./prototypes/mentions/index').default}
component={MentionsPrototype}
/>,
<DocsLayout
exact
key="/prototype-dropdowns"
path="/prototype-dropdowns"
component={require('./prototypes/dropdowns/index').default}
component={DropdownsPrototype}
/>,
<DocsLayout
exact
key="/prototype-popups"
path="/prototype-popups"
component={require('./prototypes/popups/index').default}
component={PopupsPrototype}
/>,
<DocsLayout
exact
key="/icon-viewer"
path="/icon-viewer"
component={require('./prototypes/IconViewer/index').default}
component={IconViewerPrototype}
/>,
<DocsLayout
exact
key="/menu-button"
path="/menu-button"
component={require('./prototypes/MenuButton/index').default}
component={MenuButtonPrototype}
/>,
<DocsLayout
exact
key="/prototype-alerts"
path="/prototype-alerts"
component={require('./prototypes/alerts/index').default}
component={AlertsPrototype}
/>,
]}
<DocsLayout exact path="/faq" component={FAQ} />
Expand Down
19 changes: 18 additions & 1 deletion docs/src/utils/exampleContexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,25 @@ export const examplePlaygroundContext = require.context(
/**
* The Webpack Context for doc site example sources.
*/
export const exampleSourcesContext = require.context(
// It's required for a hot reload
// eslint-disable-next-line import/no-mutable-exports
export let exampleSourcesContext = require.context(
'docs/src/exampleSources/',
true,
/.source.json$/,
)

// ----------------------------------------
// HMR
// ----------------------------------------

if (__DEV__) {
// When the application source code changes, re-render the whole thing.
if (module.hot) {
// We need this to catch cases unhandled by RHL
// https://github.com/webpack/webpack/issues/834#issuecomment-76590576
module.hot.accept(exampleSourcesContext.id, () => {
exampleSourcesContext = require.context('docs/src/exampleSources/', true, /.source.json$/)
})
Copy link
Member Author

Choose a reason for hiding this comment

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

Context should be updated separately.

}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"react-markdown": "^4.0.8",
"react-router": "^4.1.2",
"react-router-dom": "^4.1.2",
"react-source-render": "2.0.0-beta.6",
"react-source-render": "3.0.0-3",
"react-test-renderer": "^16.8.5",
"react-vis": "^1.11.6",
"request-promise-native": "^1.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/internal-tooling/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
'@babel/plugin-transform-runtime',
],
env: {
delelopment: {
development: {
plugins: ['react-hot-loader/babel'],
},
test: {
Expand Down
Loading