Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix entrypoint logic and refactor #6971

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
62 changes: 30 additions & 32 deletions packages/wrangler/src/deployment-bundle/entry.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -42,41 +47,33 @@ export async function getEntry(
config: Config,
command: "dev" | "deploy" | "versions upload" | "types"
): Promise<Entry> {
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
Expand Down Expand Up @@ -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",
};
}
Expand Down
48 changes: 48 additions & 0 deletions packages/wrangler/src/deployment-bundle/resolve-entry.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
Loading