cleaned up some repetition and structure of the entry point stuff

This commit is contained in:
Eric Allam
2024-08-07 14:58:20 +01:00
committed by Eric Allam
parent f0898714ae
commit 5bc482e3bc
9 changed files with 100 additions and 78 deletions
-1
View File
@@ -7,5 +7,4 @@ tailwind.css
**/.react-email/
**/storybook-static/
**/.changeset/
**/build/
**/dist/
+64 -50
View File
@@ -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<void>) | undefined;
};
export async function bundleWorker(
options: BundleOptions
): Promise<BundleResult> {
export async function bundleWorker(options: BundleOptions): Promise<BundleResult> {
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<BundleResult, "stop"> | undefined {
const files: Array<{ entry: string; out: string }> = [];
const imports = new Set<string>();
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);
}
}
+9 -6
View File
@@ -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<BuildManifest> {
export async function copyManifestToDir(
manifest: BuildManifest,
source: string,
destination: string
): Promise<BuildManifest> {
// 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;
}
}
+21 -13
View File
@@ -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
]
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"));
}
+4 -5
View File
@@ -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: {},
+2 -3
View File
@@ -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({