@@ -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 ( / \/ p f - i c o n \/ i c o n s \/ .* \. j s $ / , ( 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 ( / \/ p f - i c o n \/ i c o n s \/ .* \. j s $ / , ( 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