-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathSite.jsx
More file actions
325 lines (299 loc) · 9.84 KB
/
Site.jsx
File metadata and controls
325 lines (299 loc) · 9.84 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Import External Dependencies
import { Fragment, useEffect, useState } from 'react';
import {
Routes,
Route,
useLocation,
useNavigate,
Outlet,
} from 'react-router-dom';
import PropTypes from 'prop-types';
// Import Utilities
import {
extractPages,
extractSections,
getPageDescription,
getPageTitle,
} from '../../utilities/content-utils.mjs';
import isClient from '../../utilities/is-client';
import getAdjacentPages from '../../utilities/get-adjacent-pages/index.mjs';
// Import Components
import NotificationBar from '../NotificationBar/NotificationBar';
import Navigation from '../Navigation/Navigation';
import SidebarMobile from '../SidebarMobile/SidebarMobile';
import Container from '../Container/Container';
import Splash from '../Splash/Splash';
import Sponsors from '../Sponsors/Sponsors';
import Sidebar from '../Sidebar/Sidebar';
import Footer from '../Footer/Footer';
import Page from '../Page/Page';
import PageNotFound from '../PageNotFound/PageNotFound';
import { Helmet } from 'react-helmet-async';
import Favicon from '../../favicon.ico';
import Logo from '../../assets/logo-on-white-bg.svg';
import OgImage from '../../assets/icon-pwa-512x512.png';
// Load Styling
import '../../styles/index';
import './Site.scss';
// Load Content Tree
import Content from '../../_content.json';
import clientSideRedirections from './clientSideRedirections';
Site.propTypes = {
import: PropTypes.func,
};
function Site(props) {
const location = useLocation();
const navigate = useNavigate();
const [mobileSidebarOpen, setMobileSidebarOpen] = useState(false);
/**
* Toggle the mobile sidebar
*
*/
const _toggleSidebar = () => {
setMobileSidebarOpen(!mobileSidebarOpen);
};
/**
* Strip any non-applicable properties
*
* @param {array} array - ...
* @return {array} - ...
*/
const _strip = (array) => {
let anchorTitleIndex = array.findIndex(
(item) => item.name.toLowerCase() === 'index.mdx'
);
if (anchorTitleIndex !== -1) {
array.unshift(array[anchorTitleIndex]);
array.splice(anchorTitleIndex + 1, 1);
}
return array
.map(({ title, name, url, group, sort, anchors, children }) => ({
title: title || name,
content: title || name,
url,
group,
sort,
anchors,
children: children ? _strip(children) : [],
}))
.filter(
(page) =>
page.title !== 'printable.mdx' && !page.content.includes('Printable')
);
};
useEffect(() => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.addEventListener('controllerchange', () => {
window.location.reload();
});
}
}, []);
useEffect(() => {
if (isClient) {
if (process.env.NODE_ENV === 'production') {
// only register sw.js in production
if ('serviceWorker' in navigator) {
// dynamic load sw
import('workbox-window/Workbox.mjs').then(({ Workbox }) => {
const wb = new Workbox('/sw.js');
// listen to `waiting` event
wb.addEventListener('waiting', () => {
// log and show updateBox
console.log(
// eslint-disable-next-line
"A new service worker has installed, but it can't activate until all tabs running the current version have been unloaded"
);
});
// register the service worker
wb.register();
});
}
}
}
}, []);
let sections = extractSections(Content);
let section = sections.find(({ url }) => location.pathname.startsWith(url));
let pages = extractPages(Content);
const sidebarPages = _strip(
section
? section.children
: Content.children.filter(
(item) => item.type !== 'directory' && item.url !== '/'
)
);
// not to show in sub navbar
const excludeItems = ['contribute', 'blog'];
const title = getPageTitle(Content, location.pathname);
const description =
getPageDescription(Content, location.pathname) ||
'webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.';
function isPrintPage(url) {
return url.includes('/printable');
}
// As github pages uses trailing slash, we need to provide it to canonicals for consistency
// between canonical href and final url served by github pages.
function enforceTrailingSlash(url) {
return url.replace(/\/?$/, '/');
}
// Enable client side redirection
// See https://github.com/webpack/webpack.js.org/pull/5146#discussion_r663510210
useEffect(() => {
const target = clientSideRedirections(location);
if (target) {
navigate(target, { replace: true });
}
}, [location, navigate]);
return (
<div className="site">
<Helmet>
<html lang="en" />
<meta charset="utf-8" />
<meta name="theme-color" content="#2B3A42" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{isPrintPage(location.pathname) ? (
<meta name="robots" content="noindex,nofollow" />
) : null}
<title>{title}</title>
<meta name="description" content={description} />
<meta property="og:site_name" content="webpack" />
<meta property="og:type" content="website" />
<meta property="og:title" content={title} />
<meta
property="og:description"
name="description"
content={description}
/>
<meta
property="og:image"
content={`https://webpack.js.org${OgImage}`}
/>
<meta property="twitter:card" content="summary" />
<meta property="twitter:site" content="@webpack" />
<meta property="twitter:creator" content="@webpack" />
<meta property="twitter:domain" content="https://webpack.js.org/" />
<link rel="icon" type="image/x-icon" href={Favicon} />
{process.env.NODE_ENV === 'production' && (
<link rel="manifest" href="/manifest.json" />
)}
<link
rel="canonical"
href={`https://webpack.js.org${enforceTrailingSlash(
location.pathname
)}`}
/>
<meta name="mobile-web-app-capable" content="yes" />
<link rel="icon" sizes="192x192" href="/icon_192x192.png" />
<link rel="icon" sizes="512x512" href="/icon_512x512.png" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="apple-mobile-web-app-title" content="webpack" />
<link rel="apple-touch-icon" href="/icon_180x180.png" />
<link rel="mask-icon" href={Logo} color="#465e69" />
<meta name="msapplication-TileImage" content="/icon_150x150.png" />
<meta name="msapplication-TileColor" content="#465e69" />
</Helmet>
<div className="site__header">
<Navigation
pathname={location.pathname}
hash={location.hash}
toggleSidebar={_toggleSidebar}
links={[
{
content: 'Documentation',
ariaLabel: 'webpack documentation',
url: '/concepts/',
isactive: (_, location) => {
return /^\/(api|concepts|configuration|guides|loaders|migrate|plugins)/.test(
location.pathname
);
},
children: _strip(
sections.filter(
({ name }) => excludeItems.includes(name) === false
)
),
},
{
content: 'Contribute',
url: '/contribute/',
ariaLabel: 'contribute to webpack',
},
{ content: 'Blog', url: '/blog/', ariaLabel: 'webpack blog' },
]}
/>
</div>
{isClient ? (
<SidebarMobile
isOpen={mobileSidebarOpen}
sections={_strip(Content.children)}
toggle={_toggleSidebar}
/>
) : null}
<Routes>
<Route index element={<Splash />} />
<Route
element={
<Container className="site__content">
<Outlet />
</Container>
}
>
<Route path="app-shell" element={<Fragment />} />
{pages.map((page) => {
let path = page.path.replace('src/content/', '');
const { previous, next } = getAdjacentPages(
sidebarPages,
page,
'url'
);
return (
<Route
key={page.url}
path={page.url}
element={
<PageElement
currentPage={location.pathname}
sidebarPages={sidebarPages}
page={page}
next={next}
previous={previous}
import={props.import}
path={path}
/>
}
/>
);
})}
<Route path="*" element={<PageNotFound />} />
</Route>
</Routes>
<Footer />
<NotificationBar />
</div>
);
}
export default Site;
PageElement.propTypes = {
currentPage: PropTypes.string,
sidebarPages: PropTypes.array,
previous: PropTypes.object,
next: PropTypes.object,
page: PropTypes.object,
import: PropTypes.func,
path: PropTypes.string,
};
function PageElement(props) {
const { currentPage, sidebarPages, page, previous, next } = props;
const content = props.import(props.path);
return (
<Fragment>
<Sponsors />
<Sidebar
className="site__sidebar"
currentPage={currentPage}
pages={sidebarPages}
/>
<Page {...page} content={content} previous={previous} next={next} />
</Fragment>
);
}