Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: build pg-cloudflare as a CommonJS module #3168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mrbbot
Copy link

@mrbbot mrbbot commented Mar 12, 2024

Hey! 馃憢 We're currently working on an integration between Cloudflare Workers and Vitest, allowing you to run your Vitest tests inside the Workers runtime. A community member tried out an early version of this integration and uncovered an issue with pg/pg-cloudflare (cloudflare/workers-sdk#5127 (comment)).

In pg/lib/stream.js, pg-cloudflare is require()ed...

const { CloudflareSocket } = require('pg-cloudflare')

...but pg-cloudflare is compiled to an ES module. ES modules can't be require()d so this shouldn't work. Fortunately, Workers are usually bundled with esbuild which papers over differences between the module formats and allowed this to work. Our Vitest integration uses a "bundle-less" approach that imports modules at runtime from disk more like Node. This meant pg-cloudflare couldn't be require()d by pg, failing any Workers tests using pg.

This PR updates pg-cloudflare's tsconfig.json to compile to CommonJS instead, fixing this issue. 馃憤

/cc @petebacondarwin

@mrbbot mrbbot changed the title fix: build pg-cloudflare to a CommonJS module fix: build pg-cloudflare as a CommonJS module Mar 12, 2024
Copy link
Collaborator

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petebacondarwin
Copy link
Collaborator

The problem with converting this module to CommonJS is this line:

const { connect } = await import('cloudflare:sockets')

If we compile this code to CommonJS this becomes a dynamic require, which the Wrangler bundler then complains about, since the esbuild output results in a dynamic `require("cloudflare:sockets") call.

Could we solve this instead by marking the package as an ESM module (i.e. "type": "module" in its package.json)?
Surely Vite(st) can handle packages that are declared ESM?

@mrbbot
Copy link
Author

mrbbot commented Mar 13, 2024

Surely Vite(st) can handle packages that are declared ESM?

Not in this case. These files don't get transformed by Vite, and are imported using the workerd module fallback service. This means they have strict module semantics, and require() can't be used to import an ES module. We should be able to keep the dynamic import() in the output rather than converting this to a require(). Not quite sure what the tsconfig.json incantation is though, will investigate more tomorrow. 馃憤

@mrbbot
Copy link
Author

mrbbot commented Mar 13, 2024

Looks like the magic incantation is "module": "Node16" and "moduleResolution": "Node16". This compiles to a CommonJS module, but keeps dynamic import()s in the output. 馃憤

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants