Skip to content

Commit ff3de4a

Browse files
committed
fix(pg-cloudflare): use conditional export to support bundlers that don't know about cloudflare:sockets
1 parent 411869d commit ff3de4a

File tree

8 files changed

+571
-15
lines changed

8 files changed

+571
-15
lines changed

packages/pg-cloudflare/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
npm i --save-dev pg-cloudflare
1111
```
1212

13+
The package uses conditional exports to support bundlers that don't know about
14+
`cloudflare:sockets`, so the consumer code by default imports an empty file. To
15+
enable the package, resolve to the `cloudflare` condition in your bundler's
16+
config. For example:
17+
18+
- `webpack.config.js`
19+
```js
20+
resolve: { conditionNames: [..., "cloudflare"] },
21+
plugins: [
22+
// ignore cloudflare:sockets imports
23+
new webpack.IgnorePlugin({
24+
resourceRegExp: /^cloudflare:sockets$/,
25+
}),
26+
],
27+
```
28+
- `vite.config.js`
29+
```js
30+
resolve: {
31+
conditions: [..., "cloudflare"],
32+
},
33+
```
34+
1335
## How to use conditionally, in non-Node.js environments
1436

1537
As implemented in `pg` [here](https://github.com/brianc/node-postgres/commit/07553428e9c0eacf761a5d4541a3300ff7859578#diff-34588ad868ebcb232660aba7ee6a99d1e02f4bc93f73497d2688c3f074e60533R5-R13), a typical use case might look as follows, where in a Node.js environment the `net` module is used, while in a non-Node.js environment, where `net` is unavailable, `pg-cloudflare` is used instead, providing an equivalent interface:
@@ -21,15 +43,14 @@ module.exports.getStream = function getStream(ssl = false) {
2143
return net.Socket()
2244
}
2345
const { CloudflareSocket } = require('pg-cloudflare')
24-
return new CloudflareSocket(ssl);
46+
return new CloudflareSocket(ssl)
2547
}
2648
```
2749

2850
## Node.js implementation of the Socket API proposal
2951

3052
If you're looking for a way to rely on `connect()` as the interface you use to interact with raw sockets, but need this interface to be available in a Node.js environment, [`@arrowood.dev/socket`](https://github.com/Ethan-Arrowood/socket) provides a Node.js implementation of the Socket API.
3153

32-
3354
### license
3455

3556
The MIT License (MIT)

packages/pg-cloudflare/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
},
1212
"exports": {
1313
".": {
14-
"import": "./esm/index.mjs",
15-
"require": "./dist/index.js",
16-
"default": "./dist/index.js"
14+
"cloudflare": {
15+
"import": "./esm/index.mjs",
16+
"require": "./dist/index.js"
17+
},
18+
"default": "./dist/empty.js"
1719
}
1820
},
1921
"scripts": {

packages/pg-esm-test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8-
"test": "node --test"
8+
"test": "node --test --conditions=cloudflare"
99
},
1010
"keywords": [
1111
"postgres",

packages/pg-webpack-test/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "webpack-test",
3+
"version": "0.0.0",
4+
"description": "Test webpack bundle with pg-cloudflare, https://github.com/brianc/node-postgres/issues/3452",
5+
"private": true,
6+
"license": "MIT",
7+
"devDependencies": {
8+
"webpack": "^5.99.9",
9+
"webpack-cli": "^6.0.1",
10+
"pg-cloudflare": "^1.2.5"
11+
},
12+
"scripts": {
13+
"test": "webpack --config webpack-empty.config.js && webpack --config webpack-cloudflare.config.js"
14+
}
15+
}

packages/pg-webpack-test/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const _ = require('pg-cloudflare')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import webpack from 'webpack'
2+
3+
const config = {
4+
mode: 'production',
5+
resolve: { conditionNames: ['import', 'cloudflare'] },
6+
plugins: [
7+
// ignore cloudflare:sockets imports
8+
new webpack.IgnorePlugin({
9+
resourceRegExp: /^cloudflare:sockets$/,
10+
}),
11+
],
12+
}
13+
14+
export default config
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const config = {
2+
mode: 'production',
3+
}
4+
5+
export default config

0 commit comments

Comments
 (0)