Skip to content

The open alternative to Salesforce, designed for AI.

fix(client-sdk): externalize all Node builtins in Vite lib build (#26234)

Ayesha Waseembfa94963bd13
diff --git a/packages/twenty-client-sdk/src/__tests__/isExternal.test.ts b/packages/twenty-client-sdk/src/__tests__/isExternal.test.ts
new file mode 100644
index 00000000000..5994af68d57
--- /dev/null
+++ b/packages/twenty-client-sdk/src/__tests__/isExternal.test.ts
@@ -0,0 +1,33 @@
+import { describe, expect, it } from 'vitest';
+
+import { isExternal } from '../../vite.shared';
+
+describe('isExternal', () => {
+ it.each([
+ 'crypto',
+ 'node:crypto',
+ 'zlib',
+ 'node:zlib',
+ 'stream',
+ 'node:stream',
+ 'child_process',
+ 'node:child_process',
+ 'node:fs',
+ 'node:fs/promises',
+ 'node:path',
+ ])('externalizes the Node builtin "%s"', (builtin) => {
+ expect(isExternal(builtin)).toBe(true);
+ });
+
+ it('externalizes declared dependencies and their subpaths', () => {
+ expect(isExternal('graphql')).toBe(true);
+ expect(isExternal('lodash')).toBe(true);
+ expect(isExternal('lodash/merge')).toBe(true);
+ });
+
+ it('does not externalize twenty-shared or app source', () => {
+ expect(isExternal('twenty-shared')).toBe(false);
+ expect(isExternal('@/core/index')).toBe(false);
+ expect(isExternal('some-unbundled-package')).toBe(false);
+ });
+});
diff --git a/packages/twenty-client-sdk/vite.shared.ts b/packages/twenty-client-sdk/vite.shared.ts
index 3c6c6b24e69..17f386f2b26 100644
--- a/packages/twenty-client-sdk/vite.shared.ts
+++ b/packages/twenty-client-sdk/vite.shared.ts
@@ -1,3 +1,5 @@
+import { builtinModules } from 'node:module';
+
import packageJson from './package.json';
const externalDeps = [
@@ -5,14 +7,18 @@ const externalDeps = [
...Object.keys(packageJson.devDependencies).filter(
(dep) => dep !== 'twenty-shared',
),
- 'node:fs/promises',
- 'node:fs',
- 'node:path',
- 'node:os',
- 'node:url',
];
+const nodeBuiltins = new Set<string>([
+ ...builtinModules,
+ ...builtinModules.map((moduleName) => `node:${moduleName}`),
+]);
+
+const isNodeBuiltin = (id: string) =>
+ id.startsWith('node:') || nodeBuiltins.has(id);
+
export const isExternal = (id: string) =>
+ isNodeBuiltin(id) ||
externalDeps.some((dep) => id === dep || id.startsWith(`${dep}/`));
export const entryFileNames = (chunk: any, extension: 'cjs' | 'mjs') => {