-
Notifications
You must be signed in to change notification settings - Fork 20
/
web-dev-server.config.js
195 lines (187 loc) · 5.79 KB
/
web-dev-server.config.js
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
// @ts-check
import { pfeDevServerConfig } from '@patternfly/pfe-tools/dev-server/config.js';
import { glob } from 'node:fs/promises';
import { join } from 'node:path';
import { makeDemoEnv } from './scripts/environment.js';
import { parse, serialize } from 'parse5';
import {
createElement,
getAttribute,
getTextContent,
isElementNode,
query,
setAttribute,
setTextContent,
spliceChildren,
} from '@parse5/tools';
/**
* Find all modules in a glob pattern, relative to the repo root, and resolve them as package paths
* @param {string} pattern
* @param {string} [relativeTo='.']
*/
async function resolveLocal(pattern, relativeTo = './') {
const TEST_RE = /\/test\/|(\.d\.ts$)/;
// eslint-disable-next-line jsdoc/check-tag-names
/** @type [string, string][] */
const files = [];
for await (const file of glob(pattern, { cwd: join(process.cwd(), relativeTo) })) {
if (!TEST_RE.test(file)) {
files.push([
`@rhds/elements/${file.replace('.ts', '.js')}`,
join(relativeTo, file).replace('./', '/'),
]);
}
}
return Object.fromEntries(files);
}
/**
* manually inject icon import map with trailing slash.
* jspm generator doesn't yet support trailing slash
* @param {import('@parse5/tools').Document} document
*/
function injectManuallyResolvedModulesToImportMap(document) {
const importMapNode = query(document, node =>
isElementNode(node)
&& node.tagName === 'script'
&& node.attrs.some(attr =>
attr.name === 'type'
&& attr.value === 'importmap'));
if (importMapNode && isElementNode(importMapNode)) {
const json = JSON.parse(getTextContent(importMapNode));
Object.assign(json.imports, {
'lit': '/node_modules/lit/index.js',
'lit/': '/node_modules/lit/',
'@patternfly/pfe-core': '/node_modules/@patternfly/pfe-core/core.js',
'@patternfly/pfe-core/': '/node_modules/@patternfly/pfe-core/',
'@rhds/icons/': '/node_modules/@rhds/icons/',
'@rhds/tokens/': '/node_modules/@rhds/tokens/js/',
'@rhds/tokens/css/': '/node_modules/@rhds/tokens/css/',
'@floating-ui/dom': '/node_modules/@floating-ui/dom/dist/floating-ui.dom.browser.min.mjs',
'@floating-ui/core': '/node_modules/@floating-ui/core/dist/floating-ui.core.browser.min.mjs',
});
setTextContent(importMapNode, JSON.stringify(json, null, 2));
}
}
/**
* add context picker to dev sserver chrome
* @param {import('@parse5/tools').Document} document
*/
function transformDevServerHTML(document) {
const surfaceId = 'rhds-dev-server-main';
// replace the <main> element with a surface
const main = query(document, x =>
isElementNode(x)
&& x.tagName === 'main');
if (main && isElementNode(main)) {
main.tagName = 'rh-surface';
setAttribute(main, 'color-palette', 'lightest');
setAttribute(main, 'id', surfaceId);
setAttribute(main, 'role', 'main');
}
// add a context picker to header, targeting main
const header = query(document, x =>
isElementNode(x)
&& getAttribute(x, 'id') === 'main-header');
if (header && isElementNode(header)) {
const picker = createElement('rh-context-picker');
setAttribute(picker, 'target', surfaceId);
setAttribute(picker, 'value', 'lightest');
const logoBar = query(header, node =>
isElementNode(node)
&& getAttribute(node, 'class') === 'logo-bar');
if (logoBar) {
spliceChildren(logoBar, 4, 0, picker);
}
}
// import surface and picker
const module = query(document, x =>
isElementNode(x)
&& x.tagName === 'script'
&& getAttribute(x, 'type') === 'module');
if (module) {
setTextContent(module, /* js */`${getTextContent(module)}
import '@rhds/elements/rh-surface/rh-surface.js';
import '@rhds/elements/rh-tooltip/rh-tooltip.js';
import '@rhds/elements/lib/elements/rh-context-picker/rh-context-picker.js';
`);
}
}
export const litcssOptions = {
exclude: [
/(lightdom)/,
/node_modules\/@rhds\/tokens\/css\/global\.css/,
],
include: [
/elements\/rh-[\w-]+\/[\w-]+\.css$/,
/@rhds\/tokens\/css\/.*\.css$/,
/lib\/.*\.css$/,
],
};
const imports = {
...await resolveLocal('./lib/**/*.ts'),
...await resolveLocal('./**/*.ts', './elements'),
};
export default pfeDevServerConfig({
tsconfig: 'tsconfig.settings.json',
litcssOptions,
importMapOptions: {
typeScript: true,
ignore: [
/^\./,
/^@rhds\/icons/,
],
inputMap: { imports },
},
middleware: [
async function(ctx, next) {
if (ctx.path === '/lib/environment.ts') {
ctx.type = 'text/javascript';
ctx.body = await makeDemoEnv();
} else {
return next();
}
},
/**
* redirect requests for /assets/ css to /docs/assets/
* @param ctx koa context
* @param next next koa middleware
*/
function(ctx, next) {
if (ctx.path.startsWith('/styles/')) {
ctx.redirect(`/docs${ctx.path}`);
} else {
return next();
}
},
/**
* @param ctx koa context
* @param next next koa middleware
*/
async function(ctx, next) {
if (ctx.path.endsWith('/') && !ctx.path.includes('.')) {
await next();
const document = parse(ctx.body);
injectManuallyResolvedModulesToImportMap(document);
transformDevServerHTML(document);
ctx.body = serialize(document);
} else {
return next();
}
},
],
plugins: [
{
name: 'watch-demos',
serverStart(args) {
const fsDemoFilesGlob = new URL('./elements/*/demo/**/*.html', import.meta.url).pathname;
args.fileWatcher.add(fsDemoFilesGlob);
args.app.use(function(ctx, next) {
if (ctx.path.match(/\/|\.css|\.html|\.js$/)) {
ctx.etag = `e${Math.random() * Date.now()}`;
}
return next();
});
},
},
],
});