Skip to content

Commit b5d4079

Browse files
committed
fix(tools): try to get repo root automatically
1 parent 25feda9 commit b5d4079

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

.changeset/fifty-boxes-peel.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+
try to guess repository root dir when computing dev server config

tools/pfe-tools/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,23 @@ Helpers for testing web components using [web test runner](https://modern-web.de
2323
## dev-server
2424

2525
Preset [web-dev-server](https://modern-web.dev/docs/dev-server/overview/) configuration.
26+
27+
### Troubleshooting
28+
29+
> I ran `npm start` but get `404 not found` when the dev server launches the browser
30+
31+
The dev server config in pfe-tools tries its best to find the root directory of your project,
32+
but there are cases where this may not work. If you get a 404 error to index.html,
33+
34+
1. Confirm that you have an `index.html` file in your repository root
35+
2. Set the `rootDir` option to `pfeDevServerConfig`, e.g.
36+
```js
37+
// web-dev-server.config.js
38+
import { pfeDevServerConfig } from '@patternfly/pfe-tools/dev-server.js';
39+
40+
export default pfeDevServerConfig({
41+
rootDir: '.',
42+
});
43+
```
44+
45+
Make sure to do the same in `web-test-runner.config.js` as well, for your unit tests

tools/pfe-tools/dev-server.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,15 @@ import { esbuildPlugin } from '@web/dev-server-esbuild';
1313
import { importMapsPlugin } from '@web/dev-server-import-maps';
1414
import { 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

2523
const litcss = fromRollup(litcssRollup);
2624

27-
const rootDir = fileURLToPath(new URL('../..', import.meta.url));
28-
2925
function 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
*/
3733
function 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
*/
7169
export 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(/\/node_modules$/, '');
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

Comments
 (0)