|
| 1 | +import type { |
| 2 | + BuildContext, |
| 3 | + BuildExtension, |
| 4 | + BuildManifest, |
| 5 | +} from '@trigger.dev/build'; |
| 6 | +import type { Plugin } from 'esbuild'; |
| 7 | +import { existsSync } from 'node:fs'; |
| 8 | +import { cp, mkdir } from 'node:fs/promises'; |
| 9 | +import { dirname, resolve } from 'node:path'; |
| 10 | + |
| 11 | +const PACKAGE_NAME = '@comp/integration-platform'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Custom Trigger.dev build extension for @comp/integration-platform workspace package. |
| 15 | + * |
| 16 | + * Since @comp/integration-platform is a workspace package (not published to npm), |
| 17 | + * we need to: |
| 18 | + * 1. Add an esbuild plugin to resolve the import path during build |
| 19 | + * 2. Copy the built dist files into the trigger.dev deployment |
| 20 | + */ |
| 21 | +export function integrationPlatformExtension(): IntegrationPlatformExtension { |
| 22 | + return new IntegrationPlatformExtension(); |
| 23 | +} |
| 24 | + |
| 25 | +class IntegrationPlatformExtension implements BuildExtension { |
| 26 | + public readonly name = 'IntegrationPlatformExtension'; |
| 27 | + private _packagePath: string | undefined; |
| 28 | + |
| 29 | + async onBuildStart(context: BuildContext) { |
| 30 | + if (context.target === 'dev') { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Find the package path |
| 35 | + this._packagePath = this.findPackageRoot(context.workingDir); |
| 36 | + |
| 37 | + if (!this._packagePath) { |
| 38 | + throw new Error( |
| 39 | + [ |
| 40 | + `IntegrationPlatformExtension could not find ${PACKAGE_NAME}.`, |
| 41 | + 'Make sure the package is built (run `bun run build` in packages/integration-platform).', |
| 42 | + ].join('\n'), |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + context.logger.debug(`Found integration-platform at ${this._packagePath}`); |
| 47 | + |
| 48 | + // Register esbuild plugin to resolve the workspace package |
| 49 | + const packagePath = this._packagePath; |
| 50 | + const resolvePlugin: Plugin = { |
| 51 | + name: 'resolve-integration-platform', |
| 52 | + setup(build) { |
| 53 | + // Resolve bare import |
| 54 | + build.onResolve({ filter: /^@comp\/integration-platform$/ }, () => { |
| 55 | + return { |
| 56 | + path: resolve(packagePath, 'dist/index.js'), |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + // Resolve subpath imports like @comp/integration-platform/types |
| 61 | + build.onResolve( |
| 62 | + { filter: /^@comp\/integration-platform\// }, |
| 63 | + (args) => { |
| 64 | + const subpath = args.path.replace(`${PACKAGE_NAME}/`, ''); |
| 65 | + return { |
| 66 | + path: resolve(packagePath, 'dist', `${subpath}/index.js`), |
| 67 | + }; |
| 68 | + }, |
| 69 | + ); |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + context.registerPlugin(resolvePlugin); |
| 74 | + } |
| 75 | + |
| 76 | + async onBuildComplete(context: BuildContext, manifest: BuildManifest) { |
| 77 | + if (context.target === 'dev') { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const packagePath = this._packagePath; |
| 82 | + if (!packagePath) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const packageDistPath = resolve(packagePath, 'dist'); |
| 87 | + |
| 88 | + // Copy the entire dist to the build output |
| 89 | + const destPath = resolve( |
| 90 | + manifest.outputPath, |
| 91 | + 'node_modules/@comp/integration-platform', |
| 92 | + ); |
| 93 | + const destDistPath = resolve(destPath, 'dist'); |
| 94 | + |
| 95 | + await mkdir(destDistPath, { recursive: true }); |
| 96 | + |
| 97 | + // Copy dist files |
| 98 | + await cp(packageDistPath, destDistPath, { recursive: true }); |
| 99 | + |
| 100 | + // Copy package.json for proper module resolution |
| 101 | + const packageJsonPath = resolve(packagePath, 'package.json'); |
| 102 | + if (existsSync(packageJsonPath)) { |
| 103 | + await cp(packageJsonPath, resolve(destPath, 'package.json')); |
| 104 | + } |
| 105 | + |
| 106 | + context.logger.log( |
| 107 | + 'Copied @comp/integration-platform to deployment bundle', |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + private findPackageRoot(workingDir: string): string | undefined { |
| 112 | + // Look for the package relative to the api app |
| 113 | + const candidates = [ |
| 114 | + resolve(workingDir, '../../packages/integration-platform'), |
| 115 | + resolve(workingDir, '../packages/integration-platform'), |
| 116 | + ]; |
| 117 | + |
| 118 | + for (const candidate of candidates) { |
| 119 | + if ( |
| 120 | + existsSync(candidate) && |
| 121 | + existsSync(resolve(candidate, 'dist/index.js')) |
| 122 | + ) { |
| 123 | + return candidate; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return undefined; |
| 128 | + } |
| 129 | +} |
0 commit comments