|
1 | 1 | import path from "node:path";
|
2 | 2 | import { UserError } from "../errors";
|
3 | 3 | import { logger } from "../logger";
|
4 |
| -import { getBasePath } from "../paths"; |
5 | 4 | import guessWorkerFormat from "./guess-worker-format";
|
| 5 | +import { |
| 6 | + resolveEntryWithAssets, |
| 7 | + resolveEntryWithEntryPoint, |
| 8 | + resolveEntryWithMain, |
| 9 | + resolveEntryWithScript, |
| 10 | +} from "./resolve-entry"; |
6 | 11 | import { runCustomBuild } from "./run-custom-build";
|
7 | 12 | import type { Config } from "../config";
|
8 | 13 | import type { DurableObjectBindings } from "../config/environment";
|
@@ -42,41 +47,33 @@ export async function getEntry(
|
42 | 47 | config: Config,
|
43 | 48 | command: "dev" | "deploy" | "versions upload" | "types"
|
44 | 49 | ): Promise<Entry> {
|
45 |
| - let file: string; |
46 |
| - let directory = process.cwd(); |
| 50 | + const directory = process.cwd(); |
| 51 | + const entryPoint = config.site?.["entry-point"]; |
| 52 | + |
| 53 | + let paths: { absolutePath: string; relativePath: string } | undefined; |
47 | 54 |
|
48 | 55 | if (args.script) {
|
49 |
| - // If the script name comes from the command line it is relative to the current working directory. |
50 |
| - file = path.resolve(args.script); |
51 |
| - } else if (config.main === undefined) { |
52 |
| - if (config.site?.["entry-point"]) { |
53 |
| - directory = path.resolve(path.dirname(config.configPath ?? ".")); |
54 |
| - file = path.extname(config.site?.["entry-point"]) |
55 |
| - ? path.resolve(config.site?.["entry-point"]) |
56 |
| - : // site.entry-point could be a directory |
57 |
| - path.resolve(config.site?.["entry-point"], "index.js"); |
58 |
| - } else if ( |
59 |
| - args.legacyAssets || |
60 |
| - config.legacy_assets || |
61 |
| - args.assets || |
62 |
| - config.assets |
63 |
| - ) { |
64 |
| - file = path.resolve(getBasePath(), "templates/no-op-worker.js"); |
65 |
| - } else { |
66 |
| - throw new UserError( |
67 |
| - `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.` |
68 |
| - ); |
69 |
| - } |
| 56 | + paths = resolveEntryWithScript(args.script); |
| 57 | + } else if (config.main !== undefined) { |
| 58 | + paths = resolveEntryWithMain(config.main, config.configPath); |
| 59 | + } else if (entryPoint) { |
| 60 | + paths = resolveEntryWithEntryPoint(entryPoint, config.configPath); |
| 61 | + } else if ( |
| 62 | + args.legacyAssets || |
| 63 | + config.legacy_assets || |
| 64 | + args.assets || |
| 65 | + config.assets |
| 66 | + ) { |
| 67 | + paths = resolveEntryWithAssets(); |
70 | 68 | } else {
|
71 |
| - directory = path.resolve(path.dirname(config.configPath ?? ".")); |
72 |
| - file = path.resolve(directory, config.main); |
| 69 | + throw new UserError( |
| 70 | + `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.` |
| 71 | + ); |
73 | 72 | }
|
74 |
| - |
75 |
| - const relativeFile = path.relative(directory, file) || "."; |
76 |
| - await runCustomBuild(file, relativeFile, config.build); |
| 73 | + await runCustomBuild(paths.absolutePath, paths.relativePath, config.build); |
77 | 74 |
|
78 | 75 | const format = await guessWorkerFormat(
|
79 |
| - file, |
| 76 | + paths.absolutePath, |
80 | 77 | directory,
|
81 | 78 | args.format ?? config.build?.upload?.format,
|
82 | 79 | config.tsconfig
|
@@ -110,10 +107,11 @@ export async function getEntry(
|
110 | 107 | }
|
111 | 108 |
|
112 | 109 | return {
|
113 |
| - file, |
| 110 | + file: paths.absolutePath, |
114 | 111 | directory,
|
115 | 112 | format,
|
116 |
| - moduleRoot: args.moduleRoot ?? config.base_dir ?? path.dirname(file), |
| 113 | + moduleRoot: |
| 114 | + args.moduleRoot ?? config.base_dir ?? path.dirname(paths.absolutePath), |
117 | 115 | name: config.name ?? "worker",
|
118 | 116 | };
|
119 | 117 | }
|
|
0 commit comments