Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changeset/deduplicate-configs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@workflow/builders": patch
"@workflow/cli": patch
---

Deduplicate package.json and .vc-config.json generation
56 changes: 56 additions & 0 deletions packages/builders/src/base-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,62 @@ export const OPTIONS = handler;`;
);
}

/**
* Creates a package.json file with the specified module type.
*/
protected async createPackageJson(
dir: string,
type: 'commonjs' | 'module'
): Promise<void> {
const packageJson = { type };
await writeFile(
join(dir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
}

/**
* Creates a .vc-config.json file for Vercel Build Output API functions.
*/
protected async createVcConfig(
dir: string,
config: {
runtime?: string;
handler?: string;
launcherType?: string;
architecture?: string;
shouldAddHelpers?: boolean;
shouldAddSourcemapSupport?: boolean;
experimentalTriggers?: Array<{
type: string;
topic: string;
consumer: string;
maxDeliveries?: number;
retryAfterSeconds?: number;
initialDelaySeconds?: number;
}>;
}
): Promise<void> {
const vcConfig = {
runtime: config.runtime ?? 'nodejs22.x',
handler: config.handler ?? 'index.js',
launcherType: config.launcherType ?? 'Nodejs',
architecture: config.architecture ?? 'arm64',
shouldAddHelpers: config.shouldAddHelpers ?? true,
...(config.shouldAddSourcemapSupport !== undefined && {
shouldAddSourcemapSupport: config.shouldAddSourcemapSupport,
}),
...(config.experimentalTriggers && {
experimentalTriggers: config.experimentalTriggers,
}),
};

await writeFile(
join(dir, '.vc-config.json'),
JSON.stringify(vcConfig, null, 2)
);
}

private async createSwcGitignore(): Promise<void> {
try {
await writeFile(
Expand Down
77 changes: 12 additions & 65 deletions packages/builders/src/vercel-build-output-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,12 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
tsPaths,
});

// Create package.json for CommonJS
const packageJson = {
type: 'commonjs',
};
await writeFile(
join(stepsFuncDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);

// Create .vc-config.json for steps function
const stepsConfig = {
runtime: 'nodejs22.x',
handler: 'index.js',
launcherType: 'Nodejs',
architecture: 'arm64',
shouldAddHelpers: true,
// Create package.json and .vc-config.json for steps function
await this.createPackageJson(stepsFuncDir, 'commonjs');
await this.createVcConfig(stepsFuncDir, {
shouldAddSourcemapSupport: true,
experimentalTriggers: [STEP_QUEUE_TRIGGER],
};

await writeFile(
join(stepsFuncDir, '.vc-config.json'),
JSON.stringify(stepsConfig, null, 2)
);
});
}

private async buildWorkflowsFunction({
Expand All @@ -99,29 +81,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
tsPaths,
});

// Create package.json for ESM support
const packageJson = {
type: 'commonjs',
};
await writeFile(
join(workflowsFuncDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);

// Create .vc-config.json for workflows function
const workflowsConfig = {
runtime: 'nodejs22.x',
handler: 'index.js',
launcherType: 'Nodejs',
architecture: 'arm64',
shouldAddHelpers: true,
// Create package.json and .vc-config.json for workflows function
await this.createPackageJson(workflowsFuncDir, 'commonjs');
await this.createVcConfig(workflowsFuncDir, {
experimentalTriggers: [WORKFLOW_QUEUE_TRIGGER],
};

await writeFile(
join(workflowsFuncDir, '.vc-config.json'),
JSON.stringify(workflowsConfig, null, 2)
);
});
}

private async buildWebhookFunction({
Expand All @@ -143,28 +107,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
bundle, // Build Output API needs bundling (except in tests)
});

// Create package.json for CommonJS
const packageJson = {
type: 'commonjs',
};
await writeFile(
join(webhookFuncDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);

// Create .vc-config.json for webhook function
const webhookConfig = {
runtime: 'nodejs22.x',
handler: 'index.js',
launcherType: 'Nodejs',
architecture: 'arm64',
// Create package.json and .vc-config.json for webhook function
await this.createPackageJson(webhookFuncDir, 'commonjs');
await this.createVcConfig(webhookFuncDir, {
shouldAddHelpers: false,
};

await writeFile(
join(webhookFuncDir, '.vc-config.json'),
JSON.stringify(webhookConfig, null, 2)
);
});
}

private async createBuildOutputConfig(outputDir: string): Promise<void> {
Expand Down
77 changes: 12 additions & 65 deletions packages/cli/src/lib/builders/vercel-build-output-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,12 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
tsPaths,
});

// Create package.json for CommonJS
const packageJson = {
type: 'commonjs',
};
await writeFile(
join(stepsFuncDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);

// Create .vc-config.json for steps function
const stepsConfig = {
runtime: 'nodejs22.x',
handler: 'index.js',
launcherType: 'Nodejs',
architecture: 'arm64',
shouldAddHelpers: true,
// Create package.json and .vc-config.json for steps function
await this.createPackageJson(stepsFuncDir, 'commonjs');
await this.createVcConfig(stepsFuncDir, {
shouldAddSourcemapSupport: true,
experimentalTriggers: [STEP_QUEUE_TRIGGER],
};

await writeFile(
join(stepsFuncDir, '.vc-config.json'),
JSON.stringify(stepsConfig, null, 2)
);
});
}

private async buildWorkflowsFunction({
Expand All @@ -102,29 +84,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
tsPaths,
});

// Create package.json for ESM support
const packageJson = {
type: 'commonjs',
};
await writeFile(
join(workflowsFuncDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);

// Create .vc-config.json for workflows function
const workflowsConfig = {
runtime: 'nodejs22.x',
handler: 'index.js',
launcherType: 'Nodejs',
architecture: 'arm64',
shouldAddHelpers: true,
// Create package.json and .vc-config.json for workflows function
await this.createPackageJson(workflowsFuncDir, 'commonjs');
await this.createVcConfig(workflowsFuncDir, {
experimentalTriggers: [WORKFLOW_QUEUE_TRIGGER],
};

await writeFile(
join(workflowsFuncDir, '.vc-config.json'),
JSON.stringify(workflowsConfig, null, 2)
);
});
}

private async buildWebhookFunction({
Expand All @@ -146,28 +110,11 @@ export class VercelBuildOutputAPIBuilder extends BaseBuilder {
bundle, // Build Output API needs bundling (except in tests)
});

// Create package.json for CommonJS
const packageJson = {
type: 'commonjs',
};
await writeFile(
join(webhookFuncDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
);

// Create .vc-config.json for webhook function
const webhookConfig = {
runtime: 'nodejs22.x',
handler: 'index.js',
launcherType: 'Nodejs',
architecture: 'arm64',
// Create package.json and .vc-config.json for webhook function
await this.createPackageJson(webhookFuncDir, 'commonjs');
await this.createVcConfig(webhookFuncDir, {
shouldAddHelpers: false,
};

await writeFile(
join(webhookFuncDir, '.vc-config.json'),
JSON.stringify(webhookConfig, null, 2)
);
});
}

private async createBuildOutputConfig(outputDir: string): Promise<void> {
Expand Down