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

feat: support bundling the raw Pages _worker.js before deploying #2404

Merged
merged 5 commits into from
Jan 12, 2023
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
20 changes: 20 additions & 0 deletions .changeset/tiny-knives-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"wrangler": minor
---

feat: support bundling the raw Pages `_worker.js` before deploying

Previously, if you provided a `_worker.js` file, then Pages would simply check the
file for disallowed imports and then deploy the file as-is.

Not bundling the `_worker.js` file means that it cannot containing imports to other
JS files, but also prevents Wrangler from adding shims such as the one for the D1 alpha
release.

This change adds the ability to tell Wrangler to pass the `_worker.js` through the
normal Wrangler bundling process before deploying by setting the `--bundle`
command line argument to `wrangler pages dev` and `wrangler pages publish`.

This is in keeping with the same flag for `wrangler publish`.

Currently bundling is opt-in, flag defaults to `false` if not provided.
8 changes: 4 additions & 4 deletions fixtures/external-durable-objects-app/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe.concurrent.skip("Pages Functions", () => {
path.join("..", "..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["dev", "index.ts", "--local", "--port=0"],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
stdio: ["ignore", "ignore", "ignore", "ipc"],
cwd: path.resolve(__dirname, "..", "a"),
}
).on("message", (message) => {
Expand All @@ -52,7 +52,7 @@ describe.concurrent.skip("Pages Functions", () => {
path.join("..", "..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["dev", "index.ts", "--local", "--port=0"],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
stdio: ["ignore", "ignore", "ignore", "ipc"],
cwd: path.resolve(__dirname, "..", "b"),
}
).on("message", (message) => {
Expand All @@ -65,7 +65,7 @@ describe.concurrent.skip("Pages Functions", () => {
path.join("..", "..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["dev", "index.ts", "--local", "--port=0"],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
stdio: ["ignore", "ignore", "ignore", "ipc"],
cwd: path.resolve(__dirname, "..", "c"),
}
).on("message", (message) => {
Expand All @@ -84,7 +84,7 @@ describe.concurrent.skip("Pages Functions", () => {
"--port=0",
],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
stdio: ["ignore", "ignore", "ignore", "ipc"],
cwd: path.resolve(__dirname, "..", "d"),
}
).on("message", (message) => {
Expand Down
22 changes: 13 additions & 9 deletions fixtures/local-mode-tests/tests/unstableDev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ describe("worker in local mode", () => {
await worker.stop();
});

it.concurrent("should invoke the worker and exit", async () => {
await readyPromise;
const resp = await worker.fetch();
expect(resp).not.toBe(undefined);
if (resp) {
const text = await resp.text();
it.concurrent(
"should invoke the worker and exit",
async () => {
await readyPromise;
const resp = await worker.fetch();
expect(resp).not.toBe(undefined);
if (resp) {
const text = await resp.text();

expect(text).toMatchInlineSnapshot(`"Hello World!"`);
}
});
expect(text).toMatchInlineSnapshot(`"Hello World!"`);
}
},
10000
);
});
49 changes: 10 additions & 39 deletions fixtures/node-app-pages/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,23 @@
import { fork } from "child_process";
import path from "path";
import { resolve } from "node:path";
import { fetch } from "undici";
import { describe, it, beforeAll, afterAll } from "vitest";
import type { ChildProcess } from "child_process";
import { describe, it } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe.concurrent("Pages Dev", () => {
let wranglerProcess: ChildProcess;
let ip: string;
let port: number;

beforeAll(async () => {
await new Promise((resolve) => {
wranglerProcess = fork(
path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["pages", "dev", "public", "--node-compat", "--port=0"],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
cwd: path.resolve(__dirname, ".."),
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
ip = parsedMessage.ip;
port = parsedMessage.port;
resolve(null);
});
});
});

afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill("SIGTERM");
});
});

it("should work with `--node-compat` when running code requiring polyfills", async ({
expect,
}) => {
const { ip, port, stop } = await runWranglerPagesDev(
resolve(__dirname, ".."),
"public",
["--node-compat", "--port=0"]
);
const response = await fetch(`http://${ip}:${port}/stripe`);

await expect(response.text()).resolves.toContain(
`"PATH":"path/to/some-file","STRIPE_OBJECT"`
);

await stop();
});
});
55 changes: 14 additions & 41 deletions fixtures/pages-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,25 @@
import { fork } from "child_process";
import * as path from "path";
import { resolve } from "node:path";
import { fetch } from "undici";
import { describe, it, beforeAll, afterAll } from "vitest";
import type { ChildProcess } from "child_process";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe.concurrent("Pages Functions", () => {
let wranglerProcess: ChildProcess;
let ip: string;
let port: number;
let ip, port, stop;

beforeAll(async () => {
await new Promise((resolve) => {
wranglerProcess = fork(
path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"),
[
"pages",
"dev",
"public",
"--binding=NAME=VALUE",
"--binding=OTHER_NAME=THING=WITH=EQUALS",
"--r2=BUCKET",
"--port=0",
],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
cwd: path.resolve(__dirname, ".."),
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
ip = parsedMessage.ip;
port = parsedMessage.port;
resolve(null);
});
});
({ ip, port, stop } = await runWranglerPagesDev(
resolve(__dirname, ".."),
"public",
[
"--binding=NAME=VALUE",
"--binding=OTHER_NAME=THING=WITH=EQUALS",
"--r2=BUCKET",
"--port=0",
]
));
});

afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill("SIGTERM");
});
});
afterAll(async () => await stop());

it("renders static pages", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
Expand Down
42 changes: 9 additions & 33 deletions fixtures/pages-functions-with-routes-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
import { fork } from "child_process";
import * as path from "path";
import { resolve } from "node:path";
import { fetch } from "undici";
import { describe, it, beforeAll, afterAll } from "vitest";
import type { ChildProcess } from "child_process";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe.concurrent("Pages Functions with custom _routes.json", () => {
let wranglerProcess: ChildProcess;
let ip: string;
let port: number;
let ip, port, stop;

beforeAll(async () => {
await new Promise((resolve) => {
wranglerProcess = fork(
path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["pages", "dev", "public", "--port=0"],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
cwd: path.resolve(__dirname, ".."),
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
ip = parsedMessage.ip;
port = parsedMessage.port;
resolve(null);
});
});
({ ip, port, stop } = await runWranglerPagesDev(
resolve(__dirname, ".."),
"public",
["--port=0"]
));
});

afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill("SIGTERM");
});
});
afterAll(async () => await stop());

it("should render static pages", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/undefined-route`);
Expand Down
43 changes: 9 additions & 34 deletions fixtures/pages-workerjs-and-functions-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,22 @@
import { fork } from "child_process";
import * as path from "path";
import { resolve } from "node:path";
import { fetch } from "undici";
import { describe, it, beforeAll, afterAll } from "vitest";
import type { ChildProcess } from "child_process";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe.concurrent(
"Pages project with `_worker.js` and `/functions` directory",
() => {
let wranglerProcess: ChildProcess;
let ip: string;
let port: number;
let ip, port, stop;

// const std = mockConsoleMethods();
beforeAll(async () => {
await new Promise((resolve) => {
wranglerProcess = fork(
path.join("..", "..", "packages", "wrangler", "bin", "wrangler.js"),
["pages", "dev", "public", "--port=0"],
{
stdio: ["inherit", "inherit", "inherit", "ipc"],
cwd: path.resolve(__dirname, ".."),
}
).on("message", (message) => {
const parsedMessage = JSON.parse(message.toString());
ip = parsedMessage.ip;
port = parsedMessage.port;
resolve(null);
});
});
({ ip, port, stop } = await runWranglerPagesDev(
resolve(__dirname, ".."),
"public",
["--port=0"]
));
});

afterAll(async () => {
await new Promise((resolve, reject) => {
wranglerProcess.once("exit", (code) => {
if (!code) {
resolve(code);
} else {
reject(code);
}
});
wranglerProcess.kill("SIGTERM");
});
});
afterAll(async () => await stop());

it("renders static pages", async ({ expect }) => {
const response = await fetch(`http://${ip}:${port}/`);
Expand Down
25 changes: 22 additions & 3 deletions fixtures/pages-workerjs-app/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import { execSync } from "child_process";
import path from "path";
import { execSync } from "node:child_process";
import path, { resolve } from "node:path";
import { fetch } from "undici";
import { describe, it } from "vitest";
import { runWranglerPagesDev } from "../../shared/src/run-wrangler-long-lived";

describe.concurrent("Pages _worker.js", () => {
it("should throw an error when the _worker.js file imports something", ({
expect,
}) => {
expect(() =>
execSync("npm run dev", { cwd: path.resolve(__dirname, "..") })
execSync("npm run dev", {
cwd: path.resolve(__dirname, ".."),
stdio: "ignore",
})
).toThrowError();
});

it("should not throw an error when the _worker.js file imports something if --bundle is true", async ({
expect,
}) => {
const { ip, port, stop } = await runWranglerPagesDev(
resolve(__dirname, ".."),
"./workerjs-test",
["--bundle"]
);
await expect(
fetch(`http://${ip}:${port}/`).then((resp) => resp.text())
).resolves.toContain("test");
await stop();
});
});
Loading