Skip to content

Commit

Permalink
fix: ensure that pg-cloudflare can be used with bundlers that don't k…
Browse files Browse the repository at this point in the history
…now about Cloudflare sockets (#2978)

By implementing package.json `exports` we can avoid processing the Cloudflare
specific code, which contains `import ... from "cloudflare:sockets"`, in bundlers such
as Webpack.

If you are bundling for a Worker environment using Webpack then you need to add the
`workerd` condition and ignore `cloudflare:sockets` imports:

**webpack.config.js**
```js
resolve: { conditionNames: ["require", "node", "workerd"] },
  plugins: [
    new webpack.IgnorePlugin({
      resourceRegExp: /^cloudflare:sockets$/,
    }),
  ],
```
  • Loading branch information
petebacondarwin authored May 31, 2023
1 parent 20d2c08 commit 0dfd955
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/pg-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
"name": "pg-cloudflare",
"version": "1.1.0",
"description": "A socket implementation that can run on Cloudflare Workers using native TCP connections.",
"main": "dist/index.js",
"main": "dist/empty.js",
"types": "dist/index.d.ts",
"license": "MIT",
"devDependencies": {
"ts-node": "^8.5.4",
"typescript": "^4.0.3"
},
"exports": {
"workerd": "./dist/index.js",
"default": "./dist/empty.js"
},
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
Expand Down
3 changes: 3 additions & 0 deletions packages/pg-cloudflare/src/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is an empty module that is served up when outside of a workerd environment
// See the `exports` field in package.json
export default {}

0 comments on commit 0dfd955

Please sign in to comment.