From b5fa4a6c23213696023397ecaf64b61939b28a60 Mon Sep 17 00:00:00 2001 From: Andy Jessop Date: Wed, 16 Oct 2024 15:03:02 +0200 Subject: [PATCH] chore: fix entrypoint logic and refactor (#6971) * chore: fix entrypoint logic and refactor * chore: remove spurious files * Add test case --- .../startDevWorker/ConfigController.test.ts | 35 +++++++++++ .../wrangler/src/deployment-bundle/entry.ts | 62 +++++++++---------- .../src/deployment-bundle/resolve-entry.ts | 48 ++++++++++++++ 3 files changed, 113 insertions(+), 32 deletions(-) create mode 100644 packages/wrangler/src/deployment-bundle/resolve-entry.ts diff --git a/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts b/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts index ae4abd05adc7..e9cd0f67d1d0 100644 --- a/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts +++ b/packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts @@ -56,6 +56,41 @@ describe("ConfigController", () => { }); }); + it("should apply module root to parent if main is nested from base_dir", async () => { + const controller = new ConfigController(); + const event = waitForConfigUpdate(controller); + await seed({ + "some/base_dir/nested/index.js": dedent/* javascript */ ` + export default { + fetch(request, env, ctx) { + return new Response("hello world") + } + } + `, + "wrangler.toml": dedent` + main = \"./some/base_dir/nested/index.js\" +base_dir = \"./some/base_dir\"`, + }); + + const config: StartDevWorkerInput = {}; + + await controller.set(config); + + await expect(event).resolves.toMatchObject({ + type: "configUpdate", + config: { + build: { + additionalModules: [], + define: {}, + format: "modules", + moduleRoot: path.join(process.cwd(), "./some/base_dir"), + moduleRules: [], + }, + directory: process.cwd(), + entrypoint: path.join(process.cwd(), "./some/base_dir/nested/index.js"), + }, + }); + }); it("should shallow merge patched config", async () => { const controller = new ConfigController(); const event1 = waitForConfigUpdate(controller); diff --git a/packages/wrangler/src/deployment-bundle/entry.ts b/packages/wrangler/src/deployment-bundle/entry.ts index 6ded3b4f6a8d..8547ff368584 100644 --- a/packages/wrangler/src/deployment-bundle/entry.ts +++ b/packages/wrangler/src/deployment-bundle/entry.ts @@ -1,8 +1,13 @@ import path from "node:path"; import { UserError } from "../errors"; import { logger } from "../logger"; -import { getBasePath } from "../paths"; import guessWorkerFormat from "./guess-worker-format"; +import { + resolveEntryWithAssets, + resolveEntryWithEntryPoint, + resolveEntryWithMain, + resolveEntryWithScript, +} from "./resolve-entry"; import { runCustomBuild } from "./run-custom-build"; import type { Config } from "../config"; import type { DurableObjectBindings } from "../config/environment"; @@ -42,41 +47,33 @@ export async function getEntry( config: Config, command: "dev" | "deploy" | "versions upload" | "types" ): Promise { - let file: string; - let directory = process.cwd(); + const directory = process.cwd(); + const entryPoint = config.site?.["entry-point"]; + + let paths: { absolutePath: string; relativePath: string } | undefined; if (args.script) { - // If the script name comes from the command line it is relative to the current working directory. - file = path.resolve(args.script); - } else if (config.main === undefined) { - if (config.site?.["entry-point"]) { - directory = path.resolve(path.dirname(config.configPath ?? ".")); - file = path.extname(config.site?.["entry-point"]) - ? path.resolve(config.site?.["entry-point"]) - : // site.entry-point could be a directory - path.resolve(config.site?.["entry-point"], "index.js"); - } else if ( - args.legacyAssets || - config.legacy_assets || - args.assets || - config.assets - ) { - file = path.resolve(getBasePath(), "templates/no-op-worker.js"); - } else { - throw new UserError( - `Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.` - ); - } + paths = resolveEntryWithScript(args.script); + } else if (config.main !== undefined) { + paths = resolveEntryWithMain(config.main, config.configPath); + } else if (entryPoint) { + paths = resolveEntryWithEntryPoint(entryPoint, config.configPath); + } else if ( + args.legacyAssets || + config.legacy_assets || + args.assets || + config.assets + ) { + paths = resolveEntryWithAssets(); } else { - directory = path.resolve(path.dirname(config.configPath ?? ".")); - file = path.resolve(directory, config.main); + throw new UserError( + `Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.` + ); } - - const relativeFile = path.relative(directory, file) || "."; - await runCustomBuild(file, relativeFile, config.build); + await runCustomBuild(paths.absolutePath, paths.relativePath, config.build); const format = await guessWorkerFormat( - file, + paths.absolutePath, directory, args.format ?? config.build?.upload?.format, config.tsconfig @@ -110,10 +107,11 @@ export async function getEntry( } return { - file, + file: paths.absolutePath, directory, format, - moduleRoot: args.moduleRoot ?? config.base_dir ?? path.dirname(file), + moduleRoot: + args.moduleRoot ?? config.base_dir ?? path.dirname(paths.absolutePath), name: config.name ?? "worker", }; } diff --git a/packages/wrangler/src/deployment-bundle/resolve-entry.ts b/packages/wrangler/src/deployment-bundle/resolve-entry.ts new file mode 100644 index 000000000000..5a9efb535194 --- /dev/null +++ b/packages/wrangler/src/deployment-bundle/resolve-entry.ts @@ -0,0 +1,48 @@ +import path from "path"; +import { getBasePath } from "../paths"; + +export function resolveEntryWithScript(script: string): { + absolutePath: string; + relativePath: string; +} { + const file = path.resolve(script); + const relativePath = path.relative(process.cwd(), file) || "."; + return { absolutePath: file, relativePath }; +} + +export function resolveEntryWithMain( + main: string, + configPath?: string +): { + absolutePath: string; + relativePath: string; +} { + const directory = path.resolve(path.dirname(configPath ?? ".")); + const file = path.resolve(directory, main); + const relativePath = path.relative(directory, file) || "."; + return { absolutePath: file, relativePath }; +} + +export function resolveEntryWithEntryPoint( + entryPoint: string, + configPath?: string +): { + absolutePath: string; + relativePath: string; +} { + const directory = path.resolve(path.dirname(configPath ?? ".")); + const file = path.extname(entryPoint) + ? path.resolve(entryPoint) + : path.resolve(entryPoint, "index.js"); + const relativePath = path.relative(directory, file) || "."; + return { absolutePath: file, relativePath }; +} + +export function resolveEntryWithAssets(): { + absolutePath: string; + relativePath: string; +} { + const file = path.resolve(getBasePath(), "templates/no-op-worker.js"); + const relativePath = path.relative(process.cwd(), file) || "."; + return { absolutePath: file, relativePath }; +}