Skip to content

Commit 6036ee9

Browse files
committed
feat(config-utils): enable webpack public path auto setting
1 parent 5f74571 commit 6036ee9

9 files changed

+73
-2
lines changed

packages/config-utils/src/feo/feo-types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ export type CRDObject = {
125125
serviceTiles?: ServiceTile[];
126126
widgetRegistry?: ChromeWidgetEntry[];
127127
feoConfigEnabled?: boolean;
128+
frontend: {
129+
paths: string[];
130+
};
128131
};
129132
};
130133

packages/config-utils/src/feo/module-interceptor.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('module-interceptor', () => {
1414
name: moduleName,
1515
},
1616
spec: {
17+
frontend: {
18+
paths: ['/'],
19+
},
1720
module: newEntry,
1821
},
1922
},
@@ -44,6 +47,9 @@ describe('module-interceptor', () => {
4447
name: moduleName,
4548
},
4649
spec: {
50+
frontend: {
51+
paths: ['/'],
52+
},
4753
module: newEntry,
4854
},
4955
},

packages/config-utils/src/feo/navigation-interceptor.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe('NavigationInterceptor', () => {
1919
name: frontendName,
2020
},
2121
spec: {
22+
frontend: {
23+
paths: ['/'],
24+
},
2225
module: {
2326
manifestLocation: 'http://localhost:3000/manifest.json',
2427
},
@@ -207,6 +210,9 @@ describe('NavigationInterceptor', () => {
207210
name: defaultFrontendName,
208211
},
209212
spec: {
213+
frontend: {
214+
paths: ['/'],
215+
},
210216
module: {
211217
manifestLocation: 'http://localhost:3000/manifest.json',
212218
},
@@ -275,6 +281,9 @@ describe('NavigationInterceptor', () => {
275281
name: defaultFrontendName,
276282
},
277283
spec: {
284+
frontend: {
285+
paths: ['/'],
286+
},
278287
module: {
279288
manifestLocation: 'http://localhost:3000/manifest.json',
280289
},
@@ -367,6 +376,9 @@ describe('NavigationInterceptor', () => {
367376
name: defaultFrontendName,
368377
},
369378
spec: {
379+
frontend: {
380+
paths: ['/'],
381+
},
370382
module: {
371383
manifestLocation: 'http://localhost:3000/manifest.json',
372384
},
@@ -433,6 +445,9 @@ describe('NavigationInterceptor', () => {
433445
name: defaultFrontendName,
434446
},
435447
spec: {
448+
frontend: {
449+
paths: ['/'],
450+
},
436451
module: {
437452
manifestLocation: 'http://localhost:3000/manifest.json',
438453
},
@@ -500,6 +515,9 @@ describe('NavigationInterceptor', () => {
500515
name: frontendName,
501516
},
502517
spec: {
518+
frontend: {
519+
paths: ['/'],
520+
},
503521
module: {
504522
manifestLocation: 'http://localhost:3000/manifest.json',
505523
},

packages/config-utils/src/feo/search-interceptor.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('SearchInterceptor', () => {
1111
name: frontendName,
1212
},
1313
spec: {
14+
frontend: {
15+
paths: ['/'],
16+
},
1417
module: {
1518
manifestLocation: 'location',
1619
},

packages/config-utils/src/feo/service-tiles-interceptor.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('Service tiles interceptor', () => {
1111
name: frontendName,
1212
},
1313
spec: {
14+
frontend: {
15+
paths: ['/'],
16+
},
1417
module: {
1518
manifestLocation: 'location',
1619
},

packages/config-utils/src/feo/spec/frontend-crd.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"items": {
5252
"type": "string"
5353
},
54-
"minItems": 1
54+
"minItems": 1,
55+
"maxItems": 1
5556
}
5657
},
5758
"required": [

packages/config-utils/src/feo/validate-frontend-crd.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ describe('Validate FrontEnd CRD', () => {
9393
`Frontend CRD validation failed! must NOT have additional properties, must NOT have additional properties, must match exactly one schema in oneOf`
9494
);
9595
});
96+
97+
test('should only allow one frontend.paths entry', () => {
98+
const crd = cloneDeep(crdBase) as FrontendCRD;
99+
crd.objects[0].spec.frontend.paths = ['/foo/bar', '/baz'];
100+
expect(() => validateFrontEndCrd(crd)).toThrowError(`must NOT have more than 1 items`);
101+
});
96102
});

packages/config-utils/src/feo/widget-registry-interceptor.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ describe('Widget registry interceptor', () => {
1616
name: 'name',
1717
},
1818
spec: {
19+
frontend: {
20+
paths: ['/'],
21+
},
1922
module: {
2023
manifestLocation: 'location',
2124
},

packages/config-utils/src/serve-federated.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import path from 'path';
22
import fs from 'fs';
33
import concurrently from 'concurrently';
4+
import { hasFEOFeaturesEnabled, readFrontendCRD } from './feo/crd-check';
5+
import { FrontendCRD } from './feo/feo-types';
6+
import { LogType, fecLogger } from '.';
47

58
function federate(argv: Record<string, any>, cwd: string) {
69
let configPath: string = argv.config || './node_modules/@redhat-cloud-services/frontend-components-config/bin/prod.webpack.config.js';
@@ -12,20 +15,45 @@ function federate(argv: Record<string, any>, cwd: string) {
1215
}
1316

1417
configPath = path.join(cwd, configPath);
18+
const fecConfig = require(process.env.FEC_CONFIG_PATH!);
19+
const { insights } = require(`${cwd}/package.json`);
20+
const frontendCRDPath = fecConfig.frontendCRDPath ?? path.resolve(process.cwd(), 'deploy/frontend.yaml');
1521

22+
const frontendCrdRef: { current?: FrontendCRD } = { current: undefined };
23+
let FEOFeaturesEnabled = false;
24+
try {
25+
frontendCrdRef.current = readFrontendCRD(frontendCRDPath);
26+
FEOFeaturesEnabled = hasFEOFeaturesEnabled(frontendCrdRef.current);
27+
} catch (e) {
28+
fecLogger(
29+
LogType.warn,
30+
`FEO features are not enabled. Unable to find frontend CRD file at ${frontendCRDPath}. If you want FEO features for local development, make sure to have a "deploy/frontend.yaml" file in your project or specify its location via "frontendCRDPath" attribute.`
31+
);
32+
}
1633
try {
1734
fs.statSync(configPath);
1835
// eslint-disable-next-line @typescript-eslint/no-var-requires
1936
let config = require(configPath);
2037
if (typeof config === 'function') {
2138
config = config(process.env);
2239
}
40+
// take the CDN path from the config or generate it based on the deployment
41+
let cdnPath: string;
42+
// Could be written on a single line, but this is nice and readable
43+
if (FEOFeaturesEnabled && fecConfig.publicPath === 'auto' && frontendCrdRef.current) {
44+
// All service should eventually use this path
45+
cdnPath = `${frontendCrdRef.current?.objects[0]?.spec.frontend.paths[0]}/`.replace(/\/\//, '/');
46+
} else if (fecConfig.publicPath === 'auto') {
47+
cdnPath = `/${fecConfig.deployment || 'apps'}/${insights.appname}/`;
48+
} else {
49+
cdnPath = config.output.publicPath;
50+
}
2351

2452
Promise.resolve(config).then((config) => {
2553
const outputPath = config.output.path;
2654

2755
concurrently([
28-
`${WEBPACK_PATH} --config ${configPath} --watch --output-path ${path.join(outputPath, config.output.publicPath)}`,
56+
`${WEBPACK_PATH} --config ${configPath} --watch --output-path ${path.join(outputPath, cdnPath)}`,
2957
`${HTTP_SERVER_PATH} ${outputPath} -p ${argv.port || 8003} -c-1 -a :: --cors=*`,
3058
]);
3159
});

0 commit comments

Comments
 (0)