@@ -86,6 +86,26 @@ async function renderURL(context: Context, options: PfeDevServerInternalConfig):
8686 }
8787}
8888
89+ function kebabCase ( string : string ) {
90+ return string
91+ . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1-$2' ) // Replace capital letters with lowercase
92+ . replace ( / [ \s _ ] + / g, '-' ) // Replace spaces and underscores with -
93+ . replace ( / [ ^ - a - z A - Z ] / g, '' ) // Remove all chars not letters, numbers or -
94+ . toLowerCase ( ) ;
95+ }
96+
97+ function convertAliases ( aliases : Record < string , string > , tagPrefix : string ) {
98+ const keyedAliases = { } as Record < string , string > ;
99+ for ( const key in aliases ) {
100+ if ( { } . hasOwnProperty . call ( aliases , key ) ) {
101+ const newKey = kebabCase ( aliases [ key ] ) ;
102+ const preFixedKey = `${ tagPrefix } -${ newKey } ` ;
103+ keyedAliases [ preFixedKey ] = key ;
104+ }
105+ }
106+ return keyedAliases ;
107+ }
108+
89109/**
90110 * Generate HTML for each component by rendering a nunjucks template
91111 * Watch repository source files and reload the page when they change
@@ -94,8 +114,18 @@ function pfeDevServerPlugin(options: PfeDevServerInternalConfig): Plugin {
94114 return {
95115 name : 'pfe-dev-server' ,
96116 async serverStart ( { fileWatcher, app } ) {
97- const { elementsDir, tagPrefix } = options ;
117+ const { elementsDir, tagPrefix, aliases } = options ;
98118 const { componentSubpath } = options . site ;
119+
120+ const keyedAliases = convertAliases ( aliases , tagPrefix ) ;
121+
122+ const prefixTag = ( tag : string ) => {
123+ if ( ! tag . startsWith ( tagPrefix ) ) {
124+ return `${ tagPrefix } -${ tag } ` ;
125+ }
126+ return tag ;
127+ } ;
128+
99129 const router =
100130 new Router ( )
101131 . get ( / \/ p f - i c o n \/ i c o n s \/ .* \. j s $ / , ( ctx , next ) => {
@@ -109,21 +139,29 @@ function pfeDevServerPlugin(options: PfeDevServerInternalConfig): Plugin {
109139 // Redirect `components/jazz-hands/*.js` to `components/pf-jazz-hands/*.ts`
110140 . get ( `/${ componentSubpath } /:element/:fileName.js` , async ctx => {
111141 const { element, fileName } = ctx . params ;
112- ctx . redirect ( `/${ elementsDir } /${ element } /${ fileName } .ts` ) ;
142+
143+ let prefixedElement = prefixTag ( element ) ;
144+ prefixedElement = keyedAliases [ prefixedElement ] ?? prefixedElement ;
145+
146+ ctx . redirect ( `/${ elementsDir } /${ prefixedElement } /${ fileName } .ts` ) ;
113147 } )
114148 // Redirect `elements/jazz-hands/*.js` to `elements/pf-jazz-hands/*.ts`
115149 . get ( `/${ elementsDir } /:element/:fileName.js` , async ctx => {
116150 const { element, fileName } = ctx . params ;
117- ctx . redirect ( `/${ elementsDir } /${ element } /${ fileName } .ts` ) ;
151+
152+ let prefixedElement = prefixTag ( element ) ;
153+ prefixedElement = keyedAliases [ prefixedElement ] ?? prefixedElement ;
154+
155+ ctx . redirect ( `/${ elementsDir } /${ prefixedElement } /${ fileName } .ts` ) ;
118156 } )
119157 // Redirect `components/pf-jazz-hands|jazz-hands/demo/*-lightdom.css` to `components/pf-jazz-hands/*-lightdom.css`
120158 // Redirect `components/jazz-hands/demo/*.js|css` to `components/pf-jazz-hands/demo/*.js|css`
121159 . get ( `/${ componentSubpath } /:element/demo/:demoSubDir?/:fileName.:ext` , async ( ctx , next ) => {
122160 const { element, fileName, ext } = ctx . params ;
123- let prefixedElement = element ;
124- if ( ! element . includes ( tagPrefix ) ) {
125- prefixedElement = ` ${ tagPrefix } - ${ element } ` ;
126- }
161+
162+ let prefixedElement = prefixTag ( element ) ;
163+ prefixedElement = keyedAliases [ prefixedElement ] ?? prefixedElement ;
164+
127165 if ( fileName . includes ( '-lightdom' ) && ext === 'css' ) {
128166 ctx . redirect ( `/${ elementsDir } /${ prefixedElement } /${ fileName } .${ ext } ` ) ;
129167 } else if ( ! element . includes ( tagPrefix ) ) {
0 commit comments