Repository navigation
fix(webpack): support Svelte 5 alongside Svelte 3/4 - #11670
Open
NathanWalker wants to merge 3 commits into
main from
feat/webpack-svelte5
Open
Conversation
Copy link
Copy Markdown
Contributor
PR Checklist
What is the current behavior?
The Svelte configuration was written for Svelte 3/4. On a Svelte 5 project it aliases svelte$ to svelte/internal, which has no exports in Svelte 5. The build "succeeds" with warnings like export 'mount' was not found in 'svelte' (module has no exports) and the app fails at runtime. Without extra export conditions, svelte resolves to its server build anyway.
svelte-native 5 (nativescript-community/svelte-native#4 ) is built on Svelte 5.59's official custom renderer API. Components are compiled with compilerOptions.experimental.customRenderer = '@nativescript-community/svelte-native/dom/renderer' in svelte.config.js. Right now every Svelte 5 app has to patch the config in webpack.config.js: delete the alias and override resolve.conditionNames.
What is the new behavior?
@nativescript/webpack detects the installed svelte version (resolved from the project root). If svelte is not installed, it falls back to the range declared in the project's package.json. It then picks a configuration. No new options, and no changes needed in webpack.config.js:
| Detected svelte |
Configuration |
< 5 or not detectable |
Unchanged. Same output as before (electron-main target, svelte$ → svelte/internal alias, svelte-loader/svelte-loader-hot with hotReload + hotOptions.native). |
>= 5 |
No svelte$ alias and the base node target. resolve.conditionNames is ['svelte-custom-renderer', 'svelte', '...'] ('...' keeps webpack's defaults and their order, including development/production). A rule scoped to node_modules/esm-env adds browser for that package only. svelte-loader gets compilerOptions from svelte.config.js unchanged (plus dev) and its preprocess. |
svelte-custom-renderer is the official export condition Svelte 5.59 ships for custom renderers running outside a browser. With it, svelte, svelte/renderer, svelte/reactivity, svelte/store, svelte/legacy and svelte/internal/flags/custom-renderer resolve to their client builds. Without it, createRenderer throws custom_renderer_unavailable_on_server . browser is deliberately not added globally, because it would switch unrelated npm packages in the app to their browser builds.
The one exception is esm-env, which Svelte's runtime uses for its BROWSER and DEV flags. BROWSER gates Svelte's requestAnimationFrame loop, which drives transitions and svelte/motion; svelte-native installs a requestAnimationFrame shim for exactly this. Without browser for esm-env, BROWSER is false and that loop never ticks. A module rule therefore adds browser only when resolving from inside node_modules/esm-env. Keeping webpack's mode condition lets DEV resolve at build time instead of reading process.env.NODE_ENV at runtime.
HMR on Svelte 5
svelte-loader's hotReload is built on svelte-hmr, which does not support Svelte 5. svelte-loader 3.2 already skips it for Svelte 5. So for Svelte 5, hotReload/hotOptions are not set and compilerOptions.hmr is not forced on. Svelte 5's own hmr output targets Vite's import.meta.hot, and it has not been verified with the custom renderer. With --env.hmr, editing a .svelte file therefore falls back to the normal full app reload instead of in-place component replacement. Projects that want to try Svelte's compiler HMR can still set compilerOptions.hmr in svelte.config.js, and it is passed through unchanged.
Fits a patch release: Svelte 3/4 projects get the same configuration as before (covered by the existing snapshots, plus a test that the undetected-version path produces identical output).
Testing
packages/webpack5: npx jest
- New Svelte 5 snapshots (ios/android) plus unit tests for the conditions, the esm-env-scoped
browser rule, removing the alias, passing compilerOptions through without svelte-hmr, dev off in production, and detection from the declared range when svelte is not installed.
- The existing Svelte 4 snapshot tests run with svelte
4.2.19 mocked and their snapshot entries are untouched. A further test asserts that an undetectable version produces byte-identical output to Svelte 4.
- Snapshots on
main already fail for 7 suites, svelte included. They contain machine-specific absolute polyfill paths and a FixSourceMapUrlPlugin that no longer exists in src. This PR leaves those failures as they are. In this branch, running the svelte suite from a checkout path matching the committed snapshots passes every Svelte 5 test. The Svelte 4 entries show exactly the same pre-existing diff as on main.
- End-to-end bundle of the svelte-native demo app (Svelte 5.59.0, svelte-loader 3.2.4) with a plain
webpack.config.js, i.e. no alias or conditionNames workarounds:
- Current release: builds with
export 'mount'/'flushSync'/'onDestroy' ... was not found in 'svelte' warnings (broken app).
- This PR: ios dev, ios production and android builds compile cleanly. The vendor bundle contains
svelte/src/index-client.js, internal/client/**, internal/client/custom-renderer and internal/flags/custom-renderer.js, and no internal/server modules.
- The bundled module set is identical to the demo's device-tested manual workaround config (which added
browser globally). The one difference is that esm-env/dev-fallback.js is gone, because DEV now resolves through the development condition. As a control, building without the esm-env rule pulls in esm-env/false.js, so BROWSER would be false.
Known noise: svelte-loader prints "You should add "svelte" to the "resolve.conditionNames" array" at load time. It decides this with a regex over the text of webpack.config.js, and that file only calls @nativescript/webpack without listing the conditions. The warning is harmless here.
fix(webpack): support Svelte 5 alongside Svelte 3/4
62ede84
Detect the svelte major version installed in the project. For Svelte 5,
resolve the client runtime through the svelte-custom-renderer/svelte/browser
export conditions instead of aliasing svelte$ to svelte/internal, pass
svelte.config.js compilerOptions (e.g. experimental.customRenderer) through
to svelte-loader, and skip svelte-hmr, which does not support Svelte 5.
Svelte 3/4 projects get the same configuration as before.
Copy link
Copy Markdown
Copy link
Copy Markdown
|
Open in StackBlitz
npm i https://pkg.pr.new/@nativescript/core@11670
npm i https://pkg.pr.new/@nativescript/vite@11670
npm i https://pkg.pr.new/@nativescript/webpack@11670
commit: e50ff41
|
fix(webpack): resolve Svelte 5 via svelte-custom-renderer, not browser
dc8a0ed
Adding the browser condition globally switches unrelated packages to their
browser builds. Svelte 5.59's svelte-custom-renderer condition selects its
client runtime on its own; browser is kept only for esm-env so Svelte's
BROWSER flag (requestAnimationFrame loop for transitions and motion) stays on.
This was referenced Nov 24, 2026
fix(vite): support Svelte 5 apps via @nativescript/vite/svelte
#11671
Draft
feat: Svelte 5 support (svelte-native 5.0.0)
nativescript-community/svelte-native#4
Merged
Merge branch 'main' into feat/webpack-svelte5
e50ff41
This branch has not been deployed
No deployments
Sign up for free
to join this conversation on GitHub .
Already have an account?
Sign in to comment
Labels
None yet
1 participant