-
Notifications
You must be signed in to change notification settings - Fork 18
/
web-dev-server.config.js
74 lines (71 loc) · 2.17 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
// @ts-check
import { pfeDevServerConfig } from '@patternfly/pfe-tools/dev-server/config.js';
import { glob } from 'glob';
/**
* Find all modules in a glob pattern, relative to the repo root, and resolve them as package paths
* @param {string} pattern
* @param {(spec: string) => [string, string]} fn
*/
async function resolveLocal(pattern, fn) {
return glob(pattern, { ignore: ['**/test/**'] })
.then(files => files.map(fn))
.then(Object.fromEntries);
}
export const litcssOptions = {
include: (/** @type{string[]}*/(/** @type{unknown}*/([
/elements\/rh-[\w-]+\/[\w-]+\.css$/,
/lib\/.*\.css$/,
]))),
exclude: /lightdom/,
};
export default pfeDevServerConfig({
tsconfig: 'tsconfig.json',
litcssOptions,
importMapOptions: {
providers: {
'@patternfly/icons': 'nodemodules',
'@patternfly/elements': 'nodemodules',
'@patternfly/pfe-tools': 'nodemodules',
'@patternfly/pfe-core': 'nodemodules',
},
inputMap: {
imports: {
...await resolveLocal('./lib/**/*.js', spec => [`@rhds/elements/${spec}`, `./${spec}`]),
...await resolveLocal('./elements/**/*.js', x => [`@rhds/elements/${x.replace('elements/', '')}`, `./${x}`]),
},
},
},
middleware: [
/** redirect requests for /assets/ css to /docs/assets/ */
function(ctx, next) {
if (ctx.path.startsWith('/styles/')) {
ctx.redirect(`/docs${ctx.path}`);
} else {
return next();
}
},
/** redirect requests for /(lib|elements)/*.js to *.ts */
function(ctx, next) {
if (!ctx.path.includes('node_modules') && ctx.path.match(/.*\/(lib|elements)\/.*\.js/)) {
ctx.redirect(ctx.path.replace('.js', '.ts'));
} 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();
});
},
},
],
});