Skip to content

Commit d1a5ec1

Browse files
committed
Keep cross check with build imports to reduce bundle dependencies number
1 parent b1982eb commit d1a5ec1

File tree

1 file changed

+69
-29
lines changed

1 file changed

+69
-29
lines changed

packages/cli-v3/src/commands/deploy.ts

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@trigger.dev/core/v3";
1010
import { recordSpanException } from "@trigger.dev/core/v3/workers";
1111
import { Command, Option as CommandOption } from "commander";
12-
import { build } from "esbuild";
12+
import { build, Metafile } from "esbuild";
1313
import { execa } from "execa";
1414
import { createHash } from "node:crypto";
1515
import { readFileSync } from "node:fs";
@@ -32,7 +32,11 @@ import {
3232
import { ReadConfigResult, readConfig } from "../utilities/configFiles.js";
3333
import { createTempDir, writeJSONFile } from "../utilities/fileSystem";
3434
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
35-
import { parsePackageName, stripWorkspaceFromVersion } from "../utilities/installPackages";
35+
import {
36+
detectPackageNameFromImportPath,
37+
parsePackageName,
38+
stripWorkspaceFromVersion,
39+
} from "../utilities/installPackages";
3640
import { logger } from "../utilities/logger.js";
3741
import { createTaskFileImports, gatherTaskFiles } from "../utilities/taskFiles";
3842
import { login } from "./login";
@@ -1286,6 +1290,17 @@ async function compileProject(
12861290

12871291
logger.debug(`Writing compiled files to ${tempDir}`);
12881292

1293+
// Get the metaOutput for the result build
1294+
const metaOutput = result.metafile!.outputs[posix.join("out", "stdin.js")];
1295+
1296+
invariant(metaOutput, "Meta output for the result build is missing");
1297+
1298+
// Get the metaOutput for the entryPoint build
1299+
const entryPointMetaOutput =
1300+
entryPointResult.metafile!.outputs[posix.join("out", "stdin.js")];
1301+
1302+
invariant(entryPointMetaOutput, "Meta output for the entryPoint build is missing");
1303+
12891304
// Get the outputFile and the sourceMapFile for the result build
12901305
const workerOutputFile = result.outputFiles.find(
12911306
(file) => file.path === join(config.projectDir, "out", "stdin.js")
@@ -1317,7 +1332,19 @@ async function compileProject(
13171332
// Save the entryPoint outputFile to /tmp/dir/index.js
13181333
await writeFile(join(tempDir, "index.js"), entryPointOutputFile.text);
13191334

1320-
const dependencies = await resolveRequiredDependencies(directDependenciesMeta, config);
1335+
logger.debug("Getting the imports for the worker and entryPoint builds", {
1336+
workerImports: metaOutput.imports,
1337+
entryPointImports: entryPointMetaOutput.imports,
1338+
});
1339+
1340+
// Get all the required dependencies from the metaOutputs and save them to /tmp/dir/package.json
1341+
const allImports = [...metaOutput.imports, ...entryPointMetaOutput.imports];
1342+
1343+
const dependencies = await resolveRequiredDependencies(
1344+
directDependenciesMeta,
1345+
allImports,
1346+
config
1347+
);
13211348

13221349
logger.debug("gatherRequiredDependencies()", { dependencies });
13231350

@@ -1688,41 +1715,54 @@ export async function typecheckProject(config: ResolvedConfig) {
16881715
// Returns the dependency names and the version to use (taken from the CLI deps package.json)
16891716
export async function resolveRequiredDependencies(
16901717
directDependenciesMeta: Record<string, DependencyMeta>,
1718+
imports: Metafile["outputs"][string]["imports"],
16911719
config: ResolvedConfig
16921720
) {
16931721
return await tracer.startActiveSpan("resolveRequiredDependencies", async (span) => {
1694-
const externalDirectDependenciesVersion = Object.fromEntries(
1695-
Object.entries(directDependenciesMeta)
1696-
.filter(([, { external }]) => external)
1697-
.map(([packageName, { version }]) => [packageName, version])
1698-
);
1699-
span.setAttribute("resolvablePackageNames", Object.keys(externalDirectDependenciesVersion));
1722+
const dependencies: Record<string, string> = {};
1723+
const missingPackages: string[] = [];
17001724

1701-
const missingPackages = Object.entries(externalDirectDependenciesVersion)
1702-
.filter(([, version]) => !version)
1703-
.map(([name]) => name);
1725+
for (const file of imports) {
1726+
if ((file.kind !== "require-call" && file.kind !== "dynamic-import") || !file.external) {
1727+
continue;
1728+
}
17041729

1705-
span.setAttributes({
1706-
...flattenAttributes(externalDirectDependenciesVersion, "resolvedPackageVersions"),
1707-
});
1708-
span.setAttribute("missingPackages", missingPackages);
1730+
const packageName = detectPackageNameFromImportPath(file.path);
17091731

1710-
const dependencies: Record<string, string> = {};
1732+
if (!packageName) {
1733+
continue;
1734+
}
17111735

1712-
for (const missingPackage of missingPackages) {
1713-
const internalDependencyVersion =
1714-
(packageJson.dependencies as Record<string, string>)[missingPackage] ??
1715-
detectDependencyVersion(missingPackage);
1736+
if (!directDependenciesMeta[packageName]) {
1737+
continue;
1738+
}
17161739

1717-
if (internalDependencyVersion) {
1718-
dependencies[missingPackage] = stripWorkspaceFromVersion(internalDependencyVersion);
1740+
if (!directDependenciesMeta[packageName].external) {
1741+
continue;
1742+
}
1743+
1744+
if (!directDependenciesMeta[packageName].version) {
1745+
missingPackages.push(packageName);
1746+
const internalDependencyVersion =
1747+
(packageJson.dependencies as Record<string, string>)[packageName] ??
1748+
detectDependencyVersion(packageName);
1749+
1750+
if (internalDependencyVersion) {
1751+
dependencies[packageName] = stripWorkspaceFromVersion(internalDependencyVersion);
1752+
}
1753+
1754+
continue;
17191755
}
1720-
}
17211756

1722-
for (const [packageName, version] of Object.entries(externalDirectDependenciesVersion)) {
1723-
dependencies[packageName] = version;
1757+
dependencies[packageName] = directDependenciesMeta[packageName].version;
17241758
}
17251759

1760+
span.setAttribute("resolvablePackageNames", Object.keys(dependencies));
1761+
span.setAttributes({
1762+
...flattenAttributes(dependencies, "resolvedPackageVersions"),
1763+
});
1764+
span.setAttribute("missingPackages", missingPackages);
1765+
17261766
if (config.additionalPackages) {
17271767
span.setAttribute("additionalPackages", config.additionalPackages);
17281768

@@ -1737,10 +1777,10 @@ export async function resolveRequiredDependencies(
17371777
dependencies[packageParts.name] = packageParts.version;
17381778
continue;
17391779
} else {
1740-
const externalDependencyVersion = externalDirectDependenciesVersion[packageParts.name];
1780+
const dependencyVersion = dependencies[packageParts.name];
17411781

1742-
if (externalDependencyVersion) {
1743-
dependencies[packageParts.name] = externalDependencyVersion;
1782+
if (dependencyVersion) {
1783+
dependencies[packageParts.name] = dependencyVersion;
17441784
continue;
17451785
} else {
17461786
logger.log(

0 commit comments

Comments
 (0)