Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/6959.performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The generated `vite.config.js` now declares a hook filter on the plugin that redirects `react-dom/server` to `react-dom/server.node`, so the bundler no longer calls into it for every import in the module graph — on the Reflex docs site that was ~15,800 calls per build to rewrite a single specifier. The local plugin import in the config also carries a file extension now, silencing Vite's warning about features its native config loader cannot resolve.
1 change: 1 addition & 0 deletions packages/reflex-base/news/6959.performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`vite_config_template` declares a `resolveId` hook filter (`{ id: /react-dom\/server/ }`) on `vite-plugin-always-use-react-dom-server-node`. The plugin runs with `enforce: "pre"`, so without a filter rolldown invoked its JS handler for every import specifier in the graph (~15,800 calls on the Reflex docs build) to redirect the one specifier imported by `entry.server.node.tsx`; the filter is evaluated natively and skips the call otherwise. The template also imports `./vite-plugin-safari-cachebust.js` with its extension, which Vite's `configLoader: "native"` requires and warns about today.
26 changes: 14 additions & 12 deletions packages/reflex-base/src/reflex_base/compiler/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,25 +655,27 @@ def vite_config_template(
return rf"""import {{ fileURLToPath, URL }} from "url";
import {{ reactRouter }} from "@react-router/dev/vite";
import {{ defineConfig }} from "vite";
import safariCacheBustPlugin from "./vite-plugin-safari-cachebust";
import safariCacheBustPlugin from "./vite-plugin-safari-cachebust.js";

// Ensure that bun always uses the react-dom/server.node functions.
function alwaysUseReactDomServerNode() {{
return {{
name: "vite-plugin-always-use-react-dom-server-node",
enforce: "pre",

resolveId(source, importer) {{
if (
typeof importer === "string" &&
importer.endsWith("/entry.server.node.tsx") &&
source.includes("react-dom/server")
) {{
return this.resolve("react-dom/server.node", importer, {{
skipSelf: true,
}});
}}
return null;
resolveId: {{
filter: {{ id: /react-dom\/server/ }},
handler(source, importer) {{
if (
typeof importer === "string" &&
importer.endsWith("/entry.server.node.tsx")
) {{
return this.resolve("react-dom/server.node", importer, {{
skipSelf: true,
}});
}}
return null;
}},
}},
}};
}}
Expand Down
37 changes: 37 additions & 0 deletions tests/units/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,43 @@ def test_vite_config_template_pins_preview_host() -> None:
assert 'host: "127.0.0.1",' in config


def test_vite_config_template_imports_plugin_with_extension() -> None:
"""Local plugin imports carry a file extension.

Vite's native config loader (planned to become the default) cannot resolve
extensionless relative imports and warns about them today.
"""
from reflex.compiler import templates as compiler_templates

config = compiler_templates.vite_config_template(
base="/",
hmr=True,
force_full_reload=False,
experimental_hmr=False,
sourcemap=False,
)
assert 'from "./vite-plugin-safari-cachebust.js"' in config


def test_vite_config_template_filters_react_dom_server_resolve_hook() -> None:
"""The react-dom/server resolveId hook declares a hook filter.

Without a filter, rolldown calls the hook for every import in the module
graph, which dominates build time on large apps.
"""
from reflex.compiler import templates as compiler_templates

config = compiler_templates.vite_config_template(
base="/",
hmr=True,
force_full_reload=False,
experimental_hmr=False,
sourcemap=False,
)
assert "filter: { id: /react-dom\\/server/ }," in config
assert "handler(source, importer) {" in config


@pytest.mark.parametrize("minify", [True, False])
def test_compile_vite_config_reads_minify_env(
minify: bool, monkeypatch: pytest.MonkeyPatch
Expand Down
Loading