Skip to content

The open alternative to Salesforce, designed for AI.

Retain frontend dependency export maps in app builds (#28469)

Weiko3f14b45d9336
diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts
new file mode 100644
index 00000000000..6394d025bae
--- /dev/null
+++ b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts
@@ -0,0 +1,111 @@
+import { it, expect, vi } from 'vitest';
+import { createHash } from 'crypto';
+import { mkdtemp, mkdir, readFile, rm, writeFile } from 'fs/promises';
+import { tmpdir } from 'os';
+import { join } from 'path';
+import { build, transform } from 'esbuild';
+import { runInNewContext } from 'vm';
+import { FileFolder } from 'twenty-shared/types';
+import {
+ computeDependencyFileChecksum,
+ computePackageJsonChecksum,
+ FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER,
+} from 'twenty-shared/application';
+import {
+ getSharedDependenciesShimSource,
+ type SharedDependenciesBuildManifest,
+} from 'twenty-shared/front-component-build';
+
+import { buildSharedDependenciesBundle } from '@/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle';
+
+it('ships the export map needed to compile another component against the uploaded app bundle', async () => {
+ const appPath = await mkdtemp(join(tmpdir(), 'app-shared-dependencies-'));
+ const packageJson = JSON.stringify({
+ dependencies: { 'fixture-library': '1.0.0' },
+ });
+ const yarnLock = 'fixture-lock';
+ try {
+ const packagePath = join(appPath, 'node_modules/fixture-library');
+ await mkdir(packagePath, { recursive: true });
+ await writeFile(
+ join(packagePath, 'package.json'),
+ JSON.stringify({
+ name: 'fixture-library',
+ main: 'index.js',
+ type: 'module',
+ }),
+ );
+ await writeFile(
+ join(packagePath, 'index.js'),
+ 'export const label = "Current app"; export default 42;',
+ );
+ await writeFile(join(appPath, 'package.json'), packageJson);
+ await writeFile(join(appPath, 'yarn.lock'), yarnLock);
+ const onFileBuilt = vi.fn();
+ const builtPath = 'shared.mjs';
+ await buildSharedDependenciesBundle({
+ appPath,
+ outputDir: 'output',
+ sharedDependencies: {
+ dependencies: ['fixture-library'],
+ builtPath,
+ builtChecksum: null,
+ },
+ onFileBuilt,
+ });
+ const source = await readFile(join(appPath, 'output', builtPath), 'utf8');
+ const manifest: SharedDependenciesBuildManifest = JSON.parse(
+ await readFile(join(appPath, 'output', `${builtPath}.json`), 'utf8'),
+ );
+ expect(manifest).toEqual({
+ checksum: createHash('sha256').update(source).digest('hex'),
+ packageJsonChecksum: computePackageJsonChecksum(packageJson),
+ yarnLockChecksum: computeDependencyFileChecksum(yarnLock),
+ exports: {
+ 'fixture-library': { namedExports: ['label'], hasDefaultExport: true },
+ },
+ });
+ expect(onFileBuilt).toHaveBeenCalledWith(
+ expect.objectContaining({
+ fileFolder: FileFolder.Dependencies,
+ builtPath: join('output', `${builtPath}.json`),
+ }),
+ );
+ const linked = await build({
+ stdin: {
+ contents: getSharedDependenciesShimSource({
+ specifier: 'fixture-library',
+ exportNames: manifest.exports['fixture-library'],
+ }),
+ },
+ bundle: true,
+ write: false,
+ format: 'esm',
+ plugins: [
+ {
+ name: 'stored-app-bundle',
+ setup(builder) {
+ builder.onResolve(
+ { filter: /^twenty:front-component-shared-dependencies$/ },
+ () => ({
+ path: FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER,
+ namespace: 'app-bundle',
+ }),
+ );
+ builder.onLoad({ filter: /.*/, namespace: 'app-bundle' }, () => ({
+ contents: source,
+ }));
+ },
+ },
+ ],
+ });
+ const compiled = await transform(linked.outputFiles[0].text, {
+ format: 'cjs',
+ });
+ const module = { exports: {} };
+ runInNewContext(compiled.code, { module });
+ expect(module.exports).toMatchObject({ label: 'Current app', default: 42 });
+ } finally {
+ await rm(appPath, { recursive: true, force: true });
+ }
+});
diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts
index b972223febf..a39d5e8ffcd 100644
--- a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts
+++ b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts
@@ -3,17 +3,21 @@ import { getBaseFrontComponentBuildOptions } from '@/app/bundles/front-component
import { type OnFileBuiltCallback } from '@/app/bundles/types/on-file-built-callback.type';
import {
getSharedDependenciesEntrySource,
+ getSharedDependenciesBuildManifestPath,
type SharedDependenciesBuildContext,
+ type SharedDependenciesBuildManifest,
} from 'twenty-shared/front-component-build';
import { enumerateSharedDependenciesExportNames } from '@/app/bundles/front-component-build/shared-dependencies-build/utils/enumerate-shared-dependencies-export-names';
import { getSharedDependenciesNamespaceCollisions } from '@/app/bundles/front-component-build/shared-dependencies-build/utils/get-shared-dependencies-namespace-collisions';
-import { ensureDir } from '@/app/fs-utils';
+import { ensureDir, pathExists } from '@/app/fs-utils';
import crypto from 'crypto';
import * as esbuild from 'esbuild';
-import { readFile } from 'node:fs/promises';
+import { readFile, writeFile } from 'node:fs/promises';
import { dirname, join } from 'path';
import {
OUTPUT_DIR,
+ computePackageJsonChecksum,
+ computeDependencyFileChecksum,
type FrontComponentSharedDependenciesManifest,
} from 'twenty-shared/application';
import { FileFolder } from 'twenty-shared/types';
@@ -89,5 +93,29 @@ export const buildSharedDependenciesBundle = async ({
checksum,
});
+ const yarnLockPath = join(appPath, 'yarn.lock');
+ const buildManifest: SharedDependenciesBuildManifest = {
+ checksum,
+ packageJsonChecksum: computePackageJsonChecksum(
+ await readFile(join(appPath, 'package.json'), 'utf-8'),
+ ),
+ yarnLockChecksum: (await pathExists(yarnLockPath))
+ ? computeDependencyFileChecksum(await readFile(yarnLockPath, 'utf-8'))
+ : null,
+ exports: Object.fromEntries(exportNamesBySpecifier),
+ };
+ const manifestSource = JSON.stringify(buildManifest);
+
+ await writeFile(
+ getSharedDependenciesBuildManifestPath(absoluteBuiltPath),
+ manifestSource,
+ );
+ await onFileBuilt({
+ fileFolder: FileFolder.Dependencies,
+ builtPath: getSharedDependenciesBuildManifestPath(builtPath),
+ sourcePath: 'package.json',
+ checksum: crypto.createHash('sha256').update(manifestSource).digest('hex'),
+ });
+
return { exportNamesBySpecifier };
};
diff --git a/packages/twenty-cli/src/app/manifest/utils/extract-front-component-shared-dependencies.ts b/packages/twenty-cli/src/app/manifest/utils/extract-front-component-shared-dependencies.ts
index 2a5a005bbed..ff31a145181 100644
--- a/packages/twenty-cli/src/app/manifest/utils/extract-front-component-shared-dependencies.ts
+++ b/packages/twenty-cli/src/app/manifest/utils/extract-front-component-shared-dependencies.ts
@@ -1,72 +1,16 @@
-import { normalizeFrontComponentSharedDependencies } from '@/app/manifest/utils/normalize-front-component-shared-dependencies';
+import { parseFrontComponentSharedDependencies } from 'twenty-shared/front-component-build';
import { pathExists, readJson } from '@/app/fs-utils';
-import { isNonEmptyArray, isNonEmptyString } from '@sniptt/guards';
+import { isDefined } from 'twenty-shared/utils';
import path from 'path';
import {
FRONT_COMPONENT_SHARED_DEPENDENCIES_BUILT_PATH,
type FrontComponentSharedDependenciesManifest,
- FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER,
} from 'twenty-shared/application';
-const RESERVED_DEPENDENCY_PREFIXES = ['twenty-client-sdk', 'twenty-sdk'];
-
type PackageJsonWithSharedDependencies = {
frontComponentSharedDependencies?: unknown;
};
-const getSharedDependenciesErrors = (dependencies: string[]): string[] => {
- const errors: string[] = [];
- const seenDependencies = new Set<string>();
-
- for (const dependency of dependencies) {
- if (!isNonEmptyString(dependency)) {
- errors.push(
- '"frontComponentSharedDependencies" entries in package.json must be non-empty strings',
- );
- continue;
- }
-
- if (
- dependency.startsWith('.') ||
- path.posix.isAbsolute(dependency) ||
- path.win32.isAbsolute(dependency)
- ) {
- errors.push(
- `Shared dependency "${dependency}" must be a package specifier, not a relative or absolute path`,
- );
- continue;
- }
-
- if (dependency === FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER) {
- errors.push(
- `Shared dependency "${dependency}" is reserved and cannot be shared`,
- );
- continue;
- }
-
- if (
- RESERVED_DEPENDENCY_PREFIXES.some(
- (prefix) =>
- dependency === prefix || dependency.startsWith(`${prefix}/`),
- )
- ) {
- errors.push(
- `Shared dependency "${dependency}" is already served separately and cannot be shared`,
- );
- continue;
- }
-
- if (seenDependencies.has(dependency)) {
- errors.push(`Shared dependency "${dependency}" is declared twice`);
- continue;
- }
-
- seenDependencies.add(dependency);
- }
-
- return errors;
-};
-
export const extractFrontComponentSharedDependencies = async (
appPath: string,
): Promise<{
@@ -81,34 +25,17 @@ export const extractFrontComponentSharedDependencies = async (
const packageJson =
await readJson<PackageJsonWithSharedDependencies>(packageJsonPath);
- const declaredDependencies = packageJson.frontComponentSharedDependencies;
-
- if (declaredDependencies === undefined) {
- return { errors: [] };
- }
-
- if (!Array.isArray(declaredDependencies)) {
- return {
- errors: [
- '"frontComponentSharedDependencies" in package.json must be an array of package specifiers',
- ],
- };
- }
-
- if (!isNonEmptyArray(declaredDependencies)) {
- return { errors: [] };
- }
-
- const errors = getSharedDependenciesErrors(declaredDependencies);
+ const { dependencies, errors } = parseFrontComponentSharedDependencies(
+ packageJson.frontComponentSharedDependencies,
+ );
- if (isNonEmptyArray(errors)) {
+ if (!isDefined(dependencies)) {
return { errors };
}
return {
sharedDependencies: {
- dependencies:
- normalizeFrontComponentSharedDependencies(declaredDependencies),
+ dependencies,
builtPath: FRONT_COMPONENT_SHARED_DEPENDENCIES_BUILT_PATH,
builtChecksum: null,
},
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-install/utils/__tests__/build-application-file-list.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-install/utils/__tests__/build-application-file-list.util.spec.ts
index 12fa431d169..df65fb7a6cb 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-install/utils/__tests__/build-application-file-list.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-install/utils/__tests__/build-application-file-list.util.spec.ts
@@ -96,6 +96,11 @@ describe('buildApplicationFileList', () => {
fileFolder: FileFolder.BuiltFrontComponent,
isRequired: true,
},
+ {
+ relativePath: 'front-component-shared-dependencies.mjs.json',
+ fileFolder: FileFolder.Dependencies,
+ isRequired: false,
+ },
]),
);
});
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-install/utils/build-application-file-list.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-install/utils/build-application-file-list.util.ts
index 0f94cdedc9b..07ce6341417 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-install/utils/build-application-file-list.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-install/utils/build-application-file-list.util.ts
@@ -2,6 +2,7 @@ import {
APPLICATION_PACKAGE_YARN_LOCK_PATH,
type Manifest,
} from 'twenty-shared/application';
+import { getSharedDependenciesBuildManifestPath } from 'twenty-shared/front-component-build';
import { FileFolder } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
@@ -68,11 +69,20 @@ export const buildApplicationFileList = (
manifest.application.frontComponentSharedDependencies;
if (isDefined(sharedDependencies)) {
- files.push({
- relativePath: sharedDependencies.builtPath,
- fileFolder: FileFolder.BuiltFrontComponent,
- isRequired: true,
- });
+ files.push(
+ {
+ relativePath: sharedDependencies.builtPath,
+ fileFolder: FileFolder.BuiltFrontComponent,
+ isRequired: true,
+ },
+ {
+ relativePath: getSharedDependenciesBuildManifestPath(
+ sharedDependencies.builtPath,
+ ),
+ fileFolder: FileFolder.Dependencies,
+ isRequired: false,
+ },
+ );
}
for (const publicAsset of manifest.publicAssets ?? []) {
diff --git a/packages/twenty-shared/src/front-component-build/get-shared-dependencies-build-manifest-path.ts b/packages/twenty-shared/src/front-component-build/get-shared-dependencies-build-manifest-path.ts
new file mode 100644
index 00000000000..4388307a316
--- /dev/null
+++ b/packages/twenty-shared/src/front-component-build/get-shared-dependencies-build-manifest-path.ts
@@ -0,0 +1,3 @@
+export const getSharedDependenciesBuildManifestPath = (
+ builtPath: string,
+): string => `${builtPath}.json`;
diff --git a/packages/twenty-shared/src/front-component-build/index.ts b/packages/twenty-shared/src/front-component-build/index.ts
index 7ce915b70a7..1a52280cb56 100644
--- a/packages/twenty-shared/src/front-component-build/index.ts
+++ b/packages/twenty-shared/src/front-component-build/index.ts
@@ -14,10 +14,14 @@ export {
} from './css-injection-plugin';
export { enumerateSharedDependenciesExportNames } from './enumerate-shared-dependencies-export-names';
export { getCommonFrontComponentBuildOptions } from './get-common-front-component-build-options';
+export { getSharedDependenciesBuildManifestPath } from './get-shared-dependencies-build-manifest-path';
export { getSharedDependenciesEntrySource } from './get-shared-dependencies-entry-source';
export { getSharedDependenciesShimSource } from './get-shared-dependencies-shim-source';
export { createJsxRuntimeRemoteWrapperPlugin } from './jsx-runtime-remote-wrapper-plugin';
+export { normalizeFrontComponentSharedDependencies } from './normalize-front-component-shared-dependencies';
+export { parseFrontComponentSharedDependencies } from './parse-front-component-shared-dependencies';
export type { SharedDependenciesBuildContext } from './shared-dependencies-build-context.type';
+export type { SharedDependenciesBuildManifest } from './shared-dependencies-build-manifest.type';
export type { SharedDependenciesExportNames } from './shared-dependencies-export-names.type';
export { toSharedDependenciesNamespaceIdentifier } from './to-shared-dependencies-namespace-identifier';
export { unwrapDefineFrontComponentToDirectExport } from './unwrap-define-front-component-to-direct-export';
diff --git a/packages/twenty-cli/src/app/manifest/utils/normalize-front-component-shared-dependencies.ts b/packages/twenty-shared/src/front-component-build/normalize-front-component-shared-dependencies.ts
similarity index 100%
rename from packages/twenty-cli/src/app/manifest/utils/normalize-front-component-shared-dependencies.ts
rename to packages/twenty-shared/src/front-component-build/normalize-front-component-shared-dependencies.ts
diff --git a/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts b/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts
new file mode 100644
index 00000000000..913f0065c8e
--- /dev/null
+++ b/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts
@@ -0,0 +1,85 @@
+import { isNonEmptyArray, isNonEmptyString } from '@sniptt/guards';
+import path from 'node:path';
+
+import { FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER } from '../application/constants/FrontComponentSharedDependenciesImportSpecifier';
+import { normalizeFrontComponentSharedDependencies } from './normalize-front-component-shared-dependencies';
+
+const RESERVED_DEPENDENCY_PREFIXES = ['twenty-client-sdk', 'twenty-sdk'];
+
+export const parseFrontComponentSharedDependencies = (
+ declaredDependencies: unknown,
+): { dependencies?: string[]; errors: string[] } => {
+ if (declaredDependencies === undefined) {
+ return { errors: [] };
+ }
+
+ if (!Array.isArray(declaredDependencies)) {
+ return {
+ errors: [
+ '"frontComponentSharedDependencies" in package.json must be an array of package specifiers',
+ ],
+ };
+ }
+
+ if (!isNonEmptyArray(declaredDependencies)) {
+ return { errors: [] };
+ }
+
+ const errors: string[] = [];
+ const seenDependencies = new Set<string>();
+
+ for (const dependency of declaredDependencies) {
+ if (!isNonEmptyString(dependency)) {
+ errors.push(
+ '"frontComponentSharedDependencies" entries in package.json must be non-empty strings',
+ );
+ continue;
+ }
+
+ if (
+ dependency.startsWith('.') ||
+ path.posix.isAbsolute(dependency) ||
+ path.win32.isAbsolute(dependency)
+ ) {
+ errors.push(
+ `Shared dependency "${dependency}" must be a package specifier, not a relative or absolute path`,
+ );
+ continue;
+ }
+
+ if (dependency === FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER) {
+ errors.push(
+ `Shared dependency "${dependency}" is reserved and cannot be shared`,
+ );
+ continue;
+ }
+
+ if (
+ RESERVED_DEPENDENCY_PREFIXES.some(
+ (prefix) =>
+ dependency === prefix || dependency.startsWith(`${prefix}/`),
+ )
+ ) {
+ errors.push(
+ `Shared dependency "${dependency}" is already served separately and cannot be shared`,
+ );
+ continue;
+ }
+
+ if (seenDependencies.has(dependency)) {
+ errors.push(`Shared dependency "${dependency}" is declared twice`);
+ continue;
+ }
+
+ seenDependencies.add(dependency);
+ }
+
+ return isNonEmptyArray(errors)
+ ? { errors }
+ : {
+ dependencies: normalizeFrontComponentSharedDependencies([
+ ...seenDependencies,
+ ]),
+ errors: [],
+ };
+};
diff --git a/packages/twenty-shared/src/front-component-build/shared-dependencies-build-manifest.type.ts b/packages/twenty-shared/src/front-component-build/shared-dependencies-build-manifest.type.ts
new file mode 100644
index 00000000000..c6efe9c2503
--- /dev/null
+++ b/packages/twenty-shared/src/front-component-build/shared-dependencies-build-manifest.type.ts
@@ -0,0 +1,8 @@
+import { type SharedDependenciesExportNames } from '@/front-component-build/shared-dependencies-export-names.type';
+
+export type SharedDependenciesBuildManifest = {
+ checksum: string;
+ packageJsonChecksum: string;
+ yarnLockChecksum: string | null;
+ exports: Record<string, SharedDependenciesExportNames>;
+};