Skip to content

fix(compiler): preserve bundler directive comments in es5 output#6779

Merged
johnjenkins merged 1 commit into
stenciljs:mainfrom
Arul1998:fix/preserve-bundler-comments-es5
Jul 17, 2026
Merged

fix(compiler): preserve bundler directive comments in es5 output#6779
johnjenkins merged 1 commit into
stenciljs:mainfrom
Arul1998:fix/preserve-bundler-comments-es5

Conversation

@Arul1998

Copy link
Copy Markdown
Contributor

What is the current behavior?

GitHub Issue Number: #4122

The Stencil runtime's lazy loader (src/client/client-load-module.ts) annotates its dynamic import() with bundler directive comments — /* @vite-ignore */, /* webpackInclude: ... */, /* webpackExclude: ... */, and /* webpackMode: "lazy" */ — so that downstream bundlers handle the non-statically-analyzable import correctly.

With buildEs5 enabled, those comments are lost in the dist/esm-es5 output. They survive the TypeScript ES5 transpilation, but are then stripped by terser: getTerserOptions doesn't configure format.comments, so terser's default applies, which only keeps license-style comments (@license, @preserve, /*! ... */).

As a result:

  • Vite emits a warning for every consumer of an es5-enabled library: The above dynamic import cannot be analyzed by Vite. (this is the warning reported in bug: buildEs5 emits warning with Vite #4122)
  • webpack silently loses its webpackInclude/webpackExclude/webpackMode lazy-loading hints in the es5 output.

What is the new behavior?

The ES5 minification options now pass terser a format.comments pattern that preserves bundler directive comments (@vite-ignore and webpack*: magic comments) in addition to terser's default license-comment behavior. The directives now ship in the esm-es5 output, Vite no longer warns, and webpack's hints are honored.

This is a compiler-only change: it does not add any code to the runtime, and modern (non-es5) output is unaffected. Regular comments are still stripped from es5 output as before.

Documentation

N/A

Does this introduce a breaking change?

  • Yes
  • No

Testing

  • Added unit tests in src/compiler/optimize/test/optimize-module.spec.ts covering the ES5 path: bundler directive comments are preserved inside the dynamic import(), ordinary line/block comments are still removed, and license comments are still preserved.
  • Manually verified end-to-end: built a component library with buildEs5 enabled against this branch and confirmed the directive comments are present in dist/esm-es5. Ran the output through Vite's dev-server transform (vite:import-analysis): the current published output reproduces the exact warning from bug: buildEs5 emits warning with Vite #4122, and the output built with this fix produces no warning.

Other information

esm-es5 output before this change:

return import("./".concat(o,".entry.js").concat("")).then(...)

Terser was stripping the /* @vite-ignore */ and webpack magic comments (webpackInclude, webpackExclude, webpackMode) from the dynamic import() in the lazy loader when transpiling to ES5, because getTerserOptions relied on terser's default comment handling, which only keeps license-style comments. This caused Vite to warn that the dynamic import cannot be analyzed, and webpack to ignore its lazy-loading hints in esm-es5 output.

Preserve bundler directive comments through ES5 minification so downstream bundlers can honor them.

fixes stenciljs#4122
@Arul1998
Arul1998 requested a review from a team as a code owner July 17, 2026 09:49
@johnjenkins

Copy link
Copy Markdown
Contributor

hey again @Arul1998 - thanks for looking into this, but ES5 builds are being dropped in the next release of Stencil so this work won't be necessary.

@johnjenkins johnjenkins reopened this Jul 17, 2026
@johnjenkins

Copy link
Copy Markdown
Contributor

Edit ... after looking, I changed my mind as it's such a small change 😅

@johnjenkins
johnjenkins added this pull request to the merge queue Jul 17, 2026
Merged via the queue into stenciljs:main with commit f82296d Jul 17, 2026
28 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.

2 participants