diff --git a/.prettierignore b/.prettierignore index 7c73a4c74..a34447dd4 100644 --- a/.prettierignore +++ b/.prettierignore @@ -7,5 +7,4 @@ tailwind.css **/.react-email/ **/storybook-static/ **/.changeset/ -**/build/ **/dist/ \ No newline at end of file diff --git a/packages/cli-v3/src/build/bundle.ts b/packages/cli-v3/src/build/bundle.ts index 4aba4ee4a..fcaae5bf4 100644 --- a/packages/cli-v3/src/build/bundle.ts +++ b/packages/cli-v3/src/build/bundle.ts @@ -3,7 +3,16 @@ import { BuildTarget, TaskFile } from "@trigger.dev/core/v3/schemas"; import * as esbuild from "esbuild"; import { join, resolve } from "node:path"; import { logger } from "../utilities/logger.js"; -import { packageModules, shims } from "./packageModules.js"; +import { + deployEntryPoint, + deployEntryPoints, + devEntryPoint, + devEntryPoints, + isDeployEntryPoint, + isDevEntryPoint, + isLoaderEntryPoint, + shims, +} from "./packageModules.js"; import { buildPlugins } from "./plugins.js"; export interface BundleOptions { @@ -21,22 +30,19 @@ export interface BundleOptions { export type BundleResult = { files: TaskFile[]; configPath: string; - loaderPath: string | undefined; - workerProdPath: string | undefined; - workerDevPath: string | undefined; + loaderEntryPoint: string | undefined; + workerEntryPoint: string | undefined; stop: (() => Promise) | undefined; }; -export async function bundleWorker( - options: BundleOptions -): Promise { +export async function bundleWorker(options: BundleOptions): Promise { const { resolvedConfig } = options; // We need to add the package entry points here somehow // Then we need to get them out of the build result into the build manifest // taskhero/dist/esm/workers/dev.js // taskhero/dist/esm/telemetry/loader.js - const entryPoints = await getEntryPoints(resolvedConfig); + const entryPoints = await getEntryPoints(options.target, resolvedConfig); const $buildPlugins = await buildPlugins(options.target, resolvedConfig); let initialBuildResult: (result: esbuild.BuildResult) => void; @@ -81,7 +87,7 @@ export async function bundleWorker( ...(options.jsxFragment && { jsxFragment: options.jsxFragment }), logLevel: "silent", logOverride: { - 'empty-glob': 'silent', + "empty-glob": "silent", }, }; @@ -108,7 +114,7 @@ export async function bundleWorker( stop = async function () {}; } - const bundleResult = getBundleResultFromBuild(options.cwd, result); + const bundleResult = getBundleResultFromBuild(options.target, options.cwd, result); if (!bundleResult) { throw new Error("Failed to get bundle result"); @@ -118,45 +124,35 @@ export async function bundleWorker( } export function getBundleResultFromBuild( + target: BuildTarget, workingDir: string, result: esbuild.BuildResult<{ metafile: true }> ): Omit | undefined { const files: Array<{ entry: string; out: string }> = []; - const imports = new Set(); - let configPath: string | undefined; - let loaderPath: string | undefined; - let workerDevPath: string | undefined; - let workerProdPath: string | undefined; - for (const [outputPath, outputMeta] of Object.entries( - result.metafile.outputs - )) { + let configPath: string | undefined; + let loaderEntryPoint: string | undefined; + let workerEntryPoint: string | undefined; + + for (const [outputPath, outputMeta] of Object.entries(result.metafile.outputs)) { if (outputPath.endsWith(".mjs")) { const $outputPath = resolve(workingDir, outputPath); - if (outputMeta.entryPoint) { - if (outputMeta.entryPoint.startsWith("trigger.config.ts")) { - configPath = $outputPath; - } else if ( - outputMeta.entryPoint.includes( - "dist/esm/telemetry/loader.js" - ) - ) { - loaderPath = $outputPath; - } else if ( - outputMeta.entryPoint.includes("dist/esm/workers/dev.js") - ) { - workerDevPath = $outputPath; - } else if ( - outputMeta.entryPoint.includes("dist/esm/workers/prod.js") - ) { - workerProdPath = $outputPath; - } else { - files.push({ - entry: outputMeta.entryPoint, - out: $outputPath, - }); - } + if (!outputMeta.entryPoint) { + continue; + } + + if (isConfigEntryPoint(outputMeta.entryPoint)) { + configPath = $outputPath; + } else if (isLoaderEntryPoint(outputMeta.entryPoint)) { + loaderEntryPoint = $outputPath; + } else if (isEntryPointForTarget(outputMeta.entryPoint, target)) { + workerEntryPoint = $outputPath; + } else { + files.push({ + entry: outputMeta.entryPoint, + out: $outputPath, + }); } } } @@ -168,31 +164,49 @@ export function getBundleResultFromBuild( return { files, configPath: configPath, - loaderPath, - workerDevPath, - workerProdPath, + loaderEntryPoint, + workerEntryPoint, }; } -async function getEntryPoints(config: ResolvedConfig) { +function isEntryPointForTarget(entryPoint: string, target: BuildTarget) { + if (target === "dev") { + return isDevEntryPoint(entryPoint); + } else { + return isDeployEntryPoint(entryPoint); + } +} + +function isConfigEntryPoint(entryPoint: string) { + return entryPoint.startsWith("trigger.config.ts"); +} + +async function getEntryPoints(target: BuildTarget, config: ResolvedConfig) { const projectEntryPoints = config.dirs.flatMap((dir) => dirToEntryPointGlob(dir)); if (config.configFile) { projectEntryPoints.push(config.configFile); } - projectEntryPoints.push(...packageModules); + if (target === "dev") { + projectEntryPoints.push(...devEntryPoints); + } else { + projectEntryPoints.push(...deployEntryPoints); + } return projectEntryPoints; } // Converts a directory to a glob that matches all the entry points in that function dirToEntryPointGlob(dir: string): string[] { - return [join(dir, "**", "*.ts"), join(dir, "**", "*.tsx"), join(dir, "**", "*.js"), join(dir, "**", "*.jsx")]; + return [ + join(dir, "**", "*.ts"), + join(dir, "**", "*.tsx"), + join(dir, "**", "*.js"), + join(dir, "**", "*.jsx"), + ]; } - - export function logBuildWarnings(warnings: esbuild.Message[]) { const logs = esbuild.formatMessagesSync(warnings, { kind: "warning", color: true }); for (const log of logs) { @@ -210,4 +224,4 @@ export function logBuildFailure(errors: esbuild.Message[], warnings: esbuild.Mes console.error(log); } logBuildWarnings(warnings); -} \ No newline at end of file +} diff --git a/packages/cli-v3/src/build/manifests.ts b/packages/cli-v3/src/build/manifests.ts index 090d41ff0..1a9fd620e 100644 --- a/packages/cli-v3/src/build/manifests.ts +++ b/packages/cli-v3/src/build/manifests.ts @@ -2,19 +2,22 @@ import { BuildManifest } from "@trigger.dev/core/v3/schemas"; import { cp } from "node:fs/promises"; import { logger } from "../utilities/logger.js"; -export async function copyManifestToDir(manifest: BuildManifest, source: string, destination: string): Promise { +export async function copyManifestToDir( + manifest: BuildManifest, + source: string, + destination: string +): Promise { // Copy the dir in destination to workerDir await cp(source, destination, { recursive: true }); logger.debug("Copied manifest to dir", { source, destination }); - + // Then update the manifest to point to the new workerDir const updatedManifest = { ...manifest }; updatedManifest.configPath = updatedManifest.configPath.replace(source, destination); - updatedManifest.loaderPath = updatedManifest.loaderPath?.replace(source, destination); - updatedManifest.workerEntryPath = updatedManifest.workerEntryPath?.replace(source, destination); - updatedManifest.workerForkPath = updatedManifest.workerForkPath?.replace(source, destination); + updatedManifest.loaderEntryPoint = updatedManifest.loaderEntryPoint?.replace(source, destination); + updatedManifest.workerEntryPoint = updatedManifest.workerEntryPoint?.replace(source, destination); updatedManifest.files = updatedManifest.files.map((file) => { return { @@ -26,4 +29,4 @@ export async function copyManifestToDir(manifest: BuildManifest, source: string, updatedManifest.outputPath = destination; return updatedManifest; -} \ No newline at end of file +} diff --git a/packages/cli-v3/src/build/packageModules.ts b/packages/cli-v3/src/build/packageModules.ts index f5d99852a..3580e5248 100644 --- a/packages/cli-v3/src/build/packageModules.ts +++ b/packages/cli-v3/src/build/packageModules.ts @@ -1,18 +1,26 @@ -import { join } from "node:path"; +import { join, relative } from "node:path"; import { sourceDir } from "../sourceDir.js"; -export const devEntryPoint = join(sourceDir, "workers", "dev.js") -export const prodEntryPoint = join(sourceDir, "workers", "prod.js") -export const telemetryLoader = join(sourceDir, "telemetry", "loader.js") +export const devEntryPoint = join(sourceDir, "entryPoints", "dev.js"); +export const deployEntryPoint = join(sourceDir, "entryPoints", "deploy.js"); +export const telemetryEntryPoint = join(sourceDir, "entryPoints", "loader.js"); -export const packageModules = [ - devEntryPoint, - prodEntryPoint, - telemetryLoader, -] +export const devEntryPoints = [devEntryPoint, telemetryEntryPoint]; -export const esmShimPath = join(sourceDir, "shims", "esm.js") +export const deployEntryPoints = [devEntryPoint, deployEntryPoint, telemetryEntryPoint]; -export const shims = [ - esmShimPath -] \ No newline at end of file +export const esmShimPath = join(sourceDir, "shims", "esm.js"); + +export const shims = [esmShimPath]; + +export function isDevEntryPoint(entryPoint: string) { + return entryPoint.includes(join("dist", "esm", "entryPoints", "dev.js")); +} + +export function isDeployEntryPoint(entryPoint: string) { + return entryPoint.includes(join("dist", "esm", "entryPoints", "deploy.js")); +} + +export function isLoaderEntryPoint(entryPoint: string) { + return entryPoint.includes(join("dist", "esm", "entryPoints", "loader.js")); +} diff --git a/packages/cli-v3/src/dev/devSession.ts b/packages/cli-v3/src/dev/devSession.ts index 873d5a42e..e5fab6a3c 100644 --- a/packages/cli-v3/src/dev/devSession.ts +++ b/packages/cli-v3/src/dev/devSession.ts @@ -16,7 +16,7 @@ import { resolvePluginsForContext, } from "../build/extensions.js"; import { createExternalsBuildExtension } from "../build/externals.js"; -import { devEntryPoint, telemetryLoader } from "../build/packageModules.js"; +import { devEntryPoint, telemetryEntryPoint } from "../build/packageModules.js"; import { type DevCommandOptions } from "../commands/dev.js"; import { logger } from "../utilities/logger.js"; import { EphemeralDirectory, getTmpDir } from "../utilities/tempDirectories.js"; @@ -60,7 +60,7 @@ export async function startDevSession({ rawConfig }: DevSessionOptions) { } async function updateBuild(build: esbuild.BuildResult, workerDir: EphemeralDirectory) { - const bundle = getBundleResultFromBuild(rawConfig.workingDir, build); + const bundle = getBundleResultFromBuild("dev", rawConfig.workingDir, build); if (bundle) { await updateBundle({ ...bundle, stop: undefined }, workerDir); @@ -141,9 +141,8 @@ async function createBuildManifestFromBundle( externals: [], config: resolvedConfig, outputPath: destination, - workerEntryPath: bundle.workerDevPath ?? devEntryPoint, - workerForkPath: bundle.workerDevPath ?? devEntryPoint, - loaderPath: bundle.loaderPath ?? telemetryLoader, + workerEntryPoint: bundle.workerEntryPoint ?? devEntryPoint, + loaderEntryPoint: bundle.loaderEntryPoint ?? telemetryEntryPoint, configPath: bundle.configPath, deploy: { env: {}, diff --git a/packages/cli-v3/src/workers/dev.ts b/packages/cli-v3/src/entryPoints/deploy.ts similarity index 100% rename from packages/cli-v3/src/workers/dev.ts rename to packages/cli-v3/src/entryPoints/deploy.ts diff --git a/packages/cli-v3/src/workers/prod.ts b/packages/cli-v3/src/entryPoints/dev.ts similarity index 100% rename from packages/cli-v3/src/workers/prod.ts rename to packages/cli-v3/src/entryPoints/dev.ts diff --git a/packages/cli-v3/src/telemetry/loader.ts b/packages/cli-v3/src/entryPoints/loader.ts similarity index 100% rename from packages/cli-v3/src/telemetry/loader.ts rename to packages/cli-v3/src/entryPoints/loader.ts diff --git a/packages/core/src/v3/schemas/build.ts b/packages/core/src/v3/schemas/build.ts index 72e65df1b..ab5ec93b8 100644 --- a/packages/core/src/v3/schemas/build.ts +++ b/packages/core/src/v3/schemas/build.ts @@ -29,9 +29,8 @@ export const BuildManifest = z.object({ config: ConfigManifest, files: z.array(TaskFile), outputPath: z.string(), - workerEntryPath: z.string(), - workerForkPath: z.string(), - loaderPath: z.string().optional(), + workerEntryPoint: z.string(), + loaderEntryPoint: z.string().optional(), configPath: z.string(), externals: BuildExternal.array().optional(), build: z.object({