-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure that the correct entrypoint file path is applied when bundl…
…ing Pages applications (#4676) * resolve singlerWorkerScriptPath * add pages-dev-proxy-with-script fixture
- Loading branch information
1 parent
96a27f3
commit 078cf84
Showing
10 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
fetch() { | ||
return new Response("hello from _worker.js"); | ||
}, | ||
}; |
5 changes: 5 additions & 0 deletions
5
fixtures/pages-dev-proxy-with-script/custom/script/path/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.