Skip to content

Commit

Permalink
PR fixes 1
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmenPopoviciu committed Jun 28, 2024
1 parent 77f7279 commit 5abcd1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 48 deletions.
63 changes: 23 additions & 40 deletions packages/wrangler/e2e/pages-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,9 @@ describe("pages dev", () => {
});

const port = await getPort();
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const worker = helper.runLongLived(
`wrangler pages dev --port ${port} public`
);
const { url } = await worker.waitForReady();

await expect(fetchText(url)).resolves.toMatchInlineSnapshot(
Expand All @@ -601,9 +603,8 @@ describe("pages dev", () => {
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const { url } = await worker.waitForReady();

await expect(
fetch(url).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Hello World!"');
let text = await fetchText(url);
expect(text).toMatchInlineSnapshot('"Hello World!"');

await helper.seed({
"functions/_middleware.js": dedent`
Expand All @@ -614,9 +615,8 @@ describe("pages dev", () => {

await worker.waitForReload();

await expect(
fetch(url).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Updated Worker!"');
text = await fetchText(url);
expect(text).toMatchInlineSnapshot('"Updated Worker!"');
});

it("should support modifying dependencies during dev session (Functions)", async () => {
Expand All @@ -627,17 +627,11 @@ describe("pages dev", () => {
export const hello = "Hello World!"
export const hi = "Hi there!"
`,
});

await helper.seed({
"functions/greetings/_middleware.js": dedent`
import { hello } from "../../utils/greetings"
export async function onRequest() {
return new Response(hello)
}`,
});

await helper.seed({
"functions/hi.js": dedent`
import { hi } from "../utils/greetings"
export async function onRequest() {
Expand All @@ -649,13 +643,11 @@ describe("pages dev", () => {
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const { url } = await worker.waitForReady();

await expect(
fetch(`${url}/greetings/hello`).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Hello World!"');
let hello = await fetchText(`${url}/greetings/hello`);
expect(hello).toMatchInlineSnapshot('"Hello World!"');

await expect(
fetch(`${url}/hi`).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Hi there!"');
let hi = await fetchText(`${url}/hi`);
expect(hi).toMatchInlineSnapshot('"Hi there!"');

await helper.seed({
"utils/greetings.js": dedent`
Expand All @@ -666,13 +658,11 @@ describe("pages dev", () => {

await worker.waitForReload();

await expect(
fetch(`${url}/greetings/hello`).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Hey World!"');
hello = await fetchText(`${url}/greetings/hello`);
expect(hello).toMatchInlineSnapshot('"Hello World!"');

await expect(
fetch(`${url}/hi`).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Hey there!"');
hi = await fetchText(`${url}/hi`);
expect(hi).toMatchInlineSnapshot('"Hey there!"');
});

it("should support modifying external modules during dev session (Functions)", async () => {
Expand All @@ -682,9 +672,6 @@ describe("pages dev", () => {
"modules/my-html.html": dedent`
<h1>Hello HTML World!</h1>
`,
});

await helper.seed({
"functions/hello.js": dedent`
import html from "../modules/my-html.html";
export async function onRequest() {
Expand All @@ -696,9 +683,8 @@ describe("pages dev", () => {
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const { url } = await worker.waitForReady();

await expect(
fetch(`${url}/hello`).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"<h1>Hello HTML World!</h1>"');
let hello = await fetchText(`${url}/hello`);
expect(hello).toMatchInlineSnapshot('"<h1>Hello HTML World!</h1>"');

await helper.seed({
"modules/my-html.html": dedent`
Expand All @@ -708,9 +694,8 @@ describe("pages dev", () => {

await worker.waitForReload();

await expect(
fetch(`${url}/hello`).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"<h1>Updated HTML!</h1>"');
hello = await fetchText(`${url}/hello`);
expect(hello).toMatchInlineSnapshot('"<h1>Updated HTML!</h1>"');
});

it("should modify worker during dev session (_worker)", async () => {
Expand All @@ -729,9 +714,8 @@ describe("pages dev", () => {
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const { url } = await worker.waitForReady();

await expect(
fetch(url).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Hello World!"');
let hello = await fetchText(url);
expect(hello).toMatchInlineSnapshot('"Hello World!"');

await helper.seed({
"_worker.js": dedent`
Expand All @@ -744,9 +728,8 @@ describe("pages dev", () => {

await worker.waitForReload();

await expect(
fetch(url).then((r) => r.text())
).resolves.toMatchInlineSnapshot('"Updated Worker!"');
hello = await fetchText(url);
expect(hello).toMatchInlineSnapshot('"Updated Worker!"');
});

it("should support modifying _routes.json during dev session", async () => {
Expand Down
16 changes: 8 additions & 8 deletions packages/wrangler/src/pages/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ export const Handler = async (args: PagesDevArguments) => {

try {
const buildFn = async () => {
const crrBundleDependencies = new Set();
const currentBundleDependencies = new Set();

const bundle = await buildFunctions({
outfile: scriptPath,
Expand Down Expand Up @@ -539,7 +539,7 @@ export const Handler = async (args: PagesDevArguments) => {
!resolvedDep.match(/\.wrangler\/./) &&
resolvedDep.match(resolve(process.cwd()))
) {
crrBundleDependencies.add(resolvedDep);
currentBundleDependencies.add(resolvedDep);

if (!watchedBundleDependencies.has(resolvedDep)) {
watchedBundleDependencies.add(resolvedDep);
Expand All @@ -552,7 +552,7 @@ export const Handler = async (args: PagesDevArguments) => {
for (const module of bundle.modules) {
if (module.filePath) {
const resolvedDep = resolve(functionsDirectory, module.filePath);
crrBundleDependencies.add(resolvedDep);
currentBundleDependencies.add(resolvedDep);

if (!watchedBundleDependencies.has(resolvedDep)) {
watchedBundleDependencies.add(resolvedDep);
Expand All @@ -568,7 +568,7 @@ export const Handler = async (args: PagesDevArguments) => {
* them, as they are no longer relevant to the compiled Functions.
*/
watchedBundleDependencies.forEach(async (path) => {
if (!crrBundleDependencies.has(path)) {
if (!currentBundleDependencies.has(path)) {
watchedBundleDependencies.delete(path);
watcher.unwatch(path);
}
Expand Down Expand Up @@ -599,11 +599,11 @@ export const Handler = async (args: PagesDevArguments) => {
await buildFn();
} catch (e) {
if (e instanceof FunctionsNoRoutesError) {
logger.warn(
logger.error(
getFunctionsNoRoutesWarning(functionsDirectory, "skipping")
);
} else if (e instanceof FunctionsBuildError) {
logger.warn(
logger.error(
getFunctionsBuildWarning(functionsDirectory, e.message)
);
} else {
Expand All @@ -614,7 +614,7 @@ export const Handler = async (args: PagesDevArguments) => {
* the last successfully built Functions, and allow developers to
* write their code to completion
*/
logger.warn(
logger.error(
`Error while attempting to build the Functions directory ${functionsDirectory}. Skipping to last successful built version of Functions.\n` +
`${e}`
);
Expand All @@ -633,7 +633,7 @@ export const Handler = async (args: PagesDevArguments) => {
} catch (e) {
// If there are no Functions, then Pages will only serve assets.
if (e instanceof FunctionsNoRoutesError) {
logger.warn(
logger.error(
getFunctionsNoRoutesWarning(functionsDirectory, "skipping")
);
// Resolve anyway and run without Functions
Expand Down

0 comments on commit 5abcd1f

Please sign in to comment.