Skip to content

fix: terminate global shim banner statements#162

Open
SanderMuller wants to merge 1 commit into
davidmyersdev:mainfrom
SanderMuller:fix/terminate-shim-banner-statements
Open

fix: terminate global shim banner statements#162
SanderMuller wants to merge 1 commit into
davidmyersdev:mainfrom
SanderMuller:fix/terminate-shim-banner-statements

Conversation

@SanderMuller

Copy link
Copy Markdown

Fixes #155.

The bug

The banner prepended to every optimized dependency has no semicolons terminating its lines. If a dependency's first line starts with ( or + (any UMD/IIFE bundle, e.g. bootstrap or jquery-ui), ASI won't insert a semicolon there, so the banner's last assignment merges with the dependency:

globalThis.process = globalThis.process || __process_polyfill + function ($) { /* ... */ }(jQuery)

+ and the call bind tighter than ||, and globalThis.process is already defined by the time the dep runs, so || short-circuits and the IIFE never executes. The library's plugins/widgets never register, and there's no error at load. It surfaces later as $(...).tooltip is not a function, .draggable is not a function, and similar.

Reported in #155, and originally in vitejs/vite#22167 where @sapphi-red confirmed it's this plugin's bug. #152 moved the main injection off esbuild.banner, but the optimizeDeps pre-bundle still emits this banner on both the rolldown and esbuild branches, so dependency pre-bundling is still affected on 0.28.0.

The fix

Terminate each statement in globalShimBanners. Every line is now a complete statement, so a following dependency can't merge into it.

Tests

test/unit/global-shim-banner.test.ts:

Unit suite, typecheck, and lint pass.

The dev banner is prepended to every optimized dependency. Its lines had no
trailing semicolons, so when a dependency began with a UMD/IIFE (its first
character is `(` or `+`), ASI merged the banner's last assignment with it:

  globalThis.process = globalThis.process || __process_polyfill + function () {}()

Since the global is already defined, || short-circuits and the IIFE never runs,
so libraries like bootstrap and jquery-ui silently fail to initialize under the
rolldown/esbuild dep optimizer. Terminating each statement keeps the dependency
a separate statement.

Fixes davidmyersdev#155
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.

Bug: Incorrect output when banner injected above IIFE

1 participant