Skip to content

Commit

Permalink
fix(tools): dev server fixes (#2785)
Browse files Browse the repository at this point in the history
* fix(tools): dev server reload for ts changes

* fix(tools): dev server light dom shims
  • Loading branch information
bennypowers authored Jun 21, 2024
1 parent 4995067 commit 9d68f3d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .changeset/huge-mice-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@patternfly/pfe-tools": patch
---
**Dev Server**: load lightdom shim files
4 changes: 4 additions & 0 deletions .changeset/legal-chairs-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@patternfly/pfe-tools": patch
---
**Dev Server**: reload on typescript file changes
15 changes: 14 additions & 1 deletion tools/pfe-tools/dev-server/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin } from '@web/dev-server-core';
import type { DevServerConfig } from '@web/dev-server';
import type { Context, Next } from 'koa';
import type { Middleware, Context, Next } from 'koa';

import { readdir, stat } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
Expand Down Expand Up @@ -80,6 +80,18 @@ async function cacheBusterMiddleware(ctx: Context, next: Next) {
}
}

function liveReloadTsChangesMiddleware(
config: ReturnType<typeof normalizeOptions>,
): Middleware {
return function(ctx, next) {
if (!ctx.path.includes('node_modules') && ctx.path.match(new RegExp(`/^${config?.elementsDir}\\/.*.js/`))) {
ctx.redirect(ctx.path.replace('.js', '.ts'));
} else {
return next();
}
};
}

/**
* Creates a default config for PFE's dev server.
*/
Expand All @@ -96,6 +108,7 @@ export function pfeDevServerConfig(options?: PfeDevServerConfigOptions): DevServ
middleware: [
cors,
cacheBusterMiddleware,
liveReloadTsChangesMiddleware(config),
...config?.middleware ?? [],
],

Expand Down
6 changes: 3 additions & 3 deletions tools/pfe-tools/dev-server/plugins/pfe-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ function getRouter(options: PfeDevServerInternalConfig) {

// Redirect `components/jazz-hands/*-lightdom.css` to `elements/pf-jazz-hands/*-lightdom.css`
// NOTE: don't put subresources in /demo called `*-lightdom.css` , or this will break
.get(`/${componentSubpath}/:element/(demo/)?:fileName-lightdom.css`, async (ctx, next) => {
.get(`/${componentSubpath}/:element/(demo/)?:fileName.css`, async (ctx, next) => {
const { element, fileName } = ctx.params;
if (!element.startsWith(tagPrefix)) {
if (!element.startsWith(tagPrefix) && fileName.includes('lightdom')) {
const prefixedElement = deslugify(element);
ctx.redirect(`/${elementsDir}/${prefixedElement}/${fileName}-lightdom.css`);
ctx.redirect(`/${elementsDir}/${prefixedElement}/${fileName}.css`);
} else {
return next();
}
Expand Down

0 comments on commit 9d68f3d

Please sign in to comment.