@docusaurus/core 2.0.0-beta.13 → 2.0.0-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/beforeCli.js +14 -15
- package/bin/docusaurus.js +7 -5
- package/lib/babel/preset.d.ts +1 -1
- package/lib/choosePort.js +8 -8
- package/lib/client/LinksCollector.d.ts +1 -1
- package/lib/client/PendingNavigation.d.ts +2 -2
- package/lib/client/exports/BrowserOnly.d.ts +1 -1
- package/lib/client/exports/BrowserOnly.js +7 -2
- package/lib/client/exports/ErrorBoundary.d.ts +1 -1
- package/lib/client/exports/ErrorBoundary.js +3 -1
- package/lib/client/exports/Interpolate.d.ts +1 -1
- package/lib/client/exports/Translate.d.ts +2 -2
- package/lib/client/exports/browserContext.d.ts +1 -1
- package/lib/client/exports/docusaurusContext.d.ts +2 -2
- package/lib/client/exports/useBaseUrl.js +2 -2
- package/lib/client/exports/useDocusaurusContext.d.ts +1 -1
- package/lib/client/nprogress.css +2 -2
- package/lib/client/preload.d.ts +1 -1
- package/lib/client/serverEntry.d.ts +10 -0
- package/lib/client/serverEntry.js +86 -126
- package/lib/commands/build.d.ts +1 -1
- package/lib/commands/build.js +17 -15
- package/lib/commands/clear.js +10 -11
- package/lib/commands/commandUtils.d.ts +1 -1
- package/lib/commands/deploy.d.ts +1 -1
- package/lib/commands/deploy.js +12 -14
- package/lib/commands/external.d.ts +1 -1
- package/lib/commands/external.js +1 -1
- package/lib/commands/serve.d.ts +1 -1
- package/lib/commands/serve.js +2 -8
- package/lib/commands/start.d.ts +1 -1
- package/lib/commands/start.js +23 -22
- package/lib/commands/swizzle.d.ts +1 -1
- package/lib/commands/swizzle.js +26 -35
- package/lib/commands/writeHeadingIds.d.ts +1 -1
- package/lib/commands/writeHeadingIds.js +4 -5
- package/lib/commands/writeTranslations.d.ts +2 -2
- package/lib/commands/writeTranslations.js +1 -1
- package/lib/server/brokenLinks.d.ts +1 -1
- package/lib/server/brokenLinks.js +1 -2
- package/lib/server/client-modules/index.d.ts +1 -1
- package/lib/server/config.d.ts +2 -2
- package/lib/server/config.js +5 -2
- package/lib/server/configValidation.d.ts +1 -1
- package/lib/server/configValidation.js +5 -4
- package/lib/server/duplicateRoutes.d.ts +1 -1
- package/lib/server/html-tags/index.d.ts +1 -1
- package/lib/server/i18n.d.ts +1 -1
- package/lib/server/i18n.js +6 -14
- package/lib/server/index.d.ts +1 -1
- package/lib/server/index.js +26 -15
- package/lib/server/loadSetup.d.ts +1 -1
- package/lib/server/moduleShorthand.js +11 -7
- package/lib/server/plugins/applyRouteTrailingSlash.d.ts +2 -2
- package/lib/server/plugins/index.d.ts +1 -1
- package/lib/server/plugins/index.js +3 -3
- package/lib/server/plugins/init.d.ts +2 -2
- package/lib/server/plugins/init.js +10 -9
- package/lib/server/plugins/pluginIds.d.ts +1 -1
- package/lib/server/presets/index.d.ts +1 -1
- package/lib/server/presets/index.js +1 -1
- package/lib/server/routes.d.ts +1 -1
- package/lib/server/routes.js +1 -2
- package/lib/server/themes/alias.d.ts +1 -1
- package/lib/server/themes/index.d.ts +1 -1
- package/lib/server/translations/translations.d.ts +1 -1
- package/lib/server/translations/translations.js +7 -12
- package/lib/server/translations/translationsExtractor.d.ts +2 -2
- package/lib/server/translations/translationsExtractor.js +123 -92
- package/lib/server/utils.d.ts +1 -1
- package/lib/server/versions/__tests/index.test.js +6 -5
- package/lib/server/versions/index.d.ts +1 -1
- package/lib/webpack/base.d.ts +2 -2
- package/lib/webpack/client.d.ts +2 -2
- package/lib/webpack/client.js +2 -2
- package/lib/webpack/plugins/ChunkAssetPlugin.d.ts +1 -1
- package/lib/webpack/plugins/CleanWebpackPlugin.d.ts +1 -1
- package/lib/webpack/plugins/LogPlugin.d.ts +1 -1
- package/lib/webpack/plugins/WaitPlugin.d.ts +1 -1
- package/lib/webpack/server.d.ts +2 -2
- package/lib/{client → webpack}/templates/index.html.template.ejs +0 -0
- package/lib/webpack/templates/ssr.html.template.d.ts +8 -0
- package/lib/{client → webpack}/templates/ssr.html.template.js +3 -2
- package/lib/webpack/utils.d.ts +4 -4
- package/lib/webpack/utils.js +19 -14
- package/package.json +22 -22
- package/lib/.tsbuildinfo +0 -1
- package/lib/client/.tsbuildinfo +0 -1
- package/tsconfig.client.json +0 -12
- package/tsconfig.json +0 -14
package/bin/beforeCli.js
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// @ts-check
|
|
9
|
+
|
|
10
|
+
const logger = require('@docusaurus/logger').default;
|
|
9
11
|
const fs = require('fs-extra');
|
|
10
12
|
const semver = require('semver');
|
|
11
13
|
const path = require('path');
|
|
@@ -53,7 +55,7 @@ try {
|
|
|
53
55
|
}
|
|
54
56
|
} catch (e) {
|
|
55
57
|
// Do not stop cli if this fails, see https://github.com/facebook/docusaurus/issues/5400
|
|
56
|
-
|
|
58
|
+
logger.error(e);
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
// We don't want to display update message for canary releases
|
|
@@ -74,6 +76,7 @@ if (
|
|
|
74
76
|
notifier.config.set('update', notifier.update);
|
|
75
77
|
|
|
76
78
|
if (ignoreUpdate(notifier.update)) {
|
|
79
|
+
// @ts-expect-error: it works
|
|
77
80
|
return;
|
|
78
81
|
}
|
|
79
82
|
|
|
@@ -91,6 +94,7 @@ if (
|
|
|
91
94
|
? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
|
|
92
95
|
: `npm i ${siteDocusaurusPackagesForUpdate}`;
|
|
93
96
|
|
|
97
|
+
/** @type {import('boxen').Options} */
|
|
94
98
|
const boxenOptions = {
|
|
95
99
|
padding: 1,
|
|
96
100
|
margin: 1,
|
|
@@ -100,13 +104,12 @@ if (
|
|
|
100
104
|
};
|
|
101
105
|
|
|
102
106
|
const docusaurusUpdateMessage = boxen(
|
|
103
|
-
`Update available ${
|
|
104
|
-
|
|
105
|
-
)}${
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
)}`,
|
|
107
|
+
`Update available ${logger.dim(
|
|
108
|
+
`${notifier.update.current}`,
|
|
109
|
+
)} → ${logger.green(`${notifier.update.latest}`)}
|
|
110
|
+
|
|
111
|
+
To upgrade Docusaurus packages with the latest version, run the following command:
|
|
112
|
+
${logger.code(upgradeCommand)}`,
|
|
110
113
|
boxenOptions,
|
|
111
114
|
);
|
|
112
115
|
|
|
@@ -115,11 +118,7 @@ if (
|
|
|
115
118
|
|
|
116
119
|
// notify user if node version needs to be updated
|
|
117
120
|
if (!semver.satisfies(process.version, requiredVersion)) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
chalk.yellow(
|
|
121
|
-
`\n\nYou are using Node ${process.version}. We require Node ${requiredVersion} or up!\n`,
|
|
122
|
-
),
|
|
123
|
-
);
|
|
121
|
+
logger.error('Minimum Node.js version not met :(');
|
|
122
|
+
logger.info`You are using Node.js number=${process.version}, Requirement: Node.js number=${requiredVersion}.`;
|
|
124
123
|
process.exit(1);
|
|
125
124
|
}
|
package/bin/docusaurus.js
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
// @ts-check
|
|
10
|
+
|
|
11
|
+
const logger = require('@docusaurus/logger').default;
|
|
10
12
|
const fs = require('fs');
|
|
11
13
|
const cli = require('commander');
|
|
12
14
|
const {
|
|
@@ -32,7 +34,7 @@ cli
|
|
|
32
34
|
.description('Build website.')
|
|
33
35
|
.option(
|
|
34
36
|
'--bundle-analyzer',
|
|
35
|
-
'visualize size of webpack output files with an interactive zoomable
|
|
37
|
+
'visualize size of webpack output files with an interactive zoomable tree map (default: false)',
|
|
36
38
|
)
|
|
37
39
|
.option(
|
|
38
40
|
'--out-dir <dir>',
|
|
@@ -219,8 +221,7 @@ cli
|
|
|
219
221
|
|
|
220
222
|
cli.arguments('<command>').action((cmd) => {
|
|
221
223
|
cli.outputHelp();
|
|
222
|
-
|
|
223
|
-
console.log();
|
|
224
|
+
logger.error` Unknown command name=${cmd}.`;
|
|
224
225
|
});
|
|
225
226
|
|
|
226
227
|
function isInternalCommand(command) {
|
|
@@ -238,6 +239,7 @@ function isInternalCommand(command) {
|
|
|
238
239
|
|
|
239
240
|
async function run() {
|
|
240
241
|
if (!isInternalCommand(process.argv.slice(2)[0])) {
|
|
242
|
+
// @ts-expect-error: Hmmm
|
|
241
243
|
await externalCommand(cli, resolveDir('.'));
|
|
242
244
|
}
|
|
243
245
|
|
|
@@ -251,6 +253,6 @@ async function run() {
|
|
|
251
253
|
run();
|
|
252
254
|
|
|
253
255
|
process.on('unhandledRejection', (err) => {
|
|
254
|
-
|
|
256
|
+
logger.error(err.stack);
|
|
255
257
|
process.exit(1);
|
|
256
258
|
});
|
package/lib/babel/preset.d.ts
CHANGED
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { ConfigAPI, TransformOptions } from '@babel/core';
|
|
7
|
+
import type { ConfigAPI, TransformOptions } from '@babel/core';
|
|
8
8
|
declare function babelPresets(api: ConfigAPI): TransformOptions;
|
|
9
9
|
export default babelPresets;
|
package/lib/choosePort.js
CHANGED
|
@@ -14,7 +14,7 @@ const tslib_1 = require("tslib");
|
|
|
14
14
|
const child_process_1 = require("child_process");
|
|
15
15
|
const detect_port_1 = (0, tslib_1.__importDefault)(require("detect-port"));
|
|
16
16
|
const is_root_1 = (0, tslib_1.__importDefault)(require("is-root"));
|
|
17
|
-
const
|
|
17
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
18
18
|
const prompts_1 = (0, tslib_1.__importDefault)(require("prompts"));
|
|
19
19
|
const isInteractive = process.stdout.isTTY;
|
|
20
20
|
const execOptions = {
|
|
@@ -53,10 +53,7 @@ function getProcessForPort(port) {
|
|
|
53
53
|
const processId = getProcessIdOnPort(port);
|
|
54
54
|
const directory = getDirectoryOfProcessById(processId);
|
|
55
55
|
const command = getProcessCommand(processId);
|
|
56
|
-
return
|
|
57
|
-
chalk_1.default.grey(` (pid ${processId})\n`) +
|
|
58
|
-
chalk_1.default.blue(' in ') +
|
|
59
|
-
chalk_1.default.cyan(directory));
|
|
56
|
+
return logger_1.default.interpolate `code=${command} subdue=${`(pid ${processId})`} in path=${directory}`;
|
|
60
57
|
}
|
|
61
58
|
catch (e) {
|
|
62
59
|
return null;
|
|
@@ -81,7 +78,9 @@ async function choosePort(host, defaultPort) {
|
|
|
81
78
|
const question = {
|
|
82
79
|
type: 'confirm',
|
|
83
80
|
name: 'shouldChangePort',
|
|
84
|
-
message:
|
|
81
|
+
message: logger_1.default.yellow(`${logger_1.default.bold('[WARNING]')} ${message}${existingProcess ? ` Probably:\n ${existingProcess}` : ''}
|
|
82
|
+
|
|
83
|
+
Would you like to run the app on another port instead?`),
|
|
85
84
|
initial: true,
|
|
86
85
|
};
|
|
87
86
|
(0, prompts_1.default)(question).then((answer) => {
|
|
@@ -94,11 +93,12 @@ async function choosePort(host, defaultPort) {
|
|
|
94
93
|
});
|
|
95
94
|
}
|
|
96
95
|
else {
|
|
97
|
-
|
|
96
|
+
logger_1.default.error(message);
|
|
98
97
|
resolve(null);
|
|
99
98
|
}
|
|
100
99
|
}), (err) => {
|
|
101
|
-
throw new Error(
|
|
100
|
+
throw new Error(`Could not find an open port at ${host}.
|
|
101
|
+
${`Network error message: "${err.message || err}".`}`);
|
|
102
102
|
});
|
|
103
103
|
}
|
|
104
104
|
exports.default = choosePort;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { ReactNode } from 'react';
|
|
7
|
+
import { type ReactNode } from 'react';
|
|
8
8
|
declare type LinksCollector = {
|
|
9
9
|
collectLink: (link: string) => void;
|
|
10
10
|
};
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
/// <reference types="node" />
|
|
8
8
|
import React from 'react';
|
|
9
|
-
import { RouteComponentProps } from 'react-router-dom';
|
|
10
|
-
import { RouteConfig } from 'react-router-config';
|
|
9
|
+
import { type RouteComponentProps } from 'react-router-dom';
|
|
10
|
+
import type { RouteConfig } from 'react-router-config';
|
|
11
11
|
import type { Location } from '@docusaurus/history';
|
|
12
12
|
import './nprogress.css';
|
|
13
13
|
interface Props extends RouteComponentProps {
|
|
@@ -4,13 +4,18 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import React from 'react';
|
|
7
|
+
import React, { isValidElement } from 'react';
|
|
8
8
|
import useIsBrowser from '@docusaurus/useIsBrowser';
|
|
9
9
|
// Similar comp to the one described here:
|
|
10
10
|
// https://www.joshwcomeau.com/react/the-perils-of-rehydration/#abstractions
|
|
11
11
|
function BrowserOnly({ children, fallback, }) {
|
|
12
12
|
const isBrowser = useIsBrowser();
|
|
13
|
-
if (isBrowser
|
|
13
|
+
if (isBrowser) {
|
|
14
|
+
if (typeof children !== 'function' &&
|
|
15
|
+
process.env.NODE_ENV === 'development') {
|
|
16
|
+
throw new Error(`Docusaurus error: The children of <BrowserOnly> must be a "render function", e.g. <BrowserOnly>{() => <span>{window.location.href}</span>}</BrowserOnly>.
|
|
17
|
+
Current type: ${isValidElement(children) ? 'React element' : typeof children}`);
|
|
18
|
+
}
|
|
14
19
|
return React.createElement(React.Fragment, null, children());
|
|
15
20
|
}
|
|
16
21
|
return fallback || null;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
/// <reference types="@docusaurus/module-type-aliases" />
|
|
8
|
-
import React, { ReactNode } from 'react';
|
|
8
|
+
import React, { type ReactNode } from 'react';
|
|
9
9
|
import type { Props } from '@docusaurus/ErrorBoundary';
|
|
10
10
|
interface State {
|
|
11
11
|
error: Error | null;
|
|
@@ -29,7 +29,9 @@ class ErrorBoundary extends React.Component {
|
|
|
29
29
|
tryAgain: () => this.setState({ error: null }),
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
|
-
return children
|
|
32
|
+
return (children !== null && children !== void 0 ? children :
|
|
33
|
+
// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
|
|
34
|
+
null);
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
export default ErrorBoundary;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
/// <reference types="@docusaurus/module-type-aliases" />
|
|
8
|
-
import { ReactNode } from 'react';
|
|
8
|
+
import { type ReactNode } from 'react';
|
|
9
9
|
import type { InterpolateProps, InterpolateValues } from '@docusaurus/Interpolate';
|
|
10
10
|
export declare function interpolate<Str extends string>(text: Str, values?: InterpolateValues<Str, string | number>): string;
|
|
11
11
|
export declare function interpolate<Str extends string, Value extends ReactNode>(text: Str, values?: InterpolateValues<Str, Value>): ReactNode;
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
/// <reference types="@docusaurus/module-type-aliases" />
|
|
8
|
-
import { ReactNode } from 'react';
|
|
9
|
-
import { InterpolateValues } from '@docusaurus/Interpolate';
|
|
8
|
+
import type { ReactNode } from 'react';
|
|
9
|
+
import { type InterpolateValues } from '@docusaurus/Interpolate';
|
|
10
10
|
import type { TranslateParam, TranslateProps } from '@docusaurus/Translate';
|
|
11
11
|
export declare function translate<Str extends string>({ message, id }: TranslateParam<Str>, values?: InterpolateValues<Str, string | number>): string;
|
|
12
12
|
export default function Translate<Str extends string>({ children, id, values, }: TranslateProps<Str>): ReactNode;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import React, { ReactNode } from 'react';
|
|
7
|
+
import React, { type ReactNode } from 'react';
|
|
8
8
|
export declare const Context: React.Context<boolean>;
|
|
9
9
|
export declare function BrowserContextProvider({ children, }: {
|
|
10
10
|
children: ReactNode;
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import React, { ReactNode } from 'react';
|
|
8
|
-
import { DocusaurusContext } from '@docusaurus/types';
|
|
7
|
+
import React, { type ReactNode } from 'react';
|
|
8
|
+
import type { DocusaurusContext } from '@docusaurus/types';
|
|
9
9
|
export declare const Context: React.Context<DocusaurusContext>;
|
|
10
10
|
export declare function DocusaurusContextProvider({ children, }: {
|
|
11
11
|
children: ReactNode;
|
|
@@ -19,7 +19,7 @@ function addBaseUrl(siteUrl, baseUrl, url, { forcePrependBaseUrl = false, absolu
|
|
|
19
19
|
return url;
|
|
20
20
|
}
|
|
21
21
|
if (forcePrependBaseUrl) {
|
|
22
|
-
return baseUrl + url;
|
|
22
|
+
return baseUrl + url.replace(/^\//, '');
|
|
23
23
|
}
|
|
24
24
|
// We should avoid adding the baseurl twice if it's already there
|
|
25
25
|
const shouldAddBaseUrl = !url.startsWith(baseUrl);
|
|
@@ -27,7 +27,7 @@ function addBaseUrl(siteUrl, baseUrl, url, { forcePrependBaseUrl = false, absolu
|
|
|
27
27
|
return absolute ? siteUrl + basePath : basePath;
|
|
28
28
|
}
|
|
29
29
|
export function useBaseUrlUtils() {
|
|
30
|
-
const { siteConfig: { baseUrl
|
|
30
|
+
const { siteConfig: { baseUrl, url: siteUrl }, } = useDocusaurusContext();
|
|
31
31
|
return {
|
|
32
32
|
withBaseUrl: (url, options) => addBaseUrl(siteUrl, baseUrl, url, options),
|
|
33
33
|
};
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { DocusaurusContext } from '@docusaurus/types';
|
|
7
|
+
import type { DocusaurusContext } from '@docusaurus/types';
|
|
8
8
|
declare function useDocusaurusContext(): DocusaurusContext;
|
|
9
9
|
export default useDocusaurusContext;
|
package/lib/client/nprogress.css
CHANGED
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
|
|
28
28
|
#nprogress .peg {
|
|
29
29
|
position: absolute;
|
|
30
|
-
right:
|
|
30
|
+
right: 0;
|
|
31
31
|
width: 100px;
|
|
32
32
|
height: 100%;
|
|
33
33
|
box-shadow: 0 0 10px #29d, 0 0 5px #29d;
|
|
34
34
|
opacity: 1;
|
|
35
|
-
transform: rotate(3deg) translate(
|
|
35
|
+
transform: rotate(3deg) translate(0, -4px);
|
|
36
36
|
}
|
package/lib/client/preload.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { RouteConfig } from 'react-router-config';
|
|
7
|
+
import { type RouteConfig } from 'react-router-config';
|
|
8
8
|
/**
|
|
9
9
|
* Helper function to make sure all async components for that particular route
|
|
10
10
|
* is preloaded before rendering. This is especially useful to avoid loading screens.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { Locals } from '@slorber/static-site-generator-webpack-plugin';
|
|
8
|
+
export default function render(locals: Locals & {
|
|
9
|
+
path: string;
|
|
10
|
+
}): Promise<string>;
|
|
@@ -4,149 +4,109 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
7
|
import * as eta from 'eta';
|
|
9
8
|
import React from 'react';
|
|
10
|
-
import {StaticRouter} from 'react-router-dom';
|
|
9
|
+
import { StaticRouter } from 'react-router-dom';
|
|
11
10
|
import ReactDOMServer from 'react-dom/server';
|
|
12
|
-
import {Helmet} from 'react-helmet';
|
|
13
|
-
import {getBundles} from 'react-loadable-ssr-addon-v5-slorber';
|
|
11
|
+
import { Helmet } from 'react-helmet';
|
|
12
|
+
import { getBundles } from 'react-loadable-ssr-addon-v5-slorber';
|
|
14
13
|
import Loadable from 'react-loadable';
|
|
15
|
-
|
|
16
|
-
import {minify} from 'html-minifier-terser';
|
|
14
|
+
import { minify } from 'html-minifier-terser';
|
|
17
15
|
import path from 'path';
|
|
18
16
|
import fs from 'fs-extra';
|
|
19
17
|
import routes from '@generated/routes';
|
|
20
|
-
import packageJson from '../../package.json';
|
|
21
18
|
import preload from './preload';
|
|
22
19
|
import App from './App';
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
ProvideLinksCollector,
|
|
26
|
-
} from './LinksCollector';
|
|
27
|
-
import chalk from 'chalk';
|
|
20
|
+
import { createStatefulLinksCollector, ProvideLinksCollector, } from './LinksCollector';
|
|
21
|
+
import logger from '@docusaurus/logger';
|
|
28
22
|
// eslint-disable-next-line no-restricted-imports
|
|
29
|
-
import {memoize} from 'lodash';
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
23
|
+
import { memoize } from 'lodash';
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
25
|
+
const packageJson = require('../../package.json');
|
|
26
|
+
const getCompiledSSRTemplate = memoize((template) => eta.compile(template.trim(), {
|
|
33
27
|
rmWhitespace: true,
|
|
34
|
-
|
|
35
|
-
);
|
|
36
|
-
|
|
28
|
+
}));
|
|
37
29
|
function renderSSRTemplate(ssrTemplate, data) {
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
const compiled = getCompiledSSRTemplate(ssrTemplate);
|
|
31
|
+
return compiled(data, eta.defaultConfig);
|
|
40
32
|
}
|
|
41
|
-
|
|
42
33
|
export default async function render(locals) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
console.error(
|
|
57
|
-
chalk.green(
|
|
58
|
-
'Pro tip: It looks like you are using code that should run on the client-side only.\nTo get around it, try using <BrowserOnly> (https://docusaurus.io/docs/docusaurus-core/#browseronly) or ExecutionEnvironment (https://docusaurus.io/docs/docusaurus-core/#executionenvironment).\nIt might also require to wrap your client code in useEffect hook and/or import a third-party library dynamically (if any).',
|
|
59
|
-
),
|
|
60
|
-
);
|
|
34
|
+
try {
|
|
35
|
+
return await doRender(locals);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
logger.error `Docusaurus Node/SSR could not render static page with path path=${locals.path} because of following error:
|
|
39
|
+
${e.stack}`;
|
|
40
|
+
const isNotDefinedErrorRegex = /(window|document|localStorage|navigator|alert|location|buffer|self) is not defined/i;
|
|
41
|
+
if (isNotDefinedErrorRegex.test(e.message)) {
|
|
42
|
+
logger.info `It looks like you are using code that should run on the client-side only.
|
|
43
|
+
To get around it, try using code=${'<BrowserOnly>'} (path=${'https://docusaurus.io/docs/docusaurus-core/#browseronly'}) or code=${'ExecutionEnvironment'} (path=${'https://docusaurus.io/docs/docusaurus-core/#executionenvironment'}).
|
|
44
|
+
It might also require to wrap your client code in code=${'useEffect'} hook and/or import a third-party library dynamically (if any).`;
|
|
45
|
+
}
|
|
46
|
+
throw new Error('Server-side rendering fails due to the error above.');
|
|
61
47
|
}
|
|
62
|
-
|
|
63
|
-
throw new Error('Server-side rendering fails due to the error above.');
|
|
64
|
-
}
|
|
65
48
|
}
|
|
66
|
-
|
|
67
49
|
// Renderer for static-site-generator-webpack-plugin (async rendering via promises).
|
|
68
50
|
async function doRender(locals) {
|
|
69
|
-
|
|
70
|
-
routesLocation
|
|
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
|
-
// manifest information.
|
|
113
|
-
const modulesToBeLoaded = [...manifest.entrypoints, ...Array.from(modules)];
|
|
114
|
-
const bundles = getBundles(manifest, modulesToBeLoaded);
|
|
115
|
-
const stylesheets = (bundles.css || []).map((b) => b.file);
|
|
116
|
-
const scripts = (bundles.js || []).map((b) => b.file);
|
|
117
|
-
|
|
118
|
-
const renderedHtml = renderSSRTemplate(ssrTemplate, {
|
|
119
|
-
appHtml,
|
|
120
|
-
baseUrl,
|
|
121
|
-
htmlAttributes: htmlAttributes || '',
|
|
122
|
-
bodyAttributes: bodyAttributes || '',
|
|
123
|
-
headTags,
|
|
124
|
-
preBodyTags,
|
|
125
|
-
postBodyTags,
|
|
126
|
-
metaAttributes,
|
|
127
|
-
scripts,
|
|
128
|
-
stylesheets,
|
|
129
|
-
noIndex,
|
|
130
|
-
version: packageJson.version,
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
// Minify html with https://github.com/DanielRuf/html-minifier-terser
|
|
135
|
-
return await minify(renderedHtml, {
|
|
136
|
-
removeComments: false,
|
|
137
|
-
removeRedundantAttributes: true,
|
|
138
|
-
removeEmptyAttributes: true,
|
|
139
|
-
removeScriptTypeAttributes: true,
|
|
140
|
-
removeStyleLinkTypeAttributes: true,
|
|
141
|
-
useShortDoctype: true,
|
|
142
|
-
minifyJS: true,
|
|
51
|
+
const { routesLocation, headTags, preBodyTags, postBodyTags, onLinksCollected, baseUrl, ssrTemplate, noIndex, } = locals;
|
|
52
|
+
const location = routesLocation[locals.path];
|
|
53
|
+
await preload(routes, location);
|
|
54
|
+
const modules = new Set();
|
|
55
|
+
const context = {};
|
|
56
|
+
const linksCollector = createStatefulLinksCollector();
|
|
57
|
+
const appHtml = ReactDOMServer.renderToString(React.createElement(Loadable.Capture, { report: (moduleName) => modules.add(moduleName) },
|
|
58
|
+
React.createElement(StaticRouter, { location: location, context: context },
|
|
59
|
+
React.createElement(ProvideLinksCollector, { linksCollector: linksCollector },
|
|
60
|
+
React.createElement(App, null)))));
|
|
61
|
+
onLinksCollected(location, linksCollector.getCollectedLinks());
|
|
62
|
+
const helmet = Helmet.renderStatic();
|
|
63
|
+
const htmlAttributes = helmet.htmlAttributes.toString();
|
|
64
|
+
const bodyAttributes = helmet.bodyAttributes.toString();
|
|
65
|
+
const metaStrings = [
|
|
66
|
+
helmet.title.toString(),
|
|
67
|
+
helmet.meta.toString(),
|
|
68
|
+
helmet.link.toString(),
|
|
69
|
+
helmet.script.toString(),
|
|
70
|
+
];
|
|
71
|
+
const metaAttributes = metaStrings.filter(Boolean);
|
|
72
|
+
const { generatedFilesDir } = locals;
|
|
73
|
+
const manifestPath = path.join(generatedFilesDir, 'client-manifest.json');
|
|
74
|
+
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf8'));
|
|
75
|
+
// Get all required assets for this particular page based on client
|
|
76
|
+
// manifest information.
|
|
77
|
+
const modulesToBeLoaded = [...manifest.entrypoints, ...Array.from(modules)];
|
|
78
|
+
const bundles = getBundles(manifest, modulesToBeLoaded);
|
|
79
|
+
const stylesheets = (bundles.css || []).map((b) => b.file);
|
|
80
|
+
const scripts = (bundles.js || []).map((b) => b.file);
|
|
81
|
+
const renderedHtml = renderSSRTemplate(ssrTemplate, {
|
|
82
|
+
appHtml,
|
|
83
|
+
baseUrl,
|
|
84
|
+
htmlAttributes: htmlAttributes || '',
|
|
85
|
+
bodyAttributes: bodyAttributes || '',
|
|
86
|
+
headTags,
|
|
87
|
+
preBodyTags,
|
|
88
|
+
postBodyTags,
|
|
89
|
+
metaAttributes,
|
|
90
|
+
scripts,
|
|
91
|
+
stylesheets,
|
|
92
|
+
noIndex,
|
|
93
|
+
version: packageJson.version,
|
|
143
94
|
});
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
95
|
+
try {
|
|
96
|
+
// Minify html with https://github.com/DanielRuf/html-minifier-terser
|
|
97
|
+
return await minify(renderedHtml, {
|
|
98
|
+
removeComments: false,
|
|
99
|
+
removeRedundantAttributes: true,
|
|
100
|
+
removeEmptyAttributes: true,
|
|
101
|
+
removeScriptTypeAttributes: true,
|
|
102
|
+
removeStyleLinkTypeAttributes: true,
|
|
103
|
+
useShortDoctype: true,
|
|
104
|
+
minifyJS: true,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
logger.error `Minification of page path=${locals.path} failed because of following error:
|
|
109
|
+
${e.stack}`;
|
|
110
|
+
throw e;
|
|
111
|
+
}
|
|
152
112
|
}
|
package/lib/commands/build.d.ts
CHANGED
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { BuildCLIOptions } from '@docusaurus/types';
|
|
7
|
+
import type { BuildCLIOptions } from '@docusaurus/types';
|
|
8
8
|
export default function build(siteDir: string, cliOptions?: Partial<BuildCLIOptions>, forceTerminate?: boolean): Promise<string>;
|