@@ -13,19 +13,15 @@ import { esbuildPlugin } from '@web/dev-server-esbuild';
1313import { importMapsPlugin } from '@web/dev-server-import-maps' ;
1414import { transformSass } from './esbuild.js' ;
1515
16- export interface PfeDevServerConfigOptions {
16+ export interface PfeDevServerConfigOptions extends DevServerConfig {
1717 /** Extra dev server plugins */
1818 plugins ?: Plugin [ ] ;
19- /** repository root (default: process.cwd()) */
20- cwd ?: string ;
2119 importMap ?: InjectSetting [ 'importMap' ] ;
2220 hostname ?: string ;
2321}
2422
2523const litcss = fromRollup ( litcssRollup ) ;
2624
27- const rootDir = fileURLToPath ( new URL ( '../..' , import . meta. url ) ) ;
28-
2925function appendLines ( body : string , ...lines : string [ ] ) : string {
3026 return [ body , ...lines ] . join ( '\n' ) ;
3127}
@@ -35,13 +31,15 @@ function appendLines(body: string, ...lines: string[]): string {
3531 * exposed data is available by importing `@patternfly/pfe-tools/environment.js`.
3632 */
3733function bindNodeDataToBrowser ( options ?: PfeDevServerConfigOptions ) : Plugin {
38- const { cwd = process . cwd ( ) } = options ?? { } ;
34+ const { rootDir = process . cwd ( ) } = options ?? { } ;
3935 return {
4036 name : 'bind-node-data-to-browser' ,
4137 async transform ( context ) {
4238 if ( context . path . endsWith ( 'pfe-tools/environment.js' ) ) {
43- const elements = await readdir ( join ( cwd , 'elements' ) ) ;
44- const core = await readdir ( join ( cwd , 'core' ) ) ;
39+ // TODO: calculate export names from npm workspaces
40+ // for now, `elements` is the only conventionally-supported workspace
41+ const elements = await readdir ( join ( rootDir , 'elements' ) ) ;
42+ const core = await readdir ( join ( rootDir , 'core' ) ) ;
4543 const transformed = appendLines (
4644 context . body as string ,
4745 `export const elements = ${ JSON . stringify ( elements ) } ;` ,
@@ -69,7 +67,16 @@ function scssMimeType(): Plugin {
6967 * Creates a default config for PFE's dev server.
7068 */
7169export function pfeDevServerConfig ( options ?: PfeDevServerConfigOptions ) : DevServerConfig {
72- const cwd = options ?. cwd ?? process . cwd ( ) ;
70+ /**
71+ * Plain case: this file is running from `/node_modules/@patternfly/pfe-tools`.
72+ * two dirs up from here is `node_modules`, so we just shear it clean off the path string
73+ * Other case: this file is running in the `patternfly/patternfly-elements` monorepo
74+ * two dirs up from here is the project root. There is no `/node_modules$` to replace,
75+ * so we get the correct path
76+ * Edge/Corner cases: all other cases must set the `rootDir` option themselves so as to avoid 404s
77+ */
78+ const rootDir = options ?. rootDir ?? fileURLToPath ( new URL ( '../..' , import . meta. url ) )
79+ . replace ( / \/ n o d e _ m o d u l e s $ / , '' ) ;
7380
7481 /** Default set of import maps */
7582 const imports = {
@@ -87,20 +94,25 @@ export function pfeDevServerConfig(options?: PfeDevServerConfigOptions): DevServ
8794 } ;
8895
8996 return {
90- ...options ?. hostname && { hostname : options ?. hostname } ,
97+ appIndex : 'index.html' ,
98+
9199 nodeResolve : {
92100 exportConditions : [ 'esbuild' , 'import' , 'default' ] ,
93101 extensions : [ '.ts' , '.mjs' , '.js' , '.scss' ] ,
94102 } ,
103+
95104 rootDir,
96- appIndex : 'index.html' ,
105+
97106 ...options ?? { } ,
107+
98108 middleware : [
99- function rewriteIndex ( context , next ) {
109+ function cors ( context , next ) {
100110 context . set ( 'Access-Control-Allow-Origin' , '*' ) ;
101111 return next ( ) ;
102112 } ,
113+ ...options ?. middleware ?? [ ] ,
103114 ] ,
115+
104116 plugins : [
105117 ...options ?. plugins ?? [ ] ,
106118 scssMimeType ( ) ,
@@ -110,7 +122,7 @@ export function pfeDevServerConfig(options?: PfeDevServerConfigOptions): DevServ
110122 // bind data from node to the browser
111123 // e.g. the file listing of the `elements/` dir
112124 // so that we can list the elements by name
113- bindNodeDataToBrowser ( { cwd } ) ,
125+ bindNodeDataToBrowser ( { rootDir } ) ,
114126 // load .scss files as lit CSSResult modules
115127 litcss ( { include : [ '**/*.scss' ] , transform : transformSass } ) ,
116128 ] ,
0 commit comments