@@ -3,56 +3,87 @@ import type {
33 BuildExtension ,
44 BuildManifest ,
55} from '@trigger.dev/build' ;
6+ import type { Plugin } from 'esbuild' ;
67import { existsSync } from 'node:fs' ;
78import { cp , mkdir } from 'node:fs/promises' ;
89import { dirname , resolve } from 'node:path' ;
910
11+ const PACKAGE_NAME = '@comp/integration-platform' ;
12+
1013/**
1114 * Custom Trigger.dev build extension for @comp/integration-platform workspace package.
1215 *
1316 * Since @comp/integration-platform is a workspace package (not published to npm),
14- * we need to manually copy its built dist files into the trigger.dev deployment.
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
1520 */
1621export function integrationPlatformExtension ( ) : IntegrationPlatformExtension {
1722 return new IntegrationPlatformExtension ( ) ;
1823}
1924
2025class IntegrationPlatformExtension implements BuildExtension {
2126 public readonly name = 'IntegrationPlatformExtension' ;
27+ private _packagePath : string | undefined ;
2228
23- externalsForTarget ( target : string ) {
24- if ( target === 'dev' ) {
25- return [ ] ;
26- }
27- // Mark as external so esbuild doesn't try to bundle it
28- return [ '@comp/integration-platform' ] ;
29- }
30-
31- async onBuildComplete ( context : BuildContext , manifest : BuildManifest ) {
29+ async onBuildStart ( context : BuildContext ) {
3230 if ( context . target === 'dev' ) {
3331 return ;
3432 }
3533
36- // Find the integration-platform package dist
37- const packageDistPath = this . findPackageDist ( context . workingDir ) ;
34+ // Find the package path
35+ this . _packagePath = this . findPackageRoot ( context . workingDir ) ;
3836
39- if ( ! packageDistPath ) {
37+ if ( ! this . _packagePath ) {
4038 throw new Error (
4139 [
42- ' IntegrationPlatformExtension could not find @comp/integration-platform dist.' ,
40+ ` IntegrationPlatformExtension could not find ${ PACKAGE_NAME } .` ,
4341 'Make sure the package is built (run `bun run build` in packages/integration-platform).' ,
44- 'Searched in: ' +
45- resolve (
46- context . workingDir ,
47- '../../packages/integration-platform/dist' ,
48- ) ,
4942 ] . join ( '\n' ) ,
5043 ) ;
5144 }
5245
53- context . logger . debug (
54- `Found integration-platform dist at ${ packageDistPath } ` ,
55- ) ;
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 : / ^ @ c o m p \/ i n t e g r a t i o n - p l a t f o r m $ / } , ( ) => {
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 : / ^ @ c o m p \/ i n t e g r a t i o n - p l a t f o r m \/ / } ,
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' ) ;
5687
5788 // Copy the entire dist to the build output
5889 const destPath = resolve (
@@ -67,7 +98,7 @@ class IntegrationPlatformExtension implements BuildExtension {
6798 await cp ( packageDistPath , destDistPath , { recursive : true } ) ;
6899
69100 // Copy package.json for proper module resolution
70- const packageJsonPath = resolve ( dirname ( packageDistPath ) , 'package.json' ) ;
101+ const packageJsonPath = resolve ( packagePath , 'package.json' ) ;
71102 if ( existsSync ( packageJsonPath ) ) {
72103 await cp ( packageJsonPath , resolve ( destPath , 'package.json' ) ) ;
73104 }
@@ -77,15 +108,18 @@ class IntegrationPlatformExtension implements BuildExtension {
77108 ) ;
78109 }
79110
80- private findPackageDist ( workingDir : string ) : string | undefined {
111+ private findPackageRoot ( workingDir : string ) : string | undefined {
81112 // Look for the package relative to the api app
82113 const candidates = [
83- resolve ( workingDir , '../../packages/integration-platform/dist ' ) ,
84- resolve ( workingDir , '../packages/integration-platform/dist ' ) ,
114+ resolve ( workingDir , '../../packages/integration-platform' ) ,
115+ resolve ( workingDir , '../packages/integration-platform' ) ,
85116 ] ;
86117
87118 for ( const candidate of candidates ) {
88- if ( existsSync ( candidate ) ) {
119+ if (
120+ existsSync ( candidate ) &&
121+ existsSync ( resolve ( candidate , 'dist/index.js' ) )
122+ ) {
89123 return candidate ;
90124 }
91125 }
0 commit comments