-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathentry.js
196 lines (176 loc) · 5 KB
/
entry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Main application
import path from 'path'
import React from 'react'
import { render, hydrate } from 'react-dom'
import {
StaticRouter,
BrowserRouter,
Switch,
Route,
Link,
withRouter
} from 'react-router-dom'
import { Provider as RebassProvider } from 'rebass'
import minimatch from 'minimatch'
import sortBy from 'lodash.sortby'
import ScopeProvider from './ScopeProvider'
import Catch from './Catch'
import FileList from './FileList'
import ScrollTop from './ScrollTop'
import CenteredLayout from './CenteredLayout'
const IS_CLIENT = typeof document !== 'undefined'
const req = require.context(DIRNAME, true, /\.(js|md|mdx|jsx)$/)
const { filename, basename = '', disableScroll } = OPTIONS
const getComponents = req => req.keys()
.filter(minimatch.filter('!node_modules'))
.filter(key => !MATCH || minimatch(key.replace(/^\.\//, ''), MATCH))
.filter(key => !/^_/.test(path.basename(key)))
.map(key => ({
key,
name: path.basename(key, path.extname(key)),
module: req(key),
Component: req(key).default || req(key),
}))
.filter(component => typeof component.Component === 'function')
const initialComponents = getComponents(req)
const DefaultApp = props => props.children
const Router = IS_CLIENT ? BrowserRouter : StaticRouter
const appPath = req.keys().find(key => key === './_app.js')
const App = appPath ? (req(appPath).default || req(appPath)) : DefaultApp
export const getRoutes = async (components = initialComponents) => {
const promises = await components.map(async ({
key,
name,
module,
Component
}) => {
const exact = name === 'index'
const dirname = path.dirname(key).replace(/^\./, '')
const extname = path.extname(key)
let pathname = dirname + (exact ? '/' : '/' + name)
const href = pathname
const initialProps = Component.getInitialProps
? await Component.getInitialProps({ path: pathname })
: {}
const defaultProps = Component.defaultProps
const meta = module.frontMatter || {}
const props = { ...meta, ...initialProps, ...defaultProps }
// for dynamic routing
pathname = props.path || pathname
if (dirname && name === 'index') {
name = path.basename(dirname)
}
return {
key,
name,
extname,
href,
path: pathname,
dirname,
exact,
module,
Component,
props
}
})
const routes = await Promise.all(promises)
const filtered = routes
.filter(r => !r.props.ignore)
.filter(component => !/404/.test(component.name))
let sorted = [...filtered]
sorted = sortBy([...sorted], a => a.name)
sorted = sortBy([...sorted], a => !a.exact)
sorted = sortBy([...sorted], a => a.dirname)
sorted.notfound = routes.find(component => /404/.test(component.name))
return sorted
}
const RouterState = withRouter(({ render, ...props }) => {
const { pathname } = props.location
const route = props.routes.find(r => r.path === pathname || r.href === pathname) || { props: {} }
return render({ ...props, route })
})
export default class Root extends React.Component {
static defaultProps = {
path: '/',
basename
}
state = {
...this.props,
...App.defaultProps
}
render () {
const {
routes,
basename,
path = '/'
} = this.props
const NotFound = routes.notfound
? routes.notfound.Component
: FileList
const render = appProps => (
<Switch>
{routes.map(({ Component, ...route }) => (
<Route
{...route}
render={props => (
<Catch>
<CenteredLayout
active={!appPath && /md/.test(route.extname)}>
<Component
{...props}
{...appProps}
{...route.props}
/>
</CenteredLayout>
</Catch>
)}
/>
))}
<Route
render={props => <NotFound {...props} routes={routes} />}
/>
</Switch>
)
return (
<Router
context={{}}
basename={basename}
location={path}>
<React.Fragment>
<RebassProvider>
<ScopeProvider>
<Catch>
<RouterState
routes={routes}
render={(router) => (
<App
{...router}
routes={routes}
render={render}
Component={render}
children={render(router)}
/>
)}
/>
</Catch>
</ScopeProvider>
</RebassProvider>
{!disableScroll && <ScrollTop />}
</React.Fragment>
</Router>
)
}
}
if (IS_CLIENT) {
const mount = DEV ? render : hydrate
const div = window.root || document.body.appendChild(
document.createElement('div')
)
getRoutes()
.then(routes => {
mount(<Root routes={routes} />, div)
})
}
if (IS_CLIENT && module.hot) {
module.hot.accept()
}