Skip to content

Commit 9a95768

Browse files
committed
feat(tools): move dev SPA wholly into tools
1 parent cfbae7e commit 9a95768

File tree

11 files changed

+117
-29
lines changed

11 files changed

+117
-29
lines changed

.changeset/poor-birds-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/pfe-tools": minor
3+
---
4+
5+
Moves development server wholly into pfe-tools, and exposes some customization properties so daughter repos can customize the page

package-lock.json

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/pfe-tools/demo/components.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
export * from '@patternfly/pfe-accordion';
3+
export * from '@patternfly/pfe-autocomplete';
4+
export * from '@patternfly/pfe-avatar';
5+
export * from '@patternfly/pfe-badge';
6+
export * from '@patternfly/pfe-band';
7+
export * from '@patternfly/pfe-button';
8+
export * from '@patternfly/pfe-card';
9+
export * from '@patternfly/pfe-clipboard';
10+
export * from '@patternfly/pfe-codeblock';
11+
export * from '@patternfly/pfe-collapse';
12+
export * from '@patternfly/pfe-cta';
13+
export * from '@patternfly/pfe-datetime';
14+
export * from '@patternfly/pfe-dropdown';
15+
export * from '@patternfly/pfe-health-index';
16+
export * from '@patternfly/pfe-icon';
17+
export * from '@patternfly/pfe-icon-panel';
18+
export * from '@patternfly/pfe-jump-links';
19+
export * from '@patternfly/pfe-label';
20+
export * from '@patternfly/pfe-markdown';
21+
export * from '@patternfly/pfe-modal';
22+
export * from '@patternfly/pfe-number';
23+
export * from '@patternfly/pfe-page-status';
24+
export * from '@patternfly/pfe-primary-detail';
25+
export * from '@patternfly/pfe-progress-indicator';
26+
export * from '@patternfly/pfe-progress-steps';
27+
export * from '@patternfly/pfe-readtime';
28+
export * from '@patternfly/pfe-select';
29+
export * from '@patternfly/pfe-tabs';
30+
export * from '@patternfly/pfe-toast';
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { HTMLIncludeElement } from 'html-include-element';
2+
13
import 'html-include-element';
24
import 'api-viewer-element';
35
import '@vaadin/split-layout';
@@ -84,8 +86,7 @@ const linkLoaded = async function linkLoaded(link: HTMLLinkElement) {
8486
});
8587
};
8688

87-
/** @this {import('html-include-element').HTMLIncludeElement} */
88-
async function loadPartial() {
89+
async function loadPartial(this: HTMLIncludeElement) {
8990
let text = '';
9091
try {
9192
const mode = this.mode || 'cors';

index.html renamed to tools/pfe-tools/demo/index.html

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@
44
<head>
55
<meta charset="utf-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<meta name="description"
8-
content="PatternFly Elements: A set of community-created web components based on PatternFly design.">
7+
<meta name="description" content="{{ description }}">
98
<meta name="pfe-track-performance" content="true">
109
<link href="/brand/logo/svg/pfe-icon-blue.svg" rel="shortcut icon">
11-
<title>PatternFly Elements</title>
12-
<link rel="stylesheet" href="/docs/demo/demo.css">
13-
<link rel="stylesheet" href="/docs/main.css" />
14-
<link rel="stylesheet" href="/core/pfe-styles/pfe.min.css">
15-
<link rel="stylesheet" href="/core/pfe-styles/pfe-layouts.min.css">
16-
<link rel="stylesheet" href="/core/pfe-styles/red-hat-font.min.css">
17-
<script type="module">
18-
import '/docs/demo/demo.ts';
19-
</script>
10+
<title>{{ title }}</title>
11+
<link rel="stylesheet" href="/node_modules/@patternfly/pfe-styles/pfe.min.css">
12+
<link rel="stylesheet" href="/node_modules/@patternfly/pfe-styles/pfe-layouts.min.css">
13+
<link rel="stylesheet" href="/node_modules/@patternfly/pfe-styles/red-hat-font.min.css">
14+
<link rel="stylesheet" href="/node_modules/@patternfly/pfe-tools/demo/demo.css">
15+
<link rel="stylesheet" href="/docs/main.css">
16+
<script type="module" src="/node_modules/@patternfly/pfe-tools/demo/demo.ts"></script>
2017
</head>
2118

2219
<body>
2320
<header class="logo-bar">
2421
<button id="hamburger" aria-label="Expand Main Navigation">&#9776;</button>
25-
<a href="/"><img src="/brand/logo/svg/pfe-icon-white-shaded.svg" alt="" />PatternFly Elements</a>
26-
<a id="github-link" href="https://github.com/patternfly/patternfly-elements/">
22+
<a href="/"><img src="{{ logoUrl }}" alt="" />{{ title }}</a>
23+
<a id="github-link" href="{{ githubUrl }}">
2724
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512">
2825
<title>GitHub</title>
2926
<!-- Font Awesome Free 5.15.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) -->

tools/pfe-tools/dev-server.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
11
import type { Plugin } from '@web/dev-server-core';
22
import type { DevServerConfig } from '@web/dev-server';
33
import type { InjectSetting } from '@web/dev-server-import-maps/dist/importMapsPlugin';
4+
import type { Context, Middleware, Next } from 'koa';
45

56
import { readdir, stat } from 'fs/promises';
67
import { join, dirname } from 'path';
78
import { fileURLToPath } from 'url';
89

910
import litcssRollup from 'rollup-plugin-lit-css';
1011
import rollupReplace from '@rollup/plugin-replace';
12+
import nunjucks from 'nunjucks';
1113

1214
import { fromRollup } from '@web/dev-server-rollup';
1315
import { esbuildPlugin } from '@web/dev-server-esbuild';
1416
import { importMapsPlugin } from '@web/dev-server-import-maps';
1517
import { transformSass } from './esbuild.js';
1618
import { createRequire } from 'module';
1719

18-
const require = createRequire(import.meta.url);
19-
const exists = (x: string) => stat(x).then(() => true, () => false);
20-
2120
export interface PfeDevServerConfigOptions extends DevServerConfig {
2221
/** Extra dev server plugins */
2322
plugins?: Plugin[];
2423
importMap?: InjectSetting['importMap'];
2524
hostname?: string;
25+
site?: {
26+
title: string;
27+
logoUrl: string;
28+
githubUrl: string;
29+
description: string;
30+
};
2631
}
2732

33+
const require = createRequire(import.meta.url);
34+
const exists = (x: string) => stat(x).then(() => true, () => false);
2835
const litcss = fromRollup(litcssRollup);
2936
const replace = fromRollup(rollupReplace);
37+
const env = nunjucks.configure(fileURLToPath(new URL('./demo', import.meta.url)));
38+
39+
const SITE_DEFAULTS = {
40+
title: 'PatternFly Elements',
41+
logoUrl: '/brand/logo/svg/pfe-icon-white-shaded.svg',
42+
githubUrl: 'https://github.com/patternfly/patternfly-elements/',
43+
description: 'PatternFly Elements: A set of community-created web components based on PatternFly design.',
44+
};
3045

3146
function appendLines(body: string, ...lines: string[]): string {
3247
return [body, ...lines].join('\n');
@@ -127,6 +142,24 @@ export function resolveLocalFilesFromTypeScriptSources(options: PfeDevServerConf
127142
};
128143
}
129144

145+
function nunjucksSPAMiddleware(options: PfeDevServerConfigOptions): Middleware {
146+
return function(ctx, next) {
147+
if (ctx.method !== 'HEAD' && ctx.method !== 'GET' || ctx.path.match(/\./)) {
148+
return next();
149+
} else {
150+
const transformed = env.render('index.html', { ...SITE_DEFAULTS, ...options?.site });
151+
ctx.body = transformed;
152+
ctx.type = 'html';
153+
ctx.status = 200;
154+
}
155+
};
156+
}
157+
158+
function cors(context: Context, next: Next) {
159+
context.set('Access-Control-Allow-Origin', '*');
160+
return next();
161+
}
162+
130163
/**
131164
* Creates a default config for PFE's dev server.
132165
*/
@@ -147,19 +180,15 @@ export function pfeDevServerConfig(_options?: PfeDevServerConfigOptions): DevSer
147180
.replace('//', '/');
148181

149182
return {
150-
appIndex: 'index.html',
183+
rootDir,
151184

152185
nodeResolve: true,
153186

154-
rootDir,
155-
156187
...options ?? {},
157188

158189
middleware: [
159-
function cors(context, next) {
160-
context.set('Access-Control-Allow-Origin', '*');
161-
return next();
162-
},
190+
nunjucksSPAMiddleware(options),
191+
cors,
163192
...options?.middleware ?? [],
164193
],
165194

@@ -187,7 +216,7 @@ export function pfeDevServerConfig(_options?: PfeDevServerConfigOptions): DevSer
187216
litcss({ include: ['**/*.scss'], transform: transformSass }),
188217
replace({
189218
'preventAssignment': true,
190-
'process.env.NODE_ENV': JSON.stringify( 'production' )
219+
'process.env.NODE_ENV': JSON.stringify('production'),
191220
}),
192221
// Ensure .scss files are loaded as js modules, as `litcss` plugin transforms them to such
193222
scssMimeType(),

tools/pfe-tools/esbuild.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ export async function componentsEntryPoint(options?: { prefix: string }) {
130130
/** Create a single-file production bundle of all elements */
131131
export async function singleFileBuild(options?: PfeEsbuildSingleFileOptions) {
132132
try {
133+
const prefix = fileURLToPath(new URL('./demo', import.meta.url)).replace(REPO_ROOT, '');
133134
const result = await esbuild.build({
134135
absWorkingDir: REPO_ROOT,
135136
allowOverwrite: true,
136137
bundle: true,
137-
entryPoints: [await componentsEntryPoint({ prefix: 'docs/demo' })],
138+
entryPoints: [await componentsEntryPoint({ prefix })],
138139
format: 'esm',
139140
keepNames: true,
140141
legalComments: 'linked',

tools/pfe-tools/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"eslint": "^8.11.0",
9494
"glob": "^7.2.0",
9595
"html-include-element": "^0.3.0",
96+
"koa-send": "^5.0.1",
9697
"lit": "^2.1.2",
9798
"markdown-it-anchor": "^8.4.1",
9899
"npm-run-all": "^4.1.5",
@@ -111,6 +112,7 @@
111112
"devDependencies": {
112113
"@changesets/cli": "^2.20.0",
113114
"@types/dedent": "^0.7.0",
115+
"@types/koa-send": "^4.1.3",
114116
"patch-package": "^6.4.7"
115117
}
116118
}

0 commit comments

Comments
 (0)