Skip to content

Commit

Permalink
feat: experimental --node-compat / config.node_compat
Browse files Browse the repository at this point in the history
This adds an experimental node.js compatibility mode. It can be enabled by adding `node_compat = true` in `wrangler.toml`, or by passing `--node-compat` as a command line arg for `dev`/`publish` commands. This is currently powered by `@esbuild-plugins/node-globals-polyfill` (which in itself is powered by `rollup-plugin-node-polyfills`).

We'd previously added this, and then removed it because the quality of the polyfills isn't great. We're reintroducing it regardless so we can start getting feedback on its usage, and it sets up a foundation for replacing it with our own, hopefully better maintained polyfills.

Of particular note, this means that what we promised in https://blog.cloudflare.com/announcing-stripe-support-in-workers/ now actually works.

This patch also addresses some dependency issues, specifically leftover entries in `package-lock.json`.
  • Loading branch information
threepointone committed Apr 30, 2022
1 parent 0a79d75 commit 70d29ba
Show file tree
Hide file tree
Showing 17 changed files with 1,699 additions and 1,142 deletions.
13 changes: 13 additions & 0 deletions .changeset/fifty-drinks-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"wrangler": patch
---

feat: experimental `--node-compat` / `config.node_compat`

This adds an experimental node.js compatibility mode. It can be enabled by adding `node_compat = true` in `wrangler.toml`, or by passing `--node-compat` as a command line arg for `dev`/`publish` commands. This is currently powered by `@esbuild-plugins/node-globals-polyfill` (which in itself is powered by `rollup-plugin-node-polyfills`).

We'd previously added this, and then removed it because the quality of the polyfills isn't great. We're reintroducing it regardless so we can start getting feedback on its usage, and it sets up a foundation for replacing it with our own, hopefully better maintained polyfills.

Of particular note, this means that what we promised in https://blog.cloudflare.com/announcing-stripe-support-in-workers/ now actually works.

This patch also addresses some dependency issues, specifically leftover entries in package-lock.json.
12 changes: 12 additions & 0 deletions examples/node-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "node-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"stripe": "8.219.0"
},
"keywords": [],
"author": "",
"license": "ISC"
}
22 changes: 22 additions & 0 deletions examples/node-app/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `wrangler dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `wrangler publish --name node-app` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/

import * as path from "path";
import Stripe from "stripe";

// make sure Stripe imports fine
console.log(Stripe);

export default {
async fetch() {
// make sure path actually works
return new Response(path.join("path/to", "some-file"));
},
};
5 changes: 5 additions & 0 deletions examples/node-app/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "node-app"
main = "src/index.js"
compatibility_date = "2022-04-30"

node_compat = true
2 changes: 1 addition & 1 deletion examples/remix-pages-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/react": "^17.0.24",
"@types/react-dom": "^17.0.9",
"cross-env": "^7.0.3",
"esbuild": "0.13.14",
"esbuild": "0.14.34",
"npm-run-all": "^4.1.5",
"typescript": "^4.1.2",
"undici": "^4.15.1"
Expand Down
Loading

0 comments on commit 70d29ba

Please sign in to comment.