Skip to content

Commit 6955a2b

Browse files
committed
Use direct dependencies info in esbuild plugin
1 parent 393b827 commit 6955a2b

File tree

8 files changed

+194
-20
lines changed

8 files changed

+194
-20
lines changed

packages/cli-v3/e2e/compile.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import { writeJSONFile } from "../src/utilities/fileSystem.js";
1515
import { logger } from "../src/utilities/logger.js";
1616
import { createTaskFileImports, gatherTaskFiles } from "../src/utilities/taskFiles.js";
1717
import { escapeImportPath } from "../src/utilities/windows.js";
18+
import { E2EJavascriptProject } from "./javascriptProject.js";
19+
import { PackageManager } from "../src/utilities/getUserPackageManager.js";
1820

1921
type CompileOptions = {
2022
outputMetafile?: string;
23+
packageManager: PackageManager;
2124
resolvedConfig: ReadConfigResult;
2225
tempDir: string;
2326
};
@@ -28,6 +31,7 @@ export async function compile(options: CompileOptions) {
2831
}
2932

3033
const {
34+
packageManager,
3135
tempDir,
3236
resolvedConfig: { config },
3337
} = options;
@@ -61,6 +65,10 @@ export async function compile(options: CompileOptions) {
6165
);
6266
}
6367

68+
const e2eJsProject = new E2EJavascriptProject(config.projectDir, packageManager);
69+
const directDependencies = await e2eJsProject.resolveDirectDependencies();
70+
console.log("DIRECT DEPS", directDependencies);
71+
6472
const result = await build({
6573
stdin: {
6674
contents: workerContents,
@@ -86,7 +94,12 @@ export async function compile(options: CompileOptions) {
8694
},
8795
plugins: [
8896
mockServerOnlyPlugin(),
89-
bundleDependenciesPlugin("workerFacade", config.dependenciesToBundle, config.tsconfigPath),
97+
bundleDependenciesPlugin(
98+
"workerFacade",
99+
directDependencies,
100+
config.dependenciesToBundle,
101+
config.tsconfigPath
102+
),
90103
workerSetupImportConfigPlugin(configPath),
91104
esbuildDecorators({
92105
tsconfig: config.tsconfigPath,
@@ -127,7 +140,12 @@ export async function compile(options: CompileOptions) {
127140
__PROJECT_CONFIG__: JSON.stringify(config),
128141
},
129142
plugins: [
130-
bundleDependenciesPlugin("entryPoint.ts", config.dependenciesToBundle, config.tsconfigPath),
143+
bundleDependenciesPlugin(
144+
"entryPoint.ts",
145+
directDependencies,
146+
config.dependenciesToBundle,
147+
config.tsconfigPath
148+
),
131149
],
132150
});
133151

packages/cli-v3/e2e/fixtures.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export const fixturesConfig: TestCase[] = [
1616
id: "compile-monorepo-packages",
1717
skipTypecheck: true,
1818
workspaceRelativeDir: "packages/trigger",
19-
// TODO remove
20-
wantInstallationError: true,
2119
},
2220
{
2321
id: "config-infisical-sdk",

packages/cli-v3/e2e/handleDependencies.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
import { ReadConfigResult } from "../src/utilities/configFiles.js";
1414
import { writeJSONFile } from "../src/utilities/fileSystem.js";
1515
import { PackageManager } from "../src/utilities/getUserPackageManager.js";
16-
import { JavascriptProject } from "../src/utilities/javascriptProject.js";
1716
import { logger } from "../src/utilities/logger.js";
1817
import { cliLink } from "../src/utilities/cliOutput.js";
18+
import { E2EJavascriptProject } from "./javascriptProject.js";
1919

2020
type HandleDependenciesOptions = {
2121
entryPointMetaOutput: Metafile["outputs"]["out/stdin.js"];
@@ -25,19 +25,6 @@ type HandleDependenciesOptions = {
2525
tempDir: string;
2626
};
2727

28-
class JavascriptProjectLocal extends JavascriptProject {
29-
constructor(
30-
projectPath: string,
31-
private overridenPackageManager: PackageManager
32-
) {
33-
super(projectPath);
34-
}
35-
36-
async getPackageManager(): Promise<PackageManager> {
37-
return Promise.resolve(this.overridenPackageManager);
38-
}
39-
}
40-
4128
export async function handleDependencies(options: HandleDependenciesOptions) {
4229
if (options.resolvedConfig.status === "error") {
4330
throw new Error("cannot resolve config");
@@ -58,7 +45,7 @@ export async function handleDependencies(options: HandleDependenciesOptions) {
5845
// Get all the required dependencies from the metaOutputs and save them to /tmp/dir/package.json
5946
const allImports = [...metaOutput.imports, ...entryPointMetaOutput.imports];
6047

61-
const javascriptProject = new JavascriptProjectLocal(config.projectDir, packageManager);
48+
const javascriptProject = new E2EJavascriptProject(config.projectDir, packageManager);
6249

6350
const dependencies = await resolveRequiredDependencies(allImports, config, javascriptProject);
6451

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { handleDependencies } from "./handleDependencies";
1919
import { E2EOptions, E2EOptionsSchema } from "./schemas";
2020
import { fixturesConfig, TestCase } from "./fixtures.config";
2121
import { Metafile, OutputFile } from "esbuild";
22-
import { findUp, findUpMultiple } from "find-up";
22+
import { findUpMultiple } from "find-up";
2323

2424
interface E2EFixtureTest extends TestCase {
2525
fixtureDir: string;
@@ -173,6 +173,7 @@ if (testCases.length > 0) {
173173
const compileExpect = expect(
174174
(async () => {
175175
const compilationResult = await compile({
176+
packageManager,
176177
resolvedConfig: resolvedConfig!,
177178
tempDir,
178179
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PackageManager } from "../src/utilities/getUserPackageManager.js";
2+
import { JavascriptProject } from "../src/utilities/javascriptProject.js";
3+
4+
export class E2EJavascriptProject extends JavascriptProject {
5+
constructor(
6+
projectPath: string,
7+
private overridenPackageManager: PackageManager
8+
) {
9+
super(projectPath);
10+
}
11+
12+
async getPackageManager(): Promise<PackageManager> {
13+
return Promise.resolve(this.overridenPackageManager);
14+
}
15+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,9 @@ async function compileProject(
11541154
);
11551155
}
11561156

1157+
const jsProject = new JavascriptProject(config.projectDir);
1158+
const directDependencies = await jsProject.resolveDirectDependencies();
1159+
11571160
const result = await build({
11581161
stdin: {
11591162
contents: workerContents,
@@ -1181,6 +1184,7 @@ async function compileProject(
11811184
mockServerOnlyPlugin(),
11821185
bundleDependenciesPlugin(
11831186
"workerFacade",
1187+
directDependencies,
11841188
config.dependenciesToBundle,
11851189
config.tsconfigPath
11861190
),
@@ -1237,6 +1241,7 @@ async function compileProject(
12371241
plugins: [
12381242
bundleDependenciesPlugin(
12391243
"entryPoint.ts",
1244+
directDependencies,
12401245
config.dependenciesToBundle,
12411246
config.tsconfigPath
12421247
),

packages/cli-v3/src/utilities/build.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ export function workerSetupImportConfigPlugin(configPath?: string): Plugin {
106106

107107
export function bundleDependenciesPlugin(
108108
buildIdentifier: string,
109+
dependencies: Record<
110+
string,
111+
{
112+
version: string;
113+
external: boolean;
114+
}
115+
>,
109116
dependenciesToBundle?: Array<string | RegExp>,
110117
tsconfigPath?: string
111118
): Plugin {
@@ -149,6 +156,10 @@ export function bundleDependenciesPlugin(
149156
}
150157
}
151158

159+
if (dependencies[args.path] && !dependencies[args.path]!.external) {
160+
return undefined; // let esbuild bundle it
161+
}
162+
152163
logger.debug(`[${buildIdentifier}] Externalizing ${args.path}`, {
153164
...args,
154165
});

packages/cli-v3/src/utilities/javascriptProject.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,33 @@ export class JavascriptProject {
9494
}
9595
}
9696

97+
async resolveDirectDependencies(): Promise<
98+
Record<string, { version: string; external: boolean }>
99+
> {
100+
return tracer.startActiveSpan("JavascriptProject.resolveInternal", async (span) => {
101+
const command = await this.#getCommand();
102+
103+
span.setAttributes({
104+
packageManager: command.name,
105+
});
106+
107+
try {
108+
return await command.resolveDirectDependencies({
109+
cwd: this.projectPath,
110+
});
111+
} catch (error) {
112+
recordSpanException(span, error);
113+
span.end();
114+
115+
logger.debug(`Failed to resolve internal dependencies using ${command.name}`, {
116+
error,
117+
});
118+
119+
throw error;
120+
}
121+
});
122+
}
123+
97124
async resolveAll(
98125
packageNames: string[],
99126
options?: ResolveOptions
@@ -271,6 +298,10 @@ interface PackageManagerCommands {
271298

272299
installDependencies(options: PackageManagerOptions): Promise<void>;
273300

301+
resolveDirectDependencies(
302+
options: PackageManagerOptions
303+
): Promise<Record<string, { version: string; external: boolean }>>;
304+
274305
resolveDependencyVersion(
275306
packageName: string,
276307
options: PackageManagerOptions
@@ -337,6 +368,46 @@ class PNPMCommands implements PackageManagerCommands {
337368
return results;
338369
}
339370

371+
async resolveDirectDependencies(options: PackageManagerOptions) {
372+
const result = await this.#listDirectDependencies(options);
373+
374+
logger.debug(`Resolving direct dependencies using ${this.name}`);
375+
376+
const results: Record<string, { version: string; external: boolean }> = {};
377+
378+
for (const projectPkg of result) {
379+
results[projectPkg.name] = { version: projectPkg.version, external: false };
380+
381+
if (projectPkg.dependencies) {
382+
for (const [name, dep] of Object.entries(projectPkg.dependencies)) {
383+
const { version } = dep;
384+
385+
results[name] = {
386+
version,
387+
external: !version.startsWith("link:"),
388+
};
389+
}
390+
}
391+
}
392+
393+
return results;
394+
}
395+
396+
async #listDirectDependencies(options: PackageManagerOptions) {
397+
const childProcess = await $({
398+
cwd: options.cwd,
399+
reject: false,
400+
})`${this.cmd} list --recursive --json`;
401+
402+
if (childProcess.failed) {
403+
logger.debug("Failed to list dependencies, using stdout anyway...", {
404+
error: childProcess.stderr,
405+
});
406+
}
407+
408+
return JSON.parse(childProcess.stdout) as PnpmList;
409+
}
410+
340411
async #listDependencies(packageNames: string[], options: PackageManagerOptions) {
341412
const childProcess = await $({
342413
cwd: options.cwd,
@@ -410,6 +481,29 @@ class NPMCommands implements PackageManagerCommands {
410481
return results;
411482
}
412483

484+
async resolveDirectDependencies(options: PackageManagerOptions) {
485+
const result = await this.#listDirectDependencies(options);
486+
487+
logger.debug(`Resolving direct dependencies using ${this.name}`);
488+
489+
return this.#flattenDirectDependencies(result.dependencies);
490+
}
491+
492+
async #listDirectDependencies(options: PackageManagerOptions) {
493+
const childProcess = await $({
494+
cwd: options.cwd,
495+
reject: false,
496+
})`${this.cmd} list --json`;
497+
498+
if (childProcess.failed) {
499+
logger.debug("Failed to list dependencies, using stdout anyway...", {
500+
error: childProcess.stderr,
501+
});
502+
}
503+
504+
return JSON.parse(childProcess.stdout) as NpmListOutput;
505+
}
506+
413507
async #listDependencies(packageNames: string[], options: PackageManagerOptions) {
414508
const childProcess = await $({
415509
cwd: options.cwd,
@@ -443,6 +537,23 @@ class NPMCommands implements PackageManagerCommands {
443537
}
444538
}
445539
}
540+
541+
#flattenDirectDependencies(
542+
dependencies: Record<string, NpmDependency>
543+
): Record<string, { version: string; external: boolean }> {
544+
let results: Record<string, { version: string; external: boolean }> = {};
545+
546+
for (const [name, dep] of Object.entries(dependencies)) {
547+
const { version, resolved, dependencies } = dep;
548+
results[name] = { version, external: !resolved.startsWith("file:") };
549+
550+
if (dependencies) {
551+
results = { ...results, ...this.#flattenDirectDependencies(dependencies) };
552+
}
553+
}
554+
555+
return results;
556+
}
446557
}
447558

448559
class YarnCommands implements PackageManagerCommands {
@@ -501,6 +612,34 @@ class YarnCommands implements PackageManagerCommands {
501612
return results;
502613
}
503614

615+
async resolveDirectDependencies(options: PackageManagerOptions) {
616+
const result = await this.#listDirectDependencies(options);
617+
console.log("LIST DIRECT DEPS", JSON.stringify(result, undefined, 2));
618+
619+
logger.debug(`Resolving direct dependencies using ${this.name}`);
620+
621+
const results: Record<string, { version: string; external: boolean }> = {};
622+
623+
// TODO yarn
624+
625+
return results;
626+
}
627+
628+
async #listDirectDependencies(options: PackageManagerOptions) {
629+
const childProcess = await $({
630+
cwd: options.cwd,
631+
reject: false,
632+
})`${this.cmd} info --all --json`;
633+
634+
if (childProcess.failed) {
635+
logger.debug("Failed to list dependencies, using stdout anyway...", {
636+
error: childProcess.stderr,
637+
});
638+
}
639+
640+
return childProcess.stdout;
641+
}
642+
504643
async #listDependencies(packageNames: string[], options: PackageManagerOptions) {
505644
const childProcess = await $({
506645
cwd: options.cwd,

0 commit comments

Comments
 (0)