@@ -10,7 +10,7 @@ import {
10
10
import chalk from "chalk" ;
11
11
import { watch } from "chokidar" ;
12
12
import { Command } from "commander" ;
13
- import { BuildContext , context } from "esbuild" ;
13
+ import { BuildContext , Metafile , context } from "esbuild" ;
14
14
import { resolve as importResolve } from "import-meta-resolve" ;
15
15
import { Box , Text , render , useApp , useInput } from "ink" ;
16
16
import { createHash } from "node:crypto" ;
@@ -23,14 +23,14 @@ import React, { Suspense, useEffect } from "react";
23
23
import { ClientOptions , WebSocket as wsWebSocket } from "ws" ;
24
24
import { z } from "zod" ;
25
25
import * as packageJson from "../../package.json" ;
26
+ import { CliApiClient } from "../apiClient" ;
27
+ import { CommonCommandOptions } from "../cli/common.js" ;
26
28
import { BackgroundWorker , BackgroundWorkerCoordinator } from "../dev/backgroundWorker.js" ;
29
+ import { getConfigPath , readConfig } from "../utilities/configFiles" ;
27
30
import { printStandloneInitialBanner } from "../utilities/initialBanner.js" ;
28
31
import { logger } from "../utilities/logger.js" ;
29
32
import { isLoggedIn } from "../utilities/session.js" ;
30
- import { CommonCommandOptions } from "../cli/common.js" ;
31
- import { getConfigPath , readConfig } from "../utilities/configFiles" ;
32
33
import { createTaskFileImports , gatherTaskFiles } from "../utilities/taskFiles" ;
33
- import { CliApiClient } from "../apiClient" ;
34
34
35
35
let apiClient : CliApiClient | undefined ;
36
36
@@ -298,10 +298,14 @@ function useDev({
298
298
new URL ( importResolve ( "./worker-facade.js" , import . meta. url ) ) . href . replace ( "file://" , "" ) ,
299
299
"utf-8"
300
300
) ;
301
- const entryPointContents = workerFacade . replace (
302
- "__TASKS__" ,
303
- createTaskFileImports ( taskFiles )
304
- ) ;
301
+
302
+ const registerTracingPath = new URL (
303
+ importResolve ( "./register-tracing.js" , import . meta. url )
304
+ ) . href . replace ( "file://" , "" ) ;
305
+
306
+ const entryPointContents = workerFacade
307
+ . replace ( "__TASKS__" , createTaskFileImports ( taskFiles ) )
308
+ . replace ( "__REGISTER_TRACING__" , `import { tracingSDK } from "${ registerTracingPath } ";` ) ;
305
309
306
310
let firstBuild = true ;
307
311
@@ -318,17 +322,15 @@ function useDev({
318
322
write : false ,
319
323
minify : false ,
320
324
sourcemap : "external" , // does not set the //# sourceMappingURL= comment in the file, we handle it ourselves
325
+ packages : "external" , // https://esbuild.github.io/api/#packages
321
326
logLevel : "warning" ,
322
327
platform : "node" ,
323
- format : "esm" ,
328
+ format : "cjs" , // This is needed to support opentelemetry instrumentation that uses module patching
324
329
target : [ "node18" , "es2020" ] ,
325
330
outdir : "out" ,
326
331
define : {
327
332
TRIGGER_API_URL : `"${ config . triggerUrl } "` ,
328
333
} ,
329
- banner : {
330
- js : `import { createRequire } from 'module';const require = createRequire(import.meta.url);` ,
331
- } ,
332
334
plugins : [
333
335
{
334
336
name : "trigger.dev v3" ,
@@ -379,7 +381,7 @@ function useDev({
379
381
}
380
382
381
383
// Create a file at join(dir, ".trigger", path) with the fileContents
382
- const fullPath = join ( config . projectDir , ".trigger" , `${ contentHash } .mjs ` ) ;
384
+ const fullPath = join ( config . projectDir , ".trigger" , `${ contentHash } .js ` ) ;
383
385
const sourceMapPath = `${ fullPath } .map` ;
384
386
385
387
const outputFileWithSourceMap = `${
@@ -389,6 +391,10 @@ function useDev({
389
391
await fs . promises . mkdir ( dirname ( fullPath ) , { recursive : true } ) ;
390
392
await fs . promises . writeFile ( fullPath , outputFileWithSourceMap ) ;
391
393
394
+ logger . debug ( `Wrote background worker to ${ fullPath } ` ) ;
395
+
396
+ const dependencies = gatherRequiredDependencies ( metaOutput ) ;
397
+
392
398
if ( sourceMapFile ) {
393
399
const sourceMapPath = `${ fullPath } .map` ;
394
400
await fs . promises . writeFile ( sourceMapPath , sourceMapFile . text ) ;
@@ -399,6 +405,7 @@ function useDev({
399
405
400
406
const backgroundWorker = new BackgroundWorker ( fullPath , {
401
407
projectDir : config . projectDir ,
408
+ dependencies,
402
409
env : {
403
410
TRIGGER_API_URL : apiUrl ,
404
411
TRIGGER_API_KEY : apiKey ,
@@ -608,3 +615,48 @@ function WebsocketFactory(apiKey: string) {
608
615
}
609
616
} ;
610
617
}
618
+
619
+ // Returns the dependencies that are required by the output that are found in output and the CLI package dependencies
620
+ // Returns the dependency names and the version to use (taken from the CLI deps package.json)
621
+ function gatherRequiredDependencies ( outputMeta : Metafile [ "outputs" ] [ string ] ) {
622
+ const dependencies : Record < string , string > = { } ;
623
+
624
+ for ( const file of outputMeta . imports ) {
625
+ if ( file . kind !== "require-call" || ! file . external ) {
626
+ continue ;
627
+ }
628
+
629
+ const packageName = detectPackageNameFromImportPath ( file . path ) ;
630
+
631
+ if ( dependencies [ packageName ] ) {
632
+ continue ;
633
+ }
634
+
635
+ const internalDependencyVersion = ( packageJson . dependencies as Record < string , string > ) [
636
+ packageName
637
+ ] ;
638
+
639
+ if ( internalDependencyVersion ) {
640
+ dependencies [ packageName ] = internalDependencyVersion ;
641
+ }
642
+ }
643
+
644
+ return dependencies ;
645
+ }
646
+
647
+ // Expects path to be in the format:
648
+ // - source-map-support/register.js
649
+ // - @opentelemetry/api
650
+ // - zod
651
+ //
652
+ // With the result being:
653
+ // - source-map-support
654
+ // - @opentelemetry/api
655
+ // - zod
656
+ function detectPackageNameFromImportPath ( path : string ) : string {
657
+ if ( path . startsWith ( "@" ) ) {
658
+ return path . split ( "/" ) . slice ( 0 , 2 ) . join ( "/" ) ;
659
+ } else {
660
+ return path . split ( "/" ) [ 0 ] as string ;
661
+ }
662
+ }
0 commit comments