Skip to content

Commit

Permalink
Fix relative project root
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Nov 18, 2024
1 parent d4d3062 commit 865d102
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
128 changes: 128 additions & 0 deletions packages/wrangler/src/__tests__/get-entry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { writeFile } from "fs/promises";

Check failure on line 1 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'writeFile' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'writeFile' is defined but never used
import path from "path";

Check failure on line 2 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'path' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 2 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'path' is defined but never used
import dedent from "ts-dedent";
import { Config } from "../config";

Check failure on line 4 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'Config' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 4 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'Config' is defined but never used
import { defaultWranglerConfig } from "../config/config";
import { Entry, getEntry } from "../deployment-bundle/entry";

Check failure on line 6 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

Import "Entry" is only used as types
import guessWorkerFormat from "../deployment-bundle/guess-worker-format";

Check failure on line 7 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'guessWorkerFormat' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 7 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'guessWorkerFormat' is defined but never used
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);
});
});
9 changes: 6 additions & 3 deletions packages/wrangler/src/deployment-bundle/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions packages/wrangler/src/deployment-bundle/resolve-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(): {
Expand Down

0 comments on commit 865d102

Please sign in to comment.