forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
266 lines (252 loc) · 8.59 KB
/
api.ts
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
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor';
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { BuildConfig, panic } from './util';
/**
* Create each submodule's bundled dts file, and ensure
* the public API has not changed for a production build.
*/
export function apiExtractor(config: BuildConfig) {
// core
// Run the api extractor for each of the submodules
createTypesApi(config, join(config.srcDir, 'core'), join(config.distPkgDir, 'core.d.ts'), '.');
createTypesApi(
config,
join(config.srcDir, 'jsx-runtime'),
join(config.distPkgDir, 'jsx-runtime.d.ts'),
'.'
);
createTypesApi(
config,
join(config.srcDir, 'optimizer'),
join(config.distPkgDir, 'optimizer.d.ts'),
'.'
);
createTypesApi(
config,
join(config.srcDir, 'server'),
join(config.distPkgDir, 'server.d.ts'),
'.'
);
createTypesApi(
config,
join(config.srcDir, 'testing'),
join(config.distPkgDir, 'testing', 'index.d.ts'),
'..'
);
createTypesApi(
config,
join(config.srcDir, 'build'),
join(config.distPkgDir, 'build', 'index.d.ts'),
'..'
);
generateServerReferenceModules(config);
// qwik-city
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'runtime', 'src'),
join(config.packagesDir, 'qwik-city', 'lib', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'runtime', 'src', 'service-worker'),
join(config.packagesDir, 'qwik-city', 'lib', 'service-worker.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'buildtime', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'azure-swa', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'azure-swa', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'cloudflare-pages', 'vite'),
join(
config.packagesDir,
'qwik-city',
'lib',
'adapters',
'cloudflare-pages',
'vite',
'index.d.ts'
)
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'cloud-run', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'cloud-run', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'express', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'express', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'netlify-edge', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'netlify-edge', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'shared', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'shared', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'static', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'static', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'adapters', 'vercel-edge', 'vite'),
join(config.packagesDir, 'qwik-city', 'lib', 'adapters', 'vercel-edge', 'vite', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'middleware', 'azure-swa'),
join(config.packagesDir, 'qwik-city', 'lib', 'middleware', 'azure-swa', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'middleware', 'cloudflare-pages'),
join(config.packagesDir, 'qwik-city', 'lib', 'middleware', 'cloudflare-pages', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'middleware', 'netlify-edge'),
join(config.packagesDir, 'qwik-city', 'lib', 'middleware', 'netlify-edge', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'middleware', 'node'),
join(config.packagesDir, 'qwik-city', 'lib', 'middleware', 'node', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'middleware', 'request-handler'),
join(config.packagesDir, 'qwik-city', 'lib', 'middleware', 'request-handler', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'static'),
join(config.packagesDir, 'qwik-city', 'lib', 'static', 'index.d.ts')
);
createTypesApi(
config,
join(config.packagesDir, 'qwik-city', 'middleware', 'vercel-edge'),
join(config.packagesDir, 'qwik-city', 'lib', 'middleware', 'vercel-edge', 'index.d.ts')
);
generateQwikCityReferenceModules(config);
console.log('🥶', 'submodule d.ts API files generated');
}
function createTypesApi(
config: BuildConfig,
inPath: string,
outPath: string,
relativePath?: string
) {
const extractorConfigPath = join(inPath, 'api-extractor.json');
const extractorConfig = ExtractorConfig.loadFileAndPrepare(extractorConfigPath);
const result = Extractor.invoke(extractorConfig, {
localBuild: !!config.dev,
showVerboseMessages: true,
showDiagnostics: true,
messageCallback(msg) {
msg.handled = true;
if (msg.logLevel === 'verbose' || msg.logLevel === 'warning') {
return;
}
if (msg.text.includes('Analysis will use')) {
return;
}
if (msg.messageId === 'console-compiler-version-notice') {
return;
}
console.error(`❌ API Extractor, submodule: "${inPath}"\n${extractorConfigPath}\n`, msg);
},
});
if (!result.succeeded) {
panic(
`Use "pnpm api.update" to automatically update the .md files if the api changes were expected`
);
}
const srcPath = result.extractorConfig.untrimmedFilePath;
const content = fixDtsContent(config, srcPath, relativePath);
writeFileSync(outPath, content);
}
function generateQwikCityReferenceModules(config: BuildConfig) {
// @builder.io/qwik-city/server-modules.d.ts
const referenceDts = `
declare module '@qwik-city-plan' {
export const routes: any[];
export const menus: any[];
export const trailingSlash: boolean;
export const basePathname: string;
export const cacheModules: boolean;
const defaultExport: {
routes: any[];
menus: any[];
trailingSlash: boolean;
basePathname: string;
cacheModules: boolean;
};
export default defaultExport;
}
`;
const srcModulesPath = join(config.packagesDir, 'qwik-city', 'lib');
const destModulesPath = join(srcModulesPath, 'modules.d.ts');
writeFileSync(destModulesPath, referenceDts);
// manually prepend the ts reference since api extractor removes it
const prependReferenceDts = `/// <reference path="./modules.d.ts" />\n\n`;
const distIndexPath = join(srcModulesPath, 'index.d.ts');
let serverDts = readFileSync(distIndexPath, 'utf-8');
serverDts = prependReferenceDts + serverDts;
writeFileSync(distIndexPath, serverDts);
}
function generateServerReferenceModules(config: BuildConfig) {
// server-modules.d.ts
const referenceDts = `/// <reference types="./server" />
/// <reference types="./core" />
declare module '@qwik-client-manifest' {
const manifest: import('./optimizer').QwikManifest;
export { manifest };
}
// MD
declare module '*.md' {
const node: FunctionComponent;
export default node;
}
// MDX
declare module '*.mdx' {
const node: FunctionComponent;
export default node;
}
`;
const destServerModulesPath = join(config.distPkgDir, 'server-modules.d.ts');
writeFileSync(destServerModulesPath, referenceDts);
// manually prepend the ts reference since api extractor removes it
const prependReferenceDts = `/// <reference path="./server-modules.d.ts" />\n\n`;
const distServerPath = join(config.distPkgDir, 'server.d.ts');
let serverDts = readFileSync(distServerPath, 'utf-8');
serverDts = prependReferenceDts + serverDts;
writeFileSync(distServerPath, serverDts);
}
/**
* Fix up the generated dts content, and ensure it's using a relative
* path to find the core.d.ts file, rather than node resolving it.
*/
function fixDtsContent(config: BuildConfig, srcPath: string, relativePath?: string) {
let dts = readFileSync(srcPath, 'utf-8');
// ensure we're just using a relative path
if (relativePath) {
dts = dts.replace(/'@builder\.io\/qwik(.*)'/g, `'${relativePath}$1'`);
}
// for some reason api-extractor is adding this in ¯\_(ツ)_/¯
dts = dts.replace('{};', '');
// replace QWIK_VERSION with the actual version number, useful for debugging
return dts.replace(/QWIK_VERSION/g, config.distVersion);
}