Skip to content

Commit 333e611

Browse files
committed
Keep cross check with build imports to reduce bundle dependencies number
1 parent 21ffa71 commit 333e611

File tree

4 files changed

+118
-32
lines changed

4 files changed

+118
-32
lines changed

packages/cli-v3/e2e/compile.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { esbuildDecorators } from "@anatine/esbuild-decorators";
22
import { build } from "esbuild";
33
import { readFileSync } from "node:fs";
44
import { writeFile } from "node:fs/promises";
5-
import { join, resolve } from "node:path";
5+
import { join, posix, relative, resolve, sep } from "node:path";
66
import invariant from "tiny-invariant";
77

88
import {
@@ -163,6 +163,27 @@ export async function compile(options: CompileOptions) {
163163

164164
logger.debug(`Writing compiled files to ${tempDir}`);
165165

166+
// Get the metaOutput for the result build
167+
const pathsToProjectDir = relative(
168+
join(process.cwd(), "e2e", "fixtures"),
169+
config.projectDir
170+
).split(sep);
171+
172+
const metaOutput =
173+
result.metafile!.outputs[
174+
posix.join("e2e", "fixtures", ...pathsToProjectDir, "out", "stdin.js")
175+
];
176+
177+
invariant(metaOutput, "Meta output for the result build is missing");
178+
179+
// Get the metaOutput for the entryPoint build
180+
const entryPointMetaOutput =
181+
entryPointResult.metafile!.outputs[
182+
posix.join("e2e", "fixtures", ...pathsToProjectDir, "out", "stdin.js")
183+
];
184+
185+
invariant(entryPointMetaOutput, "Meta output for the entryPoint build is missing");
186+
166187
// Get the outputFile and the sourceMapFile for the result build
167188
const workerOutputFile = result.outputFiles.find(
168189
(file) => file.path === join(config.projectDir, "out", "stdin.js")
@@ -195,6 +216,8 @@ export async function compile(options: CompileOptions) {
195216
await writeFile(join(tempDir, "index.js"), entryPointOutputFile.text);
196217

197218
return {
219+
entryPointMetaOutput,
220+
workerMetaOutput: metaOutput,
198221
directDependenciesMeta,
199222
workerOutputFile,
200223
entryPointOutputFile,

packages/cli-v3/e2e/handleDependencies.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import { logger } from "../src/utilities/logger.js";
1616
import { cliLink } from "../src/utilities/cliOutput.js";
1717
import { E2EJavascriptProject } from "./javascriptProject.js";
1818
import { DependencyMeta } from "../src/utilities/javascriptProject.js";
19+
import { Metafile } from "esbuild";
1920

2021
type HandleDependenciesOptions = {
22+
entryPointMetaOutput: Metafile["outputs"]["out/stdin.js"];
23+
metaOutput: Metafile["outputs"]["out/stdin.js"];
2124
directDependenciesMeta: Record<string, DependencyMeta>;
2225
packageManager: PackageManager;
2326
resolvedConfig: ReadConfigResult;
@@ -29,15 +32,29 @@ export async function handleDependencies(options: HandleDependenciesOptions) {
2932
throw new Error("cannot resolve config");
3033
}
3134
const {
35+
entryPointMetaOutput,
36+
metaOutput,
3237
directDependenciesMeta,
3338
packageManager,
3439
resolvedConfig: { config },
3540
tempDir,
3641
} = options;
3742

43+
logger.debug("Getting the imports for the worker and entryPoint builds", {
44+
workerImports: metaOutput.imports,
45+
entryPointImports: entryPointMetaOutput.imports,
46+
});
47+
48+
// Get all the required dependencies from the metaOutputs and save them to /tmp/dir/package.json
49+
const allImports = [...metaOutput.imports, ...entryPointMetaOutput.imports];
50+
3851
const javascriptProject = new E2EJavascriptProject(config.projectDir, packageManager);
3952

40-
const dependencies = await resolveRequiredDependencies(directDependenciesMeta, config);
53+
const dependencies = await resolveRequiredDependencies(
54+
directDependenciesMeta,
55+
allImports,
56+
config
57+
);
4158

4259
logger.debug("gatherRequiredDependencies()", { dependencies });
4360

packages/cli-v3/e2e/index.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { createDeployHash } from "./createDeployHash";
1818
import { handleDependencies } from "./handleDependencies";
1919
import { E2EOptions, E2EOptionsSchema } from "./schemas";
2020
import { fixturesConfig, TestCase } from "./fixtures.config";
21-
import { OutputFile } from "esbuild";
21+
import { Metafile, OutputFile } from "esbuild";
2222
import { findUpMultiple } from "find-up";
2323
import { DependencyMeta } from "../src/utilities/javascriptProject";
2424

@@ -164,8 +164,10 @@ if (testCases.length > 0) {
164164
).resolves.not.toThrowError();
165165
}
166166

167+
let entryPointMetaOutput: Metafile["outputs"]["out/stdin.js"];
167168
let directDependenciesMeta: Record<string, DependencyMeta>;
168169
let entryPointOutputFile: OutputFile;
170+
let workerMetaOutput: Metafile["outputs"]["out/stdin.js"];
169171
let workerOutputFile: OutputFile;
170172

171173
const compileExpect = expect(
@@ -175,8 +177,10 @@ if (testCases.length > 0) {
175177
resolvedConfig: resolvedConfig!,
176178
tempDir,
177179
});
180+
entryPointMetaOutput = compilationResult.entryPointMetaOutput;
178181
directDependenciesMeta = compilationResult.directDependenciesMeta;
179182
entryPointOutputFile = compilationResult.entryPointOutputFile;
183+
workerMetaOutput = compilationResult.workerMetaOutput;
180184
workerOutputFile = compilationResult.workerOutputFile;
181185
})(),
182186
wantCompilationError ? "does not compile" : "compiles"
@@ -200,6 +204,8 @@ if (testCases.length > 0) {
200204
const depsExpectation = expect(
201205
(async () => {
202206
dependencies = await handleDependencies({
207+
entryPointMetaOutput: entryPointMetaOutput!,
208+
metaOutput: workerMetaOutput!,
203209
directDependenciesMeta: directDependenciesMeta!,
204210
resolvedConfig: resolvedConfig!,
205211
tempDir,

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)