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

Make sure that the correct entrypoint file path is applied when bundling Pages applications #4676

Merged
merged 3 commits into from
Jan 11, 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
5 changes: 5 additions & 0 deletions .changeset/eighty-hotels-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

make sure the script path is correctly resolved in `pages dev` when no directory is specified
5 changes: 5 additions & 0 deletions fixtures/pages-dev-proxy-with-script/_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
fetch() {
return new Response("hello from _worker.js");
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
fetch() {
return new Response("hello from custom/script/path/index.js");
},
};
21 changes: 21 additions & 0 deletions fixtures/pages-dev-proxy-with-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "pages-dev-proxy-with-script",
"version": "0.0.0",
"private": true,
"sideEffects": false,
"scripts": {
"dev": "npx wrangler pages dev public",
"test": "vitest run",
"test:watch": "vitest",
"type:tests": "tsc -p ./tests/tsconfig.json"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20221111.1",
"undici": "^5.9.1",
"wrangler": "workspace:*"
},
"engines": {
"node": ">=16.13"
}
}
77 changes: 77 additions & 0 deletions fixtures/pages-dev-proxy-with-script/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import path from "node:path";
import { Response, fetch } from "undici";
import { describe, it, expect } from "vitest";
import { ChildProcess, fork } from "node:child_process";
import { setTimeout } from "node:timers/promises";

describe("Pages dev with proxy and a script file", () => {
it("should handle requests using a script from the default _worker.js path", async () => {
const { port, childProcess } = await startWranglerPagesDevProxy();
const combinedResponse = await waitUntilReady(`http://127.0.0.1:${port}/`);
const respText = await combinedResponse.text();
expect(respText).toMatchInlineSnapshot('"hello from _worker.js"');
await terminateChildProcess(childProcess);
});

it("should handle requests using a script from a custom script path", async () => {
const { port, childProcess } = await startWranglerPagesDevProxy([
"--script-path=custom/script/path/index.js",
]);
const combinedResponse = await waitUntilReady(`http://127.0.0.1:${port}/`);
const respText = await combinedResponse.text();
expect(respText).toMatchInlineSnapshot(
'"hello from custom/script/path/index.js"'
);
await terminateChildProcess(childProcess);
});
});

async function startWranglerPagesDevProxy(extraArgs: string[] = []): Promise<{
childProcess: ChildProcess;
port: string;
}> {
return new Promise(async (resolve) => {
const childProcess = fork(
path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["pages", "dev", "--port=0", "--proxy=9999", ...extraArgs],
{
cwd: path.resolve(__dirname, ".."),
env: { BROWSER: "none", ...process.env },
stdio: ["ignore", "ignore", "ignore", "ipc"],
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
resolve({
childProcess,
port: parsedMessage.port,
});
});
});
}

function terminateChildProcess(childProcess: ChildProcess): Promise<unknown> {
return new Promise((resolve, reject) => {
childProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
childProcess.kill("SIGTERM");
});
}

async function waitUntilReady(url: string): Promise<Response> {
let response: Response | undefined = undefined;

while (response === undefined) {
await setTimeout(500);

try {
response = await fetch(url);
} catch (e) {}
}

return response as Response;
}
7 changes: 7 additions & 0 deletions fixtures/pages-dev-proxy-with-script/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["**/*.ts", "../../../node-types.d.ts"]
}
12 changes: 12 additions & 0 deletions fixtures/pages-dev-proxy-with-script/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"include": ["index.d.ts"],
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"lib": ["ES2020"],
"types": ["@cloudflare/workers-types"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true
}
}
10 changes: 10 additions & 0 deletions fixtures/pages-dev-proxy-with-script/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
testTimeout: 10_000,
hookTimeout: 10_000,
teardownTimeout: 10_000,
useAtomics: true,
},
});
2 changes: 1 addition & 1 deletion packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export const Handler = async ({
const workerScriptPath =
directory !== undefined
? join(directory, singleWorkerScriptPath)
: singleWorkerScriptPath;
: resolve(singleWorkerScriptPath);
const usingWorkerDirectory =
existsSync(workerScriptPath) && lstatSync(workerScriptPath).isDirectory();
const usingWorkerScript = existsSync(workerScriptPath);
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading