Skip to content

Commit 27ad7b6

Browse files
authored
Merge pull request #2464 from patternfly/fix/tools/dev-server-config
fix(tools): account for custom componentsSubpath in router
2 parents 2c2fde1 + c3374e4 commit 27ad7b6

File tree

3 files changed

+51
-51
lines changed

3 files changed

+51
-51
lines changed

.changeset/long-islands-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/pfe-tools": patch
3+
---
4+
5+
**Dev Server**: updates router to use project subpath configuration

tools/pfe-tools/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface SiteOptions {
1313
stylesheets?: string[];
1414
/** Title for main page of the demo */
1515
title?: string;
16-
/** Site subpath for components. e.g. 'elements'. default: 'components' */
16+
/** Site subpath for components. default: 'components' i.e. 'https://patternflyelements.org/components' */
1717
componentSubpath?: string;
1818
}
1919

@@ -46,6 +46,7 @@ const SITE_DEFAULTS: Required<SiteOptions> = {
4646
const DEFAULT_CONFIG: PfeConfig = {
4747
demoURLPrefix: 'https://patternflyelements.org/',
4848
sourceControlURLPrefix: 'https://github.com/patternfly/patternfly-elements/tree/main/',
49+
elementsDir: 'elements',
4950
tagPrefix: 'pf',
5051
aliases: {},
5152
};

tools/pfe-tools/dev-server/config.ts

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -94,57 +94,51 @@ function pfeDevServerPlugin(options: PfeDevServerInternalConfig): Plugin {
9494
return {
9595
name: 'pfe-dev-server',
9696
async serverStart({ fileWatcher, app }) {
97-
app.use(new Router()
98-
.get(/\/pf-icon\/icons\/.*\.js$/, (ctx, next) => {
99-
ctx.type = 'application/javascript';
100-
return next();
101-
})
102-
.get('/elements/:tagName/:fileName.js', async ctx => {
103-
return ctx.redirect(`/elements/${ctx.params.tagName}/${ctx.params.fileName}.ts`);
104-
})
105-
.get('/tools/pfe-tools/environment.js(.js)?', async ctx => {
106-
ctx.body = await makeDemoEnv(options.rootDir);
107-
ctx.type = 'application/javascript';
108-
})
109-
// redirect /components/jazz-hands/pf-jazz-hands/index.html to /elements/pf-jazz-hands/demo/pf-jazz-hands.html
110-
// redirect /components/jazz-hands/index.html to /elements/pf-jazz-hands/demo/pf-jazz-hands.html
111-
.get('/components/:slug/demo/:sub?/:fileName', (ctx, next) => {
112-
const { slug, fileName } = ctx.params;
113-
if (fileName.includes('.')) {
114-
const tagName = deslugify(slug, options.rootDir);
115-
const redir = `/elements/${tagName}/demo/${fileName === 'index.html' ? tagName : fileName}`;
116-
ctx.redirect(redir);
117-
}
118-
return next();
119-
})
120-
// redirect /components/jazz-hands/pf-jazz-hands-lightdom.css to /elements/pf-jazz-hands/pf-jazz-hands-lightdom.css
121-
.get('/components/:slug/demo/:sub?/:fileName.css', (ctx, next) => {
122-
// FIXME: will probably break if one component links to another's lightdom css.
123-
// better to find out why it's requesting from /components/ in the first place
124-
const { slug, fileName } = ctx.params;
125-
const tagName = deslugify(slug);
126-
if (tagName && fileName.includes('-lightdom')) {
127-
return ctx.redirect(`/elements/${tagName}/${fileName}.css`);
128-
} else {
97+
const { elementsDir, tagPrefix } = options;
98+
const { componentSubpath } = options.site;
99+
const router =
100+
new Router()
101+
.get(/\/pf-icon\/icons\/.*\.js$/, (ctx, next) => {
102+
ctx.type = 'application/javascript';
129103
return next();
130-
}
131-
})
132-
// redirect /components/jazz-hands/demo/demo.css to /elements/pf-jazz-hands/demo/demo.css
133-
// redirect /components/jazz-hands/demo/special-demo/demo.css to /elements/pf-jazz-hands/demo/demo.css
134-
.get('/components/:slug/demo/:sub?/:fileName.:ext', (ctx, next) => {
135-
// FIXME: will probably break if one component links to another's lightdom css.
136-
// better to find out why it's requesting from /components/ in the first place
137-
const { slug, fileName, ext } = ctx.params;
138-
const tagName = deslugify(slug);
139-
const lastDir = ctx.originalUrl.split('/').at(-2);
140-
if (tagName && lastDir !== 'demo') {
141-
return ctx.redirect(`/elements/${tagName}/demo/${fileName}.${ext}`);
142-
} else {
143-
return next();
144-
}
145-
})
146-
.routes());
147-
104+
})
105+
.get('/tools/pfe-tools/environment.js(.js)?', async ctx => {
106+
ctx.body = await makeDemoEnv(options.rootDir);
107+
ctx.type = 'application/javascript';
108+
})
109+
// Redirect `components/jazz-hands/*.js` to `components/pf-jazz-hands/*.ts`
110+
.get(`/${componentSubpath}/:element/:fileName.js`, async ctx => {
111+
const { element, fileName } = ctx.params;
112+
ctx.redirect(`/${elementsDir}/${element}/${fileName}.ts`);
113+
})
114+
// Redirect `elements/jazz-hands/*.js` to `elements/pf-jazz-hands/*.ts`
115+
.get(`/${elementsDir}/:element/:fileName.js`, async ctx => {
116+
const { element, fileName } = ctx.params;
117+
ctx.redirect(`/${elementsDir}/${element}/${fileName}.ts`);
118+
})
119+
// Redirect `components/jazz-hands/demo/*.js|css` to `components/pf-jazz-hands/demo/*.js|css`
120+
.get(`/${componentSubpath}/:element/demo/:demoSubDir?/:fileName.:ext`, async (ctx, next) => {
121+
const { element, fileName, ext } = ctx.params;
122+
if (!element.includes(tagPrefix)) {
123+
ctx.redirect(`/${elementsDir}/${tagPrefix}-${element}/demo/${fileName}.${ext}`);
124+
} else {
125+
return next();
126+
}
127+
})
128+
// Redirect `components/jazz-hands/*` to `components/pf-jazz-hands/*` for requests not previously handled
129+
.get(`/${componentSubpath}/:element/:splatPath*`, async (ctx, next) => {
130+
const { element, splatPath } = ctx.params;
131+
if (splatPath.includes('demo')) {
132+
/* if its the demo directory return */
133+
return next();
134+
}
135+
if (!element.includes(tagPrefix)) {
136+
ctx.redirect(`/${elementsDir}/${tagPrefix}-${element}/${splatPath}`);
137+
} else {
138+
return next();
139+
}
140+
});
141+
app.use(router.routes());
148142
const files = await glob(options.watchFiles, { cwd: process.cwd() });
149143
for (const file of files) {
150144
fileWatcher.add(file);

0 commit comments

Comments
 (0)