-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support bundling the raw Pages
_worker.js
before deploying (#…
…2404) * refactor: tidy up fixture tests of long-lived wrangler processes We now use helper functions to setup and teardown a long-lived wrangler process. This means that we can remove the `--forceExit` option from the jest setup. * 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. * test: increase timeout on local-mode test
- Loading branch information
1 parent
fe8c691
commit 3f82434
Showing
20 changed files
with
433 additions
and
378 deletions.
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,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. |
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
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
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 |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
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
42 changes: 9 additions & 33 deletions
42
fixtures/pages-functions-with-routes-app/tests/index.test.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
43 changes: 9 additions & 34 deletions
43
fixtures/pages-workerjs-and-functions-app/tests/index.test.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
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 |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
Oops, something went wrong.