Skip to content

fix(page-agent): wrap demo bundle helpers in local scope#430

Open
voidborne-d wants to merge 1 commit intoalibaba:mainfrom
voidborne-d:fix/iife-reinjection-scope
Open

fix(page-agent): wrap demo bundle helpers in local scope#430
voidborne-d wants to merge 1 commit intoalibaba:mainfrom
voidborne-d:fix/iife-reinjection-scope

Conversation

@voidborne-d
Copy link
Copy Markdown
Contributor

Summary

  • wrap the generated page-agent.demo.js chunk in an extra function scope during bundling
  • keep Vite/esbuild helper variables out of the global scope on repeated script injection
  • preserve the existing runtime behavior while avoiding redeclaration collisions in reinjected pages

Why

Issue #426 reports "St has already been declared" after re-injecting the built script on some pages.

I traced this to helper variables emitted before the current IIFE prelude, for example:

var St = Object.defineProperty
...
(function(){ ... })()

Those top-level helpers can leak into the page realm and collide when the same bundle is injected again. Wrapping the final emitted chunk gives the whole bundle, including helper prelude code, its own local scope.

Validation

  • npm install --ignore-scripts
  • npm run build:demo
  • verified the built file now starts with (() => {
  • parsed the built bundle twice in the same VM script without redeclaration errors

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

3 participants