diff --git a/packages/wrangler/src/__tests__/get-entry.test.ts b/packages/wrangler/src/__tests__/get-entry.test.ts new file mode 100644 index 000000000000..534cc74e2f83 --- /dev/null +++ b/packages/wrangler/src/__tests__/get-entry.test.ts @@ -0,0 +1,128 @@ +import { writeFile } from "fs/promises"; +import path from "path"; +import dedent from "ts-dedent"; +import { Config } from "../config"; +import { defaultWranglerConfig } from "../config/config"; +import { Entry, getEntry } from "../deployment-bundle/entry"; +import guessWorkerFormat from "../deployment-bundle/guess-worker-format"; +import { mockConsoleMethods } from "./helpers/mock-console"; +import { runInTempDir } from "./helpers/run-in-tmp"; +import { seed } from "./helpers/seed"; + +function normalize(entry: Entry): Entry { + return JSON.parse( + JSON.stringify(entry).replaceAll(process.cwd(), "/tmp/dir") + ); +} +describe("getEntry()", () => { + runInTempDir(); + mockConsoleMethods(); + it.each([ + [ + "--script index.ts", + { + "index.ts": dedent/* javascript */ ` + export default { + fetch() { + + } + } + `, + }, + { script: "index.ts" }, + {}, + { + directory: "/tmp/dir", + file: "/tmp/dir/index.ts", + moduleRoot: "/tmp/dir", + }, + ], + [ + "--script src/index.ts", + { + "src/index.ts": dedent/* javascript */ ` + export default { + fetch() { + + } + } + `, + }, + { script: "src/index.ts" }, + {}, + { + directory: "/tmp/dir", + file: "/tmp/dir/src/index.ts", + moduleRoot: "/tmp/dir/src", + }, + ], + [ + "main = index.ts", + { + "index.ts": dedent/* javascript */ ` + export default { + fetch() { + + } + } + `, + }, + {}, + { main: "index.ts" }, + { + directory: "/tmp/dir", + file: "/tmp/dir/index.ts", + moduleRoot: "/tmp/dir", + }, + ], + [ + "main = src/index.ts", + { + "src/index.ts": dedent/* javascript */ ` + export default { + fetch() { + + } + } + `, + }, + {}, + { main: "src/index.ts" }, + { + directory: "/tmp/dir", + file: "/tmp/dir/src/index.ts", + moduleRoot: "/tmp/dir/src", + }, + ], + [ + "main = src/index.ts w/ configPath", + { + "other-worker/src/index.ts": dedent/* javascript */ ` + export default { + fetch() { + + } + } + `, + }, + {}, + { + main: "src/index.ts", + configPath: "other-worker/wrangler.toml", + }, + { + directory: "/tmp/dir/other-worker", + file: "/tmp/dir/other-worker/src/index.ts", + moduleRoot: "/tmp/dir/other-worker/src", + }, + ], + ])("%s", async (_name, files, args, config, result) => { + await seed(files); + const entry = await getEntry( + args, + { ...defaultWranglerConfig, ...config }, + "deploy" + ); + expect(normalize(entry)).toMatchObject(result); + }); +}); diff --git a/packages/wrangler/src/deployment-bundle/entry.ts b/packages/wrangler/src/deployment-bundle/entry.ts index 1be44b13f210..4e5e40a711e9 100644 --- a/packages/wrangler/src/deployment-bundle/entry.ts +++ b/packages/wrangler/src/deployment-bundle/entry.ts @@ -53,7 +53,9 @@ export async function getEntry( const directory = process.cwd(); const entryPoint = config.site?.["entry-point"]; - let paths: { absolutePath: string; relativePath: string } | undefined; + let paths: + | { absolutePath: string; relativePath: string; directory?: string } + | undefined; if (args.script) { paths = resolveEntryWithScript(args.script); @@ -81,9 +83,10 @@ export async function getEntry( } await runCustomBuild(paths.absolutePath, paths.relativePath, config.build); + const projectRoot = paths.directory ?? directory; const { format, exports } = await guessWorkerFormat( paths.absolutePath, - directory, + projectRoot, args.format ?? config.build?.upload?.format, config.tsconfig ); @@ -117,7 +120,7 @@ export async function getEntry( return { file: paths.absolutePath, - directory, + directory: projectRoot, format, moduleRoot: args.moduleRoot ?? config.base_dir ?? path.dirname(paths.absolutePath), diff --git a/packages/wrangler/src/deployment-bundle/resolve-entry.ts b/packages/wrangler/src/deployment-bundle/resolve-entry.ts index 5a9efb535194..3d13b7c76be8 100644 --- a/packages/wrangler/src/deployment-bundle/resolve-entry.ts +++ b/packages/wrangler/src/deployment-bundle/resolve-entry.ts @@ -16,11 +16,12 @@ export function resolveEntryWithMain( ): { absolutePath: string; relativePath: string; + directory: string; } { const directory = path.resolve(path.dirname(configPath ?? ".")); const file = path.resolve(directory, main); const relativePath = path.relative(directory, file) || "."; - return { absolutePath: file, relativePath }; + return { absolutePath: file, relativePath, directory }; } export function resolveEntryWithEntryPoint( @@ -29,13 +30,14 @@ export function resolveEntryWithEntryPoint( ): { absolutePath: string; relativePath: string; + directory: 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 }; + return { absolutePath: file, relativePath, directory }; } export function resolveEntryWithAssets(): {