Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,6 @@ dist

.turbo
dist
link
link

federation-bundle/
89 changes: 89 additions & 0 deletions bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Bun, { $ } from 'bun';

const inputDir = './packages/federation-sdk';
const outputDir = './federation-bundle';

// get dependencies from all packages
function getAllDependencies() {
const packages = ['core', 'crypto', 'federation-sdk', 'room'];

const allDependencies = new Set<string>();

for (const pkg of packages) {
const packageJson = require(`./packages/${pkg}/package.json`);

const dependencies = packageJson.dependencies
? Object.keys(packageJson.dependencies)
: [];

for (const dep of dependencies) {
allDependencies.add(dep);
}
}

return Array.from(allDependencies);
}

async function main() {
await $`rm -rf ${outputDir}/dist`;
await $`mkdir -p ${outputDir}/dist`;

const dependencies = getAllDependencies();

await Bun.build({
entrypoints: [`${inputDir}/src/index.ts`],
outdir: `${outputDir}/dist`,
target: 'node',
format: 'cjs',
external: dependencies,
env: 'disable',
define: {
'process.env.NODE_ENV': '"production"',
},
minify: true,
sourcemap: true,
});
Comment on lines +7 to +46

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix require usage under Bun’s ESM runtime

Line 13: executing this file via bun run bundle.ts treats it as ESM, so require is undefined and the script aborts before bundling. Swap to Bun’s file API and make the helper async so JSON loading works under Bun.

-// get dependencies from all packages
-function getAllDependencies() {
+// get dependencies from all packages
+async function getAllDependencies() {
 	const packages = ['core', 'crypto', 'federation-sdk', 'room'];
 
 	const allDependencies = new Set<string>();
 
 	for (const pkg of packages) {
-		const packageJson = require(`./packages/${pkg}/package.json`);
+		const packageJson = JSON.parse(
+			await Bun.file(`./packages/${pkg}/package.json`).text(),
+		);
 
 		const dependencies = packageJson.dependencies
 			? Object.keys(packageJson.dependencies)
 			: [];
 
 		for (const dep of dependencies) {
 			allDependencies.add(dep);
 		}
 	}
 
 	return Array.from(allDependencies);
 }
 
 async function main() {
 	await $`rm -rf ${outputDir}/dist`;
 	await $`mkdir -p ${outputDir}/dist`;
 
-	const dependencies = getAllDependencies();
+	const dependencies = await getAllDependencies();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function getAllDependencies() {
const packages = ['core', 'crypto', 'federation-sdk', 'room'];
const allDependencies = new Set<string>();
for (const pkg of packages) {
const packageJson = require(`./packages/${pkg}/package.json`);
const dependencies = packageJson.dependencies
? Object.keys(packageJson.dependencies)
: [];
for (const dep of dependencies) {
allDependencies.add(dep);
}
}
return Array.from(allDependencies);
}
async function main() {
await $`rm -rf ${outputDir}/dist`;
await $`mkdir -p ${outputDir}/dist`;
const dependencies = getAllDependencies();
await Bun.build({
entrypoints: [`${inputDir}/src/index.ts`],
outdir: `${outputDir}/dist`,
target: 'node',
format: 'cjs',
external: dependencies,
env: 'disable',
define: {
'process.env.NODE_ENV': '"production"',
},
minify: true,
sourcemap: true,
});
// get dependencies from all packages
async function getAllDependencies() {
const packages = ['core', 'crypto', 'federation-sdk', 'room'];
const allDependencies = new Set<string>();
for (const pkg of packages) {
const packageJson = JSON.parse(
await Bun.file(`./packages/${pkg}/package.json`).text(),
);
const dependencies = packageJson.dependencies
? Object.keys(packageJson.dependencies)
: [];
for (const dep of dependencies) {
allDependencies.add(dep);
}
}
return Array.from(allDependencies);
}
async function main() {
await $`rm -rf ${outputDir}/dist`;
await $`mkdir -p ${outputDir}/dist`;
const dependencies = await getAllDependencies();
await Bun.build({
entrypoints: [`${inputDir}/src/index.ts`],
outdir: `${outputDir}/dist`,
target: 'node',
format: 'cjs',
external: dependencies,
env: 'disable',
define: {
'process.env.NODE_ENV': '"production"',
},
minify: true,
sourcemap: true,
});
}
🤖 Prompt for AI Agents
In bundle.ts around lines 7 to 45, the helper uses require() which fails under
Bun's ESM runtime; make getAllDependencies async and replace require calls with
Bun.file(...).text() followed by JSON.parse (or Bun.file(...).json() if
available), iterate packageJson.dependencies the same way, and return the
dependency array; update main to await getAllDependencies() so bundling runs
with the resolved external list.


const packageJson = JSON.parse(
await Bun.file(`${inputDir}/package.json`).text(),
);

const filterWorkspace = (deps: Record<string, unknown>) =>
Object.fromEntries(
Object.entries(deps || {}).filter(
([, value]) =>
typeof value === 'string' && !value.startsWith('workspace:'),
),
);

packageJson.dependencies = filterWorkspace(packageJson.dependencies);
packageJson.devDependencies = filterWorkspace(packageJson.devDependencies);
packageJson.peerDependencies = filterWorkspace(packageJson.peerDependencies);

await Bun.file(`${outputDir}/package.json`).write(
`${JSON.stringify(packageJson, null, 2)}\n`,
);

await $`tsc --emitDeclarationOnly -p tsconfig.sdk.types.json`;

console.log('Bundle complete!');
}

await main();

/*
bun build ./packages/federation-sdk/src/index.ts \
--outdir ./packages/federation-bundle/dist-cli \
--target node \
--format=cjs \
-e pino \
-e mongodb \
-e zod \
-e pino-pretty \
-e @rocket.chat/emitter \
-e reflect-metadata \
-e tsyringe \
-e tweetnacl \
--production \
--sourcemap=inline
*/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"lint": "bunx @biomejs/biome lint --diagnostic-level=error",
"lint:ci": "bunx @biomejs/biome ci --diagnostic-level=error",
"lint:fix": "bunx @biomejs/biome lint --fix",
"tsc": "bunx tsc --noEmit",
"bundle:sdk": "bunx esbuild --bundle --sourcemap --tsconfig=./tsconfig.json --platform=node --allow-overwrite --outfile=packages/federation-sdk/dist/index.js packages/federation-sdk/dist/index.js"
"tsc": "tsc --noEmit",
"bundle:sdk": "bun run bundle.ts",
"bundle:watch": "bun build --watch ./packages/federation-sdk/src/index.ts --outdir ./federation-bundle/dist --target node --format=cjs -e pino -e mongodb -e zod -e pino-pretty -e @rocket.chat/emitter -e reflect-metadata -e tsyringe -e tweetnacl --production --sourcemap=inline"
}
}
6 changes: 3 additions & 3 deletions packages/federation-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "@rocket.chat/federation-sdk",
"version": "0.1.3",
"description": "Matrix Federation SDK for server-to-server communication",
"main": "./dist/bundle.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/bundle.js",
"require": "./dist/bundle.js"
"import": "./dist/index.js",
"require": "./dist/index.js"
}
},
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.sdk.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./packages/federation-bundle/dist",
"rootDir": "./packages/federation-sdk/src",
"composite": false,
"verbatimModuleSyntax": false,
"declarationMap": true,
"noEmit": false,
"tsBuildInfoFile": null,
"incremental": false
},
"include": ["./packages/federation-sdk/src/**/*"],
"exclude": ["node_modules", "dist", "**/*.spec.ts", "**/*.test.ts"]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}