Skip to content

Commit 668366a

Browse files
committed
Create overridable constants file
1 parent 836a427 commit 668366a

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

packages/telemetry/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"@firebase/app": "0.14.6",
111111
"@opentelemetry/sdk-trace-web": "2.1.0",
112112
"@rollup/plugin-json": "6.1.0",
113+
"@rollup/plugin-replace": "6.0.2",
113114
"@testing-library/dom": "10.4.1",
114115
"@testing-library/react": "16.3.0",
115116
"@types/react": "19.1.13",

packages/telemetry/rollup.config.js

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,30 @@
1717

1818
import json from '@rollup/plugin-json';
1919
import copy from 'rollup-plugin-copy';
20+
import replacePlugin from '@rollup/plugin-replace';
2021
import typescriptPlugin from 'rollup-plugin-typescript2';
2122
import typescript from 'typescript';
2223
import pkg from './package.json';
2324
import { emitModulePackageFile } from '../../scripts/build/rollup_emit_module_package_file';
2425

25-
const deps = Object.keys(
26-
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
27-
);
26+
const deps = [
27+
...Object.keys(Object.assign({}, pkg.peerDependencies, pkg.dependencies)),
28+
'./constants/auto-constants',
29+
];
30+
31+
function replaceSource(path) {
32+
return replacePlugin({
33+
'./src/constants/auto-constants': `'${path}'`,
34+
'../constants/auto-constants': `'${path}'`,
35+
delimiters: ["'", "'"],
36+
preventAssignment: true
37+
});
38+
}
2839

29-
const buildPlugins = [typescriptPlugin({ typescript }), json()];
40+
const buildPlugins = [
41+
typescriptPlugin({ typescript }),
42+
json(),
43+
];
3044

3145
const browserBuilds = [
3246
{
@@ -36,7 +50,7 @@ const browserBuilds = [
3650
format: 'es',
3751
sourcemap: true
3852
},
39-
plugins: buildPlugins,
53+
plugins: [...buildPlugins, replaceSource('./auto-constants.js')],
4054
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
4155
},
4256
{
@@ -46,7 +60,7 @@ const browserBuilds = [
4660
format: 'cjs',
4761
sourcemap: true
4862
},
49-
plugins: buildPlugins,
63+
plugins: [...buildPlugins, replaceSource('./auto-constants.js')],
5064
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
5165
}
5266
];
@@ -59,7 +73,7 @@ const nodeBuilds = [
5973
format: 'cjs',
6074
sourcemap: true
6175
},
62-
plugins: buildPlugins,
76+
plugins: [...buildPlugins, replaceSource('./auto-constants.js')],
6377
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
6478
},
6579
{
@@ -69,7 +83,7 @@ const nodeBuilds = [
6983
format: 'es',
7084
sourcemap: true
7185
},
72-
plugins: [...buildPlugins, emitModulePackageFile()],
86+
plugins: [...buildPlugins, emitModulePackageFile(), replaceSource('../auto-constants.js')],
7387
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
7488
}
7589
];
@@ -100,7 +114,8 @@ const reactBuilds = [
100114
dest: 'dist'
101115
}
102116
]
103-
})
117+
}),
118+
replaceSource('../auto-constants.js')
104119
],
105120
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
106121
},
@@ -117,7 +132,8 @@ const reactBuilds = [
117132
typescript,
118133
tsconfig: 'tsconfig.react.json'
119134
}),
120-
json()
135+
json(),
136+
replaceSource('../auto-constants.js')
121137
],
122138
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
123139
}
@@ -148,7 +164,8 @@ const angularBuilds = [
148164
dest: 'dist'
149165
}
150166
]
151-
})
167+
}),
168+
replaceSource('../auto-constants.js')
152169
],
153170
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
154171
},
@@ -164,15 +181,28 @@ const angularBuilds = [
164181
typescript,
165182
tsconfig: 'tsconfig.angular.json'
166183
}),
167-
json()
184+
json(),
185+
replaceSource('../auto-constants.js')
168186
],
169187
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
170188
}
171189
];
172190

191+
const autoinitBuild = [
192+
{
193+
input: './src/constants/auto-constants.ts',
194+
output: {
195+
file: './dist/auto-constants.js',
196+
format: 'cjs'
197+
},
198+
plugins: buildPlugins
199+
},
200+
];
201+
173202
export default [
174203
...browserBuilds,
175204
...nodeBuilds,
176205
...reactBuilds,
177-
...angularBuilds
206+
...angularBuilds,
207+
...autoinitBuild
178208
];

packages/telemetry/src/api.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Provider } from '@firebase/component';
2222
import { AnyValueMap, SeverityNumber } from '@opentelemetry/api-logs';
2323
import { trace } from '@opentelemetry/api';
2424
import { TelemetryService } from './service';
25+
import { getAppVersion } from './helpers';
2526

2627
declare module '@firebase/component' {
2728
interface NameServiceMapping {
@@ -93,12 +94,7 @@ export function captureError(
9394
}
9495

9596
// Add app version metadata
96-
let appVersion = 'unset';
97-
// TODO: implement app version fallback logic
98-
if ((telemetry as TelemetryService).options?.appVersion) {
99-
appVersion = (telemetry as TelemetryService).options!.appVersion!;
100-
}
101-
customAttributes['app.version'] = appVersion;
97+
customAttributes['app.version'] = getAppVersion(telemetry);
10298

10399
if (error instanceof Error) {
104100
logger.emit({
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const AUTO_APP_VERSION = '';

packages/telemetry/src/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as constants from './constants/auto-constants';
2+
import { Telemetry } from './public-types';
3+
import { TelemetryService } from './service';
4+
5+
export function getAppVersion(telemetry: Telemetry): string {
6+
if ((telemetry as TelemetryService).options?.appVersion) {
7+
return (telemetry as TelemetryService).options!.appVersion!;
8+
} else if (constants.AUTO_APP_VERSION) {
9+
return constants.AUTO_APP_VERSION;
10+
}
11+
return 'unset';
12+
}

0 commit comments

Comments
 (0)