forked from facebook/docusaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.ts
More file actions
174 lines (162 loc) · 3.64 KB
/
codegen.ts
File metadata and controls
174 lines (162 loc) · 3.64 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
generate,
escapePath,
DEFAULT_CONFIG_FILE_NAME,
} from '@docusaurus/utils';
import {generateRouteFiles} from './codegenRoutes';
import type {
CodeTranslations,
DocusaurusConfig,
GlobalData,
I18n,
PluginRouteConfig,
SiteMetadata,
SiteStorage,
} from '@docusaurus/types';
function genWarning({generatedFilesDir}: {generatedFilesDir: string}) {
return generate(
generatedFilesDir,
// cSpell:ignore DONT
'DONT-EDIT-THIS-FOLDER',
`This folder stores temp files that Docusaurus' client bundler accesses.
DO NOT hand-modify files in this folder because they will be overwritten in the
next build. You can clear all build artifacts (including this folder) with the
\`docusaurus clear\` command.
`,
);
}
function genSiteConfig({
generatedFilesDir,
siteConfig,
}: {
generatedFilesDir: string;
siteConfig: DocusaurusConfig;
}) {
return generate(
generatedFilesDir,
`${DEFAULT_CONFIG_FILE_NAME}.mjs`,
`/*
* AUTOGENERATED - DON'T EDIT
* Your edits in this file will be overwritten in the next build!
* Modify the docusaurus.config.js file at your site's root instead.
*/
export default ${JSON.stringify(siteConfig, null, 2)};
`,
);
}
function genClientModules({
generatedFilesDir,
clientModules,
}: {
generatedFilesDir: string;
clientModules: string[];
}) {
return generate(
generatedFilesDir,
'client-modules.js',
`export default [
${clientModules
// Use `require()` because `import()` is async but client modules can have CSS
// and the order matters for loading CSS.
.map((clientModule) => ` require("${escapePath(clientModule)}"),`)
.join('\n')}
];
`,
);
}
function genGlobalData({
generatedFilesDir,
globalData,
}: {
generatedFilesDir: string;
globalData: GlobalData;
}) {
return generate(
generatedFilesDir,
'globalData.json',
JSON.stringify(globalData, null, 2),
);
}
function genI18n({
generatedFilesDir,
i18n,
}: {
generatedFilesDir: string;
i18n: I18n;
}) {
return generate(
generatedFilesDir,
'i18n.json',
JSON.stringify(i18n, null, 2),
);
}
function genCodeTranslations({
generatedFilesDir,
codeTranslations,
}: {
generatedFilesDir: string;
codeTranslations: CodeTranslations;
}) {
return generate(
generatedFilesDir,
'codeTranslations.json',
JSON.stringify(codeTranslations, null, 2),
);
}
function genSiteMetadata({
generatedFilesDir,
siteMetadata,
}: {
generatedFilesDir: string;
siteMetadata: SiteMetadata;
}) {
return generate(
generatedFilesDir,
'site-metadata.json',
JSON.stringify(siteMetadata, null, 2),
);
}
function genSiteStorage({
generatedFilesDir,
siteStorage,
}: {
generatedFilesDir: string;
siteStorage: SiteStorage;
}) {
return generate(
generatedFilesDir,
'site-storage.json',
JSON.stringify(siteStorage, null, 2),
);
}
type CodegenParams = {
generatedFilesDir: string;
siteConfig: DocusaurusConfig;
baseUrl: string;
clientModules: string[];
globalData: GlobalData;
i18n: I18n;
codeTranslations: CodeTranslations;
siteMetadata: SiteMetadata;
siteStorage: SiteStorage;
routes: PluginRouteConfig[];
};
export async function generateSiteFiles(params: CodegenParams): Promise<void> {
await Promise.all([
genWarning(params),
genClientModules(params),
genSiteConfig(params),
generateRouteFiles(params),
genGlobalData(params),
genSiteMetadata(params),
genSiteStorage(params),
genI18n(params),
genCodeTranslations(params),
]);
}