Skip to content

fix(vite): replace the browser preload helper before esbuild minifies it - #11425

Merged
NathanWalker merged 1 commit into
NativeScript:mainfrom
sitefinitysteve:fix/vite-dynamic-import-plugin-pre-minify
Sep 10, 2026
Merged

NathanWalker merged 1 commit into
NativeScript:mainfrom
sitefinitysteve:fix/vite-dynamic-import-plugin-pre-minify

Conversation

@sitefinitysteve

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

dynamicImportPlugin replaces Vite's browser __vitePreload helper (which touches document and window) with a native one, but it does so from generateBundle using a literal regex on the identifier. With build.minify: 'esbuild' Vite's vite:esbuild-transpile plugin has already renamed __vitePreload in renderChunk, so the regex misses, the browser helper ships, and its window.dispatchEvent in the rejection path turns every failed dynamic import into ReferenceError: window is not defined with a misattributed stack. Details in #11423.

What is the new behavior?

The transform runs in renderChunk as an enforce: 'post' plugin. User post plugins run before buildPlugins.post (where the esbuild minifier lives), so the helper is still named when it is replaced. renderChunk returns null when nothing changed, so untouched chunks and already-transformed code are left alone. transformDynamicImports is unchanged.

Specs cover the plugin shape (post + renderChunk, no generateBundle), the replacement through renderChunk, and the no-op/idempotent return.

Verified with a real ns build ios of a blank Vue app with build.minify: 'esbuild': the emitted bundle contains the native helper and no vite:preloadError / document.createElement("link"); the published 8.0.5 bundle has the reverse. At runtime a deliberately failing import() now reports the real load error.

Fixes #11423.

@nx-cloud

nx-cloud Bot commented Sep 10, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 7d8ead3

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 37s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded 6s View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-09-10 00:05:23 UTC

@pkg-pr-new

pkg-pr-new Bot commented Sep 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11425
npm i https://pkg.pr.new/@nativescript/vite@11425
npm i https://pkg.pr.new/@nativescript/webpack@11425

commit: 7d8ead3

@NathanWalker
NathanWalker merged commit 9388630 into NativeScript:main Sep 10, 2026
7 checks passed
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.

[Vite] dynamicImportPlugin silently no-ops on minified builds, masking dynamic-import errors as ReferenceError: window is not defined

2 participants