Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions tools/pfe-tools/dev-server/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin, Context, Middleware } from '@web/dev-server-core';
import type { Plugin, Context } from '@web/dev-server-core';
import type { DevServerConfig } from '@web/dev-server';

import { readdir, stat } from 'node:fs/promises';
Expand Down Expand Up @@ -85,25 +85,19 @@ async function cacheBusterMiddleware(ctx: Context, next: () => Promise<any>) {
}
}

function liveReloadTsChangesMiddleware(
function tsRedirectPlugin(
config: ReturnType<typeof normalizeOptions>,
): Middleware {
/**
* capture group 1:
* Either config.elementsDir or `pfe-core`
* `/`
* **ANY** (_>= 0x_)
* `.js`
*/
const TYPESCRIPT_SOURCES_RE = new RegExp(`(${config.elementsDir}|pfe-core)/.*\\.js`);

return function(ctx, next) {
if (!ctx.path.includes('node_modules') && ctx.path
.match(TYPESCRIPT_SOURCES_RE)) {
ctx.redirect(ctx.path.replace('.js', '.ts'));
} else {
return next();
}
): Plugin {
const TYPESCRIPT_SOURCES_RE = new RegExp(`(${config.elementsDir}|pfe-core)/.*\\.js$`);
return {
name: 'pfe-ts-redirect',
serve(context) {
if (!context.path.includes('node_modules')
&& TYPESCRIPT_SOURCES_RE.test(context.path)) {
// Rewrite .js to .ts so esbuild plugin can compile it
context.path = context.path.replace(/\.js$/, '.ts');
}
},
};
}

Expand All @@ -124,11 +118,13 @@ export function pfeDevServerConfig(options?: PfeDevServerConfigOptions): DevServ
middleware: [
cors,
cacheBusterMiddleware,
liveReloadTsChangesMiddleware(config),
...config?.middleware ?? [],
],

plugins: [
// Rewrite .js paths to .ts before static file serving
tsRedirectPlugin(config),

// Dev server app which loads component demo files
pfeDevServerPlugin(config),

Expand Down
Loading