fix(drizzle): lazy load drizzle-kit to prevent production bundling#6
fix(drizzle): lazy load drizzle-kit to prevent production bundling#6deepshekhardas wants to merge 3 commits into
Conversation
The MCP endpoint was accepting only Bearer token but Payload convention uses 'payload-mcp-api-keys API-Key <key>' format. Now accepts both: - 'payload-mcp-api-keys API-Key <key>' (Payload convention) - 'Bearer <key>' (backwards compatible) This makes the plugin consistent with other Payload API-key surfaces. Fixes: payloadcms#16572
When using localizeStatus, the last version wasn't retrieved properly when publishing directly. This is because the version_updatedAt and version_createdAt weren't being set properly when publishing. The fix adds a check for localized status (_status) in locales to ensure main row data update happens even when only localized fields have changed. Fixes: payloadcms#16395
drizzle-kit/api was being required at module load time, causing it to be included in production bundle for OpenNext Cloudflare and other edge runtimes. This moves the require() inside the function so it's only loaded when actually needed (during migrations/schema push). Also fixes the same issue in postgres adapter. Fixes: payloadcms#16470
There was a problem hiding this comment.
2 issues found across 4 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/drizzle/src/postgres/requireDrizzleKit.ts">
<violation number="1" location="packages/drizzle/src/postgres/requireDrizzleKit.ts:4">
P0: The first line of the function calls `require('module')` but `require` is not available in ESM. The `import { createRequire } from 'module'` that was removed is necessary — it imports a Node.js built-in (not drizzle-kit), so it won't affect production bundling. Only the `const require = createRequire(...)` and the `require('drizzle-kit/api')` call need to be inside the function for lazy loading.</violation>
</file>
<file name="packages/drizzle/src/sqlite/requireDrizzleKit.ts">
<violation number="1" location="packages/drizzle/src/sqlite/requireDrizzleKit.ts:4">
P0: `require` is not defined in ESM scope — `require('module')` will throw a ReferenceError before the next line can define `require`. Keep `import { createRequire } from 'module'` at the top level (it's a Node.js built-in, safe for tree-shaking) and only move `const require = createRequire(import.meta.url)` inside the function to lazy-load `drizzle-kit/api`.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| const require = createRequire(import.meta.url) | ||
|
|
||
| export const requireDrizzleKit: RequireDrizzleKit = () => { | ||
| const { createRequire } = require('module') |
There was a problem hiding this comment.
P0: The first line of the function calls require('module') but require is not available in ESM. The import { createRequire } from 'module' that was removed is necessary — it imports a Node.js built-in (not drizzle-kit), so it won't affect production bundling. Only the const require = createRequire(...) and the require('drizzle-kit/api') call need to be inside the function for lazy loading.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/drizzle/src/postgres/requireDrizzleKit.ts, line 4:
<comment>The first line of the function calls `require('module')` but `require` is not available in ESM. The `import { createRequire } from 'module'` that was removed is necessary — it imports a Node.js built-in (not drizzle-kit), so it won't affect production bundling. Only the `const require = createRequire(...)` and the `require('drizzle-kit/api')` call need to be inside the function for lazy loading.</comment>
<file context>
@@ -1,10 +1,8 @@
-const require = createRequire(import.meta.url)
-
export const requireDrizzleKit: RequireDrizzleKit = () => {
+ const { createRequire } = require('module')
+ const require = createRequire(import.meta.url)
const {
</file context>
| const require = createRequire(import.meta.url) | ||
|
|
||
| export const requireDrizzleKit: RequireDrizzleKit = () => { | ||
| const { createRequire } = require('module') |
There was a problem hiding this comment.
P0: require is not defined in ESM scope — require('module') will throw a ReferenceError before the next line can define require. Keep import { createRequire } from 'module' at the top level (it's a Node.js built-in, safe for tree-shaking) and only move const require = createRequire(import.meta.url) inside the function to lazy-load drizzle-kit/api.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/drizzle/src/sqlite/requireDrizzleKit.ts, line 4:
<comment>`require` is not defined in ESM scope — `require('module')` will throw a ReferenceError before the next line can define `require`. Keep `import { createRequire } from 'module'` at the top level (it's a Node.js built-in, safe for tree-shaking) and only move `const require = createRequire(import.meta.url)` inside the function to lazy-load `drizzle-kit/api`.</comment>
<file context>
@@ -1,10 +1,8 @@
-const require = createRequire(import.meta.url)
-
export const requireDrizzleKit: RequireDrizzleKit = () => {
+ const { createRequire } = require('module')
+ const require = createRequire(import.meta.url)
const {
</file context>
Description
drizzle-kit/api was being required at module load time, causing it to be included in production bundle for OpenNext Cloudflare and other edge runtimes.
This moves the require() inside the function so it's only loaded when actually needed (during migrations/schema push).
Also fixes the same issue in postgres adapter.
Testing
Fixes: payloadcms#16470
Summary by cubic
Lazily load
drizzle-kit/apiso it’s not bundled in production builds for edge runtimes (e.g., OpenNext on Cloudflare); migrations and schema push still work. Fixespayloadcms/payload#16470.payloadcms/payload#16572.localizeStatuspublish: detect localized_statusupdates so version timestamps are set and the latest version is retrieved. Fixespayloadcms/payload#16395.Written for commit d8317b4. Summary will update on new commits. Review in cubic