-
Notifications
You must be signed in to change notification settings - Fork 955
Ensure that Node.js polyfills are pre-optimized before the first request #8688
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
petebacondarwin
merged 10 commits into
main
from
pbd/vite-plugin/pre-bundle-nodejs-polyfills
Mar 31, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
844feaf
Ensure that Node.js polyfills are pre-optimized before the first request
petebacondarwin bef3e4d
fixup! Ensure that Node.js polyfills are pre-optimized before the fir…
petebacondarwin f2ffd16
add test
petebacondarwin d4f1470
simplify the nodejs warnings plugin
petebacondarwin 24c9dce
fixup! Ensure that Node.js polyfills are pre-optimized before the fir…
petebacondarwin 4dfabe6
fixup! simplify the nodejs warnings plugin
petebacondarwin d7298d4
add debugging to e2e tests
petebacondarwin a3954ce
fixup! Ensure that Node.js polyfills are pre-optimized before the fir…
petebacondarwin 083b6b2
Initialize nodeJsCompatWarnings in configResolved hook
jamesopstad 6613be0
Fix wrangler-configs-validation e2e tests
jamesopstad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"@cloudflare/vite-plugin": patch | ||
--- | ||
|
||
Ensure that Node.js polyfills are pre-optimized before the first request | ||
|
||
Previously, these polyfills were only optimized on demand when Vite became aware of them. | ||
This was either because Vite was able to find an import to a polyfill when statically analysing the import tree of the entry-point, | ||
or when a polyfilled module was dynamically imported as part of a executing code to handle a request. | ||
|
||
In the second case, the optimizing of the dynamically imported dependency causes a reload of the Vite server, which can break applications that are holding state in modules during the request. | ||
This is the case of most React type frameworks, in particular React Router. | ||
|
||
Now, we pre-optimize all the possible Node.js polyfills when the server starts before the first request is handled. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe } from "vitest"; | ||
import { fetchJson, test, waitForReady } from "./helpers.js"; | ||
|
||
describe("prebundling Node.js compatibility", () => { | ||
describe.each(["pnpm", "npm", "yarn"])("using %s", (pm) => { | ||
test("will not cause a reload on a dynamic import of a Node.js module", async ({ | ||
expect, | ||
seed, | ||
viteDev, | ||
}) => { | ||
const projectPath = await seed("dynamic", pm); | ||
|
||
const proc = await viteDev(projectPath); | ||
const url = await waitForReady(proc); | ||
expect(await fetchJson(url)).toEqual("OK!"); | ||
expect(proc.stdout).not.toContain( | ||
"optimized dependencies changed. reloading" | ||
); | ||
expect(proc.stdout).not.toContain("[vite] program reload"); | ||
}); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/vite-plugin-cloudflare/e2e/fixtures/basic/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/vite-plugin-cloudflare/e2e/fixtures/basic/wrangler.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "cloudflare-vite-e2e-dynamic", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"build": "vite build", | ||
"dev": "vite", | ||
"lint": "eslint .", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/vite-plugin": "*", | ||
"@cloudflare/workers-types": "^4.20250204.0", | ||
"@eslint/js": "^9.19.0", | ||
"eslint": "^9.19.0", | ||
"eslint-plugin-react-hooks": "^5.0.0", | ||
"eslint-plugin-react-refresh": "^0.4.18", | ||
"globals": "^15.14.0", | ||
"typescript": "~5.7.2", | ||
"typescript-eslint": "^8.22.0", | ||
"vite": "^6.1.0", | ||
"wrangler": "^3.108.1" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/src/dynamic.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// This dynamically imported module relies upon Node.js | ||
// When Vite becomes aware of this file it will need to optimize the `node:asset` library | ||
// if it hasn't already. | ||
|
||
import assert from "node:assert/strict"; | ||
|
||
assert(true, "the world is broken!"); | ||
export const x = '"OK!"'; |
8 changes: 8 additions & 0 deletions
8
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
async fetch() { | ||
// By dynamically importing the `./dynamic` module we prevent Vite from being able to statically | ||
// analyze the imports and pre-optimize the Node.js import that is within it. | ||
const { x } = await import("./dynamic"); | ||
return new Response(x); | ||
}, | ||
} satisfies ExportedHandler; |
7 changes: 7 additions & 0 deletions
7
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"files": [], | ||
"references": [ | ||
{ "path": "./tsconfig.node.json" }, | ||
{ "path": "./tsconfig.worker.json" } | ||
] | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/tsconfig.node.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", | ||
"target": "ES2022", | ||
"lib": ["ES2023"], | ||
"module": "ESNext", | ||
"skipLibCheck": true, | ||
|
||
/* Bundler mode */ | ||
"moduleResolution": "bundler", | ||
"allowImportingTsExtensions": true, | ||
"isolatedModules": true, | ||
"moduleDetection": "force", | ||
"noEmit": true, | ||
|
||
/* Linting */ | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noUncheckedSideEffectImports": true | ||
}, | ||
"include": ["vite.config.ts"] | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/tsconfig.worker.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.node.json", | ||
"compilerOptions": { | ||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.worker.tsbuildinfo", | ||
"types": ["@cloudflare/workers-types/2023-07-01", "vite/client"] | ||
}, | ||
"include": ["src"] | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/vite.config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { cloudflare } from "@cloudflare/vite-plugin"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
plugins: [cloudflare({ inspectorPort: false, persistState: false })], | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/vite-plugin-cloudflare/e2e/fixtures/dynamic/wrangler.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name = "cloudflare-vite-e2e-dynamic" | ||
main = "./src/index.ts" | ||
compatibility_date = "2024-12-30" | ||
compatibility_flags = ["nodejs_compat"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.