-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added
--node-compat
to Pages Dev & Functions (#1000)
`pages dev <dir>` & `wrangler pages functions build` will have a `--node-compat` flag powered by @esbuild-plugins/node-globals-polyfill (which in itself is powered by rollup-plugin-node-polyfills). The only difference in `pages` will be it does not check the `wrangler.toml` so the `node_compat = true` will not enable it for `wrangler pages` functionality. resolves #890
- Loading branch information
1 parent
a0e0f5f
commit 5a8e8d5
Showing
13 changed files
with
302 additions
and
10 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,7 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
`pages dev <dir>` & `wrangler pages functions build` will have a `--node-compat` flag powered by @esbuild-plugins/node-globals-polyfill (which in itself is powered by rollup-plugin-node-polyfills). The only difference in `pages` will be it does not check the `wrangler.toml` so the `node_compat = true`will not enable it for `wrangler pages` functionality. | ||
|
||
resolves #890 |
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 @@ | ||
cdn-cgi/ |
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 @@ | ||
import * as path from "path"; | ||
import Stripe from "stripe"; | ||
|
||
export const onRequest = () => { | ||
// make sure path actually works | ||
return new Response( | ||
JSON.stringify({ | ||
PATH: path.join("path/to", "some-file"), | ||
STRIPE_OBJECT: Stripe.toString(), | ||
}) | ||
); | ||
}; |
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,36 @@ | ||
{ | ||
"name": "node-app-pages", | ||
"version": "0.0.0", | ||
"private": true, | ||
"main": "dist/worker.js", | ||
"engines": { | ||
"node": ">=14" | ||
}, | ||
"scripts": { | ||
"dev": "npx wrangler pages dev public --port 12345 --node-compat", | ||
"test": "npx jest --forceExit" | ||
}, | ||
"dependencies": { | ||
"stripe": "^9.1.0" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^3.10.0", | ||
"@types/node": "^17.0.33", | ||
"undici": "^4.15.1" | ||
}, | ||
"sideEffects": false, | ||
"jest": { | ||
"testRegex": ".*.(test|spec)\\.[jt]sx?$", | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!find-up|locate-path|p-locate|p-limit|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream)" | ||
], | ||
"transform": { | ||
"^.+\\.c?(t|j)sx?$": [ | ||
"esbuild-jest", | ||
{ | ||
"sourcemap": 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,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>Hello, world!</h1> | ||
</body> | ||
</html> |
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,56 @@ | ||
import { spawn } from "child_process"; | ||
import path from "path"; | ||
import { fetch } from "undici"; | ||
import type { ChildProcess } from "child_process"; | ||
import type { Response } from "undici"; | ||
|
||
const isWindows = process.platform === "win32"; | ||
|
||
const waitUntilReady = async (url: string): Promise<Response> => { | ||
let response: Response | undefined = undefined; | ||
|
||
while (response === undefined) { | ||
await new Promise((resolvePromise) => setTimeout(resolvePromise, 500)); | ||
|
||
try { | ||
response = await fetch(url); | ||
} catch {} | ||
} | ||
|
||
return response as Response; | ||
}; | ||
|
||
describe("Pages Dev", () => { | ||
let wranglerProcess: ChildProcess; | ||
|
||
beforeEach(() => { | ||
wranglerProcess = spawn("npm", ["run", "dev"], { | ||
shell: isWindows, | ||
cwd: path.resolve(__dirname, "../"), | ||
env: { BROWSER: "none", ...process.env }, | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
await new Promise((resolve, reject) => { | ||
wranglerProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
isWindows | ||
? wranglerProcess.kill("SIGTERM") | ||
: wranglerProcess.kill("SIGKILL"); | ||
}); | ||
}); | ||
|
||
it("should work with `--node-compat` when running code requiring polyfills", async () => { | ||
const response = await waitUntilReady("http://localhost:12345/stripe"); | ||
|
||
await expect(response.text()).resolves.toContain( | ||
`"PATH":"path/to/some-file","STRIPE_OBJECT"` | ||
); | ||
}); | ||
}); |
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 @@ | ||
{ | ||
"include": ["functions"], | ||
"compilerOptions": { | ||
"target": "ES2020", | ||
"module": "CommonJS", | ||
"lib": ["ES2020"], | ||
"types": ["@cloudflare/workers-types"], | ||
"moduleResolution": "node" | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.