Skip to content

Commit b5fa4a6

Browse files
authored
chore: fix entrypoint logic and refactor (#6971)
* chore: fix entrypoint logic and refactor * chore: remove spurious files * Add test case
1 parent e4a8591 commit b5fa4a6

File tree

3 files changed

+113
-32
lines changed

3 files changed

+113
-32
lines changed

packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,41 @@ describe("ConfigController", () => {
5656
});
5757
});
5858

59+
it("should apply module root to parent if main is nested from base_dir", async () => {
60+
const controller = new ConfigController();
61+
const event = waitForConfigUpdate(controller);
62+
await seed({
63+
"some/base_dir/nested/index.js": dedent/* javascript */ `
64+
export default {
65+
fetch(request, env, ctx) {
66+
return new Response("hello world")
67+
}
68+
}
69+
`,
70+
"wrangler.toml": dedent`
71+
main = \"./some/base_dir/nested/index.js\"
72+
base_dir = \"./some/base_dir\"`,
73+
});
74+
75+
const config: StartDevWorkerInput = {};
76+
77+
await controller.set(config);
78+
79+
await expect(event).resolves.toMatchObject({
80+
type: "configUpdate",
81+
config: {
82+
build: {
83+
additionalModules: [],
84+
define: {},
85+
format: "modules",
86+
moduleRoot: path.join(process.cwd(), "./some/base_dir"),
87+
moduleRules: [],
88+
},
89+
directory: process.cwd(),
90+
entrypoint: path.join(process.cwd(), "./some/base_dir/nested/index.js"),
91+
},
92+
});
93+
});
5994
it("should shallow merge patched config", async () => {
6095
const controller = new ConfigController();
6196
const event1 = waitForConfigUpdate(controller);

packages/wrangler/src/deployment-bundle/entry.ts

+30-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import path from "node:path";
22
import { UserError } from "../errors";
33
import { logger } from "../logger";
4-
import { getBasePath } from "../paths";
54
import guessWorkerFormat from "./guess-worker-format";
5+
import {
6+
resolveEntryWithAssets,
7+
resolveEntryWithEntryPoint,
8+
resolveEntryWithMain,
9+
resolveEntryWithScript,
10+
} from "./resolve-entry";
611
import { runCustomBuild } from "./run-custom-build";
712
import type { Config } from "../config";
813
import type { DurableObjectBindings } from "../config/environment";
@@ -42,41 +47,33 @@ export async function getEntry(
4247
config: Config,
4348
command: "dev" | "deploy" | "versions upload" | "types"
4449
): 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;
4754

4855
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();
7068
} 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+
);
7372
}
74-
75-
const relativeFile = path.relative(directory, file) || ".";
76-
await runCustomBuild(file, relativeFile, config.build);
73+
await runCustomBuild(paths.absolutePath, paths.relativePath, config.build);
7774

7875
const format = await guessWorkerFormat(
79-
file,
76+
paths.absolutePath,
8077
directory,
8178
args.format ?? config.build?.upload?.format,
8279
config.tsconfig
@@ -110,10 +107,11 @@ export async function getEntry(
110107
}
111108

112109
return {
113-
file,
110+
file: paths.absolutePath,
114111
directory,
115112
format,
116-
moduleRoot: args.moduleRoot ?? config.base_dir ?? path.dirname(file),
113+
moduleRoot:
114+
args.moduleRoot ?? config.base_dir ?? path.dirname(paths.absolutePath),
117115
name: config.name ?? "worker",
118116
};
119117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import path from "path";
2+
import { getBasePath } from "../paths";
3+
4+
export function resolveEntryWithScript(script: string): {
5+
absolutePath: string;
6+
relativePath: string;
7+
} {
8+
const file = path.resolve(script);
9+
const relativePath = path.relative(process.cwd(), file) || ".";
10+
return { absolutePath: file, relativePath };
11+
}
12+
13+
export function resolveEntryWithMain(
14+
main: string,
15+
configPath?: string
16+
): {
17+
absolutePath: string;
18+
relativePath: string;
19+
} {
20+
const directory = path.resolve(path.dirname(configPath ?? "."));
21+
const file = path.resolve(directory, main);
22+
const relativePath = path.relative(directory, file) || ".";
23+
return { absolutePath: file, relativePath };
24+
}
25+
26+
export function resolveEntryWithEntryPoint(
27+
entryPoint: string,
28+
configPath?: string
29+
): {
30+
absolutePath: string;
31+
relativePath: string;
32+
} {
33+
const directory = path.resolve(path.dirname(configPath ?? "."));
34+
const file = path.extname(entryPoint)
35+
? path.resolve(entryPoint)
36+
: path.resolve(entryPoint, "index.js");
37+
const relativePath = path.relative(directory, file) || ".";
38+
return { absolutePath: file, relativePath };
39+
}
40+
41+
export function resolveEntryWithAssets(): {
42+
absolutePath: string;
43+
relativePath: string;
44+
} {
45+
const file = path.resolve(getBasePath(), "templates/no-op-worker.js");
46+
const relativePath = path.relative(process.cwd(), file) || ".";
47+
return { absolutePath: file, relativePath };
48+
}

0 commit comments

Comments
 (0)