-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathdev-server-templates.ts
More file actions
127 lines (115 loc) · 3.98 KB
/
dev-server-templates.ts
File metadata and controls
127 lines (115 loc) · 3.98 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
import type { PfeDevServerInternalConfig } from './pfe-dev-server.js';
import type { DemoRecord } from '../../custom-elements-manifest/lib/Manifest.js';
import type { Context, Next } from 'koa';
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { glob } from 'glob';
import nunjucks from 'nunjucks';
import { Manifest } from '../../custom-elements-manifest/lib/Manifest.js';
const env = nunjucks.configure(join(dirname(fileURLToPath(import.meta.url)), 'templates'));
env.addFilter('log', x => (console.log(x), x)); // eslint-disable-line no-console
// TODO: regexing html is stupid
const MOD_STYLE_RE = /(<script type="module">.*<\/script>)|(<style>.*<\/style>)/g;
env.addFilter('noModulesOrStyles', x => x.replaceAll(MOD_STYLE_RE, ''));
env.addFilter('noElement', (x, tagName) => x.replaceAll(new RegExp(`<${tagName}>.*</${tagName}>`, 'gm'), ''));
env.addFilter('getSourceControlUrl', function(
manifest: Manifest,
sourceControlURLPrefix: string,
cwd: string,
) {
if (!manifest || !sourceControlURLPrefix) {
return '';
} else {
return `${sourceControlURLPrefix.replace('tree/main/', '')}${(
`tree/main${
manifest.path.replace(cwd, '')
.replace('/custom-elements.json', '/')}`
)}`;
}
});
async function getElementsPkgJson(config: PfeDevServerInternalConfig) {
let dir = config.elementsDir;
let pkgPath;
while (dir !== config.rootDir && !pkgPath) {
[pkgPath] = await glob('package.json', { cwd: dir, absolute: true });
if (!pkgPath) {
dir = dirname(dir);
}
}
try {
if (pkgPath) {
return JSON.parse(await readFile(pkgPath, 'utf8'));
}
} catch {
return;
}
}
/**
* cludge to ensure the dev server starts up only after the manifests are generated
* @param config Normalized dev server options
*/
async function waitForManifestFileThenReadIt(config: PfeDevServerInternalConfig) {
let count = 0;
let manifests = Manifest.getAll(config.rootDir);
while (count < 1000 && manifests.length < 0) {
await new Promise(r => setTimeout(r, 50));
count++;
manifests = Manifest.getAll(config.rootDir);
}
return manifests;
}
function getDemos(config: PfeDevServerInternalConfig, manifests: Manifest[]) {
return manifests
.flatMap(manifest =>
manifest
.getTagNames()
.flatMap(tagName =>
manifest.getDemoMetadata(tagName, config as PfeDevServerInternalConfig)));
}
async function getTemplateContent(demo?: DemoRecord) {
if (typeof demo?.filePath === 'string') {
return readFile(demo.filePath, 'utf8');
} else {
return undefined;
}
}
async function getManifest(
config: PfeDevServerInternalConfig,
manifests: Manifest[],
demo?: DemoRecord,
) {
const elementsPkgJson = await getElementsPkgJson(config);
const manifest =
demo?.manifest ?? manifests.find(x => x.packageJson?.name === elementsPkgJson.name);
return manifest;
}
/**
* Render the demo page whenever there's a trailing slash
* @param config Normalized dev server options
*/
export function pfeDevServerTemplateMiddleware(config: PfeDevServerInternalConfig) {
return async function(ctx: Context, next: Next): Promise<void> {
const { method, path } = ctx;
if (config.loadDemo && !(method !== 'HEAD' && method !== 'GET' || path.includes('.'))) {
const manifests = await waitForManifestFileThenReadIt(config);
const url = new URL(ctx.request.url, `http://${ctx.request.headers.host}`);
const demos = getDemos(config, manifests);
const demo = demos.find(x => x.permalink === url.pathname);
const templateContent = await getTemplateContent(demo);
const manifest = await getManifest(config, manifests, demo);
ctx.cwd = process.cwd();
ctx.type = 'html';
ctx.status = 200;
ctx.body = env.render('index.html', {
context: ctx,
options: config,
demo,
demos,
manifest,
templateContent,
});
}
return next();
};
}