Skip to content

Commit 30d97c2

Browse files
authored
Merge pull request #2190 from Hyperkid123/enable-public-path-auto
Enable public path auto
2 parents d7ab0cb + 11f50be commit 30d97c2

File tree

7 files changed

+57
-34
lines changed

7 files changed

+57
-34
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/components/src/ConditionalFilter/ConditionalFilter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const ConditionalFilter: React.FunctionComponent<ConditionalFilterProps> = ({
155155
const C = typeMapper.custom;
156156
// make sure no invalid props are passed to the Fragment element which is mapped to the custom component
157157
const { key, children } = activeItem.filterValues;
158-
return <C key={key} children={children} />;
158+
return <C key={key}>{children}</C>;
159159
} else {
160160
throw new Error(`Invalid conditional filter component type! Expected one of ${Object.keys(conditionalFilterType)}, got ${activeItem.type}.`);
161161
}

packages/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"scripts": {},
2828
"dependencies": {
2929
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.15",
30-
"@redhat-cloud-services/frontend-components-config-utilities": "^4.1.6",
30+
"@redhat-cloud-services/frontend-components-config-utilities": "^4.2.0",
3131
"@redhat-cloud-services/tsc-transform-imports": "^1.0.21",
3232
"@swc/core": "^1.3.76",
3333
"assert": "^2.0.0",

packages/config/src/bin/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export function validateFECConfig(cwd: string) {
1717
fecLogger(LogType.error, 'Missing config "appUrl" in fec.config.js');
1818
throw 'fec.config.js validation failed, missing "appUrl" config';
1919
}
20+
21+
if (config.publicPath && config.publicPath !== 'auto') {
22+
fecLogger(LogType.error, 'Invalid config "publicPath" in fec.config.js, must be empty or set to "auto"');
23+
throw 'fec.config.js validation failed, "publicPath" must be empty or set to "auto"';
24+
}
2025
process.env.FEC_CONFIG_PATH = configPath;
2126
}
2227

packages/config/src/lib/config.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('should create dummy config with no options', () => {
1010
appEntry: '/foo/bar',
1111
appName: 'Fooapp',
1212
env: 'stage-stable',
13-
publicPath: 'foo/bar',
1413
frontendCRDPath: crdMockPath,
1514
});
1615

@@ -35,7 +34,6 @@ describe('should create dummy config with no options', () => {
3534
expect(output).toEqual({
3635
filename: expect.stringMatching(/js\/\[name\]\.\[contenthash\]\.js/),
3736
path: '/dist',
38-
publicPath: 'foo/bar',
3937
chunkFilename: expect.stringMatching(/js\/\[name\]\.\[contenthash\]\.js/),
4038
});
4139
});
@@ -95,10 +93,13 @@ test('appEntry correctly set', () => {
9593
});
9694

9795
describe('publicPath', () => {
98-
const { output } = configBuilder({ publicPath: 'test-value' });
99-
100-
test('output', () => {
101-
expect(output.publicPath).toBe('test-value');
96+
test('should ignore unknown public path', () => {
97+
const { output } = configBuilder({ publicPath: 'test-value' });
98+
expect(output.publicPath).toBe(undefined);
99+
});
100+
test('should propagate public path auto', () => {
101+
const { output } = configBuilder({ publicPath: 'auto' });
102+
expect(output.publicPath).toBe('auto');
102103
});
103104
});
104105

packages/config/src/lib/createConfig.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path');
2-
const fs = require('fs');
32
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
43
import searchIgnoredStyles from '@redhat-cloud-services/frontend-components-config-utilities/search-ignored-styles';
54

@@ -22,7 +21,8 @@ export interface CommonConfigOptions {
2221
export type FrontendEnv = 'stage-stable' | 'prod-stable';
2322
export interface CreateConfigOptions extends CommonConfigOptions {
2423
port?: number;
25-
publicPath: string;
24+
publicPath?: 'auto';
25+
cdnPath: string;
2626
appEntry: string;
2727
https?: boolean;
2828
mode?: Configuration['mode'];
@@ -61,6 +61,7 @@ export interface CreateConfigOptions extends CommonConfigOptions {
6161

6262
export const createConfig = ({
6363
port,
64+
cdnPath,
6465
publicPath,
6566
appEntry,
6667
rootFolder,
@@ -114,15 +115,6 @@ export const createConfig = ({
114115

115116
const outputPath = `${rootFolder || ''}/dist`;
116117

117-
const copyTemplate = (chromePath: string) => {
118-
const template = fs.readFileSync(`${chromePath}/index.html`, { encoding: 'utf-8' });
119-
if (!fs.existsSync(outputPath)) {
120-
fs.mkdirSync(outputPath);
121-
}
122-
123-
fs.writeFileSync(`${outputPath}/index.html`, template);
124-
};
125-
126118
const devServerPort = typeof port === 'number' ? port : useProxy || standalone ? 1337 : 8002;
127119
return {
128120
mode: mode || (isProd ? 'production' : 'development'),
@@ -150,7 +142,7 @@ export const createConfig = ({
150142
output: {
151143
filename: filenameMask,
152144
path: outputPath,
153-
publicPath,
145+
publicPath: publicPath === 'auto' ? publicPath : cdnPath,
154146
chunkFilename: filenameMask,
155147
},
156148
...(internalHotReload
@@ -299,6 +291,7 @@ export const createConfig = ({
299291
},
300292
devMiddleware: {
301293
writeToDisk: true,
294+
publicPath: cdnPath,
302295
},
303296
client,
304297
...proxy({
@@ -314,7 +307,7 @@ export const createConfig = ({
314307
port: devServerPort,
315308
reposDir,
316309
appUrl,
317-
publicPath,
310+
publicPath: cdnPath,
318311
proxyVerbose,
319312
target,
320313
registry,

packages/config/src/lib/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { LogType, fecLogger } from '@redhat-cloud-services/frontend-components-config-utilities';
2+
import path from 'path';
23
import createConfig, { CreateConfigOptions } from './createConfig';
34
import createPlugins, { CreatePluginsOptions } from './createPlugins';
5+
import { hasFEOFeaturesEnabled, readFrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/crd-check';
6+
import { FrontendCRD } from '@redhat-cloud-services/frontend-components-config-utilities/feo/feo-types';
47
const { sync } = require('glob');
58
export * from './createConfig';
69
export * from './createPlugins';
@@ -29,12 +32,13 @@ const getAppEntry = (rootFolder: string, isProd?: boolean) => {
2932
return `${rootFolder}/${entries[0]}`;
3033
};
3134

32-
type FecConfigurationOptions = Omit<CreateConfigOptions, 'publicPath' | 'appEntry' | 'appName'> &
33-
CreatePluginsOptions & {
35+
type FecConfigurationOptions = Omit<CreateConfigOptions, 'publicPath' | 'appEntry' | 'appName' | 'cdnPath'> &
36+
Omit<CreatePluginsOptions, 'cdnPath'> & {
3437
deployment?: string;
3538
debug?: boolean;
3639
appEntry?: string;
3740
blockLegacyChrome?: boolean;
41+
publicPath?: 'auto';
3842
};
3943

4044
const createFecConfig = (
@@ -54,8 +58,27 @@ const createFecConfig = (
5458
gitBranch = 'main';
5559
}
5660
const appDeployment = typeof configurations.deployment === 'string' ? configurations.deployment : configurations.deployment || 'apps';
61+
const { frontendCRDPath = path.resolve(configurations.rootFolder, 'deploy/frontend.yaml') } = configurations;
5762

58-
const publicPath = `/${appDeployment}/${insights.appname}/`;
63+
const frontendCrdRef: { current?: FrontendCRD } = { current: undefined };
64+
let FEOFeaturesEnabled = false;
65+
try {
66+
frontendCrdRef.current = readFrontendCRD(frontendCRDPath);
67+
FEOFeaturesEnabled = hasFEOFeaturesEnabled(frontendCrdRef.current);
68+
} catch (e) {
69+
fecLogger(
70+
LogType.warn,
71+
`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.`
72+
);
73+
}
74+
let cdnPath: string;
75+
// Could be written on a single line, but this is nice and readable
76+
if (FEOFeaturesEnabled && configurations.publicPath === 'auto' && frontendCrdRef.current) {
77+
// All service should eventually use this path
78+
cdnPath = `${frontendCrdRef.current?.objects[0]?.spec.frontend.paths[0]}/`.replace(/\/\//, '/');
79+
} else {
80+
cdnPath = `/${appDeployment}/${insights.appname}/`;
81+
}
5982
const appEntry = configurations.appEntry || getAppEntry(configurations.rootFolder, isProd);
6083
const generateSourceMaps = !isProd;
6184

@@ -66,7 +89,7 @@ const createFecConfig = (
6689
fecLogger(LogType.debug, `Current branch: ${gitBranch}`);
6790
!generateSourceMaps && fecLogger(LogType.debug, `Source map generation for "${gitBranch}" deployment has been disabled.`);
6891
fecLogger(LogType.debug, `Using deployments: ${appDeployment}`);
69-
fecLogger(LogType.debug, `Public path: ${publicPath}`);
92+
fecLogger(LogType.debug, `CDN path: ${cdnPath}`);
7093
fecLogger(LogType.debug, `App entry: ${appEntry}`);
7194
fecLogger(LogType.debug, `Use proxy: ${configurations.useProxy ? 'true' : 'false'}`);
7295
if (!(configurations.useProxy || configurations.standalone)) {
@@ -81,7 +104,8 @@ const createFecConfig = (
81104
return {
82105
config: createConfig({
83106
...configurations,
84-
publicPath,
107+
cdnPath,
108+
publicPath: configurations.publicPath,
85109
appEntry,
86110
appName: insights.appname,
87111
}),

0 commit comments

Comments
 (0)