Skip to content

Commit 229d8f1

Browse files
perf: optimize analytics app imports to avoid loading entire app store
- Add analytics service generation to app-store-cli build process - Generate analytics.services.generated.ts with only analytics apps (dub) - Update getAnalytics.ts to use AnalyticsServiceMap instead of full appStore - Add NEXT_PUBLIC_IS_E2E to turbo.json globalEnv for generated files - Reduces import footprint from 100+ apps to only analytics apps with AnalyticsService - Follows same pattern as calendar services optimization from PR #22450 Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>
1 parent 1cc5482 commit 229d8f1

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

packages/app-store-cli/src/build.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,43 @@ function generateFiles() {
378378
calendarOutput.push(...calendarServices);
379379
}
380380

381+
const analyticsOutput = [];
382+
const analyticsServices = getExportedObject(
383+
"AnalyticsServiceMap",
384+
{
385+
importConfig: {
386+
fileToBeImported: "lib/AnalyticsService.ts",
387+
importName: "default",
388+
},
389+
lazyImport: true,
390+
},
391+
(app: App) => {
392+
const hasAnalyticsService = fs.existsSync(
393+
path.join(APP_STORE_PATH, app.path, "lib/AnalyticsService.ts")
394+
);
395+
return hasAnalyticsService;
396+
}
397+
);
398+
399+
const analyticsExportLineIndex = analyticsServices.findIndex((line) =>
400+
line.startsWith("export const AnalyticsServiceMap")
401+
);
402+
if (analyticsExportLineIndex !== -1) {
403+
const exportLine = analyticsServices[analyticsExportLineIndex];
404+
const objectContent = analyticsServices.slice(analyticsExportLineIndex + 1, -1);
405+
406+
analyticsOutput.push(
407+
exportLine.replace(
408+
"export const AnalyticsServiceMap = {",
409+
"export const AnalyticsServiceMap = process.env.NEXT_PUBLIC_IS_E2E ? {} : {"
410+
),
411+
...objectContent,
412+
"};"
413+
);
414+
} else {
415+
analyticsOutput.push(...analyticsServices);
416+
}
417+
381418
const banner = `/**
382419
This file is autogenerated using the command \`yarn app-store:build --watch\`.
383420
Don't modify this file manually.
@@ -392,6 +429,7 @@ function generateFiles() {
392429
["bookerApps.metadata.generated.ts", bookerMetadataOutput],
393430
["crm.apps.generated.ts", crmOutput],
394431
["calendar.services.generated.ts", calendarOutput],
432+
["analytics.services.generated.ts", analyticsOutput],
395433
];
396434
filesToGenerate.forEach(([fileName, output]) => {
397435
fs.writeFileSync(`${APP_STORE_PATH}/${fileName}`, formatOutput(`${banner}${output.join("\n")}`));

packages/app-store/_utils/getAnalytics.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import logger from "@calcom/lib/logger";
22
import type { AnalyticsService, AnalyticsServiceClass } from "@calcom/types/AnalyticsService";
33
import type { CredentialPayload } from "@calcom/types/Credential";
44

5-
import appStore from "..";
5+
import { AnalyticsServiceMap } from "../analytics.services.generated";
66

77
const log = logger.getSubLogger({ prefix: ["AnalyticsManager"] });
88

@@ -30,14 +30,15 @@ export const getAnalyticsService = async ({
3030

3131
const analyticsName = analyticsType.split("_")[0];
3232

33-
const analyticsAppImportFn = appStore[analyticsName as keyof typeof appStore];
33+
const analyticsAppImportFn = AnalyticsServiceMap[analyticsName as keyof typeof AnalyticsServiceMap];
3434

3535
if (!analyticsAppImportFn) {
3636
log.warn(`analytics app not implemented`);
3737
return null;
3838
}
3939

40-
const analyticsApp = await analyticsAppImportFn();
40+
const analyticsAppModule = await analyticsAppImportFn;
41+
const analyticsApp = { lib: { AnalyticsService: analyticsAppModule.default } };
4142

4243
if (!isAnalyticsService(analyticsApp)) {
4344
log.warn(`Analytics is not implemented`);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
This file is autogenerated using the command `yarn app-store:build --watch`.
3+
Don't modify this file manually.
4+
**/
5+
export const AnalyticsServiceMap = process.env.NEXT_PUBLIC_IS_E2E
6+
? {}
7+
: {
8+
dub: import("./dub/lib/AnalyticsService"),
9+
};

turbo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"IFFY_API_KEY",
8787
"INTEGRATION_TEST_MODE",
8888
"IS_E2E",
89+
"NEXT_PUBLIC_IS_E2E",
8990
"INTERCOM_SECRET",
9091
"INTERCOM_SECRET",
9192
"INSIGHTS_DATABASE_URL",

0 commit comments

Comments
 (0)