forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSite.jsx
More file actions
396 lines (360 loc) · 11.7 KB
/
Site.jsx
File metadata and controls
396 lines (360 loc) · 11.7 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Import External Dependencies
import PropTypes from "prop-types";
import { Fragment, useEffect, useState } from "react";
import { Helmet } from "react-helmet-async";
import {
Outlet,
Route,
Routes,
useLocation,
useNavigate,
} from "react-router-dom";
// Import Utilities
/* eslint import/no-unresolved: ["error", { ignore: ["_content\.json$"] }] */
import Content from "../../_content.json";
import OgImage from "../../assets/icon-pwa-512x512.png";
import Logo from "../../assets/logo-on-white-bg.svg";
import Favicon from "../../favicon.ico";
import {
extractPages,
extractSections,
getPageDescription,
getPageTitle,
} from "../../utilities/content-utils.mjs";
import getAdjacentPages from "../../utilities/get-adjacent-pages/index.mjs";
import isClient from "../../utilities/is-client.js";
// Import Components
import Container from "../Container/Container.jsx";
import Footer from "../Footer/Footer.jsx";
import Navigation from "../Navigation/Navigation.jsx";
import OfflineBanner from "../OfflineBanner/OfflineBanner.jsx";
import Page from "../Page/Page.jsx";
import PageNotFound from "../PageNotFound/PageNotFound.jsx";
import ReadingProgress from "../ReadingProgress/ReadingProgress.jsx";
import ScrollToTop from "../ScrollToTop/ScrollToTop.jsx";
import Sidebar from "../Sidebar/Sidebar.jsx";
import SidebarMobile from "../SidebarMobile/SidebarMobile.jsx";
import Splash from "../Splash/Splash.jsx";
import Sponsors from "../Sponsors/Sponsors.jsx";
// Load Styling
import "../../styles/index.css";
// Load Content Tree
import clientSideRedirections from "./clientSideRedirections.js";
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) => {
const 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,
date,
teaser,
}) => ({
title: title || name,
content: title || name,
url,
group,
sort,
anchors,
date,
teaser,
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 &&
process.env.NODE_ENV === "production" && // only register sw.js in production
"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(
"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();
});
}
}, []);
useEffect(() => {
if (process.env.NODE_ENV === "production") {
const GA4_ID = "G-KGQCZQ8B8H";
if (!window.gtag) {
const script = document.createElement("script");
script.async = true;
script.src = `https://www.googletagmanager.com/gtag/js?id=${GA4_ID}`;
document.head.appendChild(script);
window.dataLayer ||= [];
window.gtag = function gtag() {
// eslint-disable-next-line prefer-rest-params
window.dataLayer.push(arguments);
};
window.gtag("js", new Date());
window.gtag("config", GA4_ID, {
// eslint-disable-next-line camelcase
send_page_view: false,
});
}
window.gtag("event", "page_view", {
// eslint-disable-next-line camelcase
page_path: location.pathname + location.search,
// eslint-disable-next-line camelcase
page_title: document.title,
});
}
}, [location.pathname, location.search]);
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
const sections = extractSections(Content);
const section = sections.find(({ url }) => location.pathname.startsWith(url));
const 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="flex flex-col min-h-screen bg-white dark:bg-gray-900">
<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,
)}`}
/>
<link
rel="alternate"
type="application/rss+xml"
title="webpack Blog"
href="https://webpack.js.org/feed.xml"
/>
<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="z-[100] fixed w-full">
<OfflineBanner />
<Navigation
pathname={location.pathname}
hash={location.hash}
toggleSidebar={_toggleSidebar}
links={[
{
content: "Documentation",
ariaLabel: "webpack documentation",
url: "/concepts/",
isActive: (_, location) =>
/^\/(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" },
]}
/>
{location.pathname !== "/" && <ReadingProgress />}
</div>
{mounted ? (
<SidebarMobile
isOpen={mobileSidebarOpen}
sections={_strip(Content.children)}
toggle={_toggleSidebar}
/>
) : null}
<Routes>
<Route index element={<Splash />} />
<Route
element={
<div className="flex-[1_1_auto] relative mt-[110px] print:mt-0">
<Container className="flex">
<Outlet />
</Container>
</div>
}
>
<Route path="app-shell" element={<Fragment />} />
{pages.map((page) => {
const 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}
loadContent={props.loadContent}
path={path}
/>
}
/>
);
})}
<Route path="*" element={<PageNotFound />} />
</Route>
</Routes>
<ScrollToTop />
<Footer />
</div>
);
}
Site.propTypes = {
loadContent: PropTypes.func,
};
export default Site;
function PageElement(props) {
const { currentPage, sidebarPages, page, previous, next } = props;
const content = props.loadContent(props.path);
return (
<Fragment>
<Sponsors />
<Sidebar
className="flex-[0_0_280px]"
currentPage={currentPage}
pages={sidebarPages}
/>
<Page
{...page}
content={content}
previous={previous}
next={next}
pages={sidebarPages}
/>
</Fragment>
);
}
PageElement.propTypes = {
currentPage: PropTypes.string,
sidebarPages: PropTypes.array,
pages: PropTypes.array,
previous: PropTypes.object,
next: PropTypes.object,
page: PropTypes.object,
loadContent: PropTypes.func,
path: PropTypes.string,
};