Mixing core-js global polyfills and core-js-pure in one dependency graph: which mode should a shared library depend on
0 reputation · 07 Feb 2022, 21:54 UTC
0 reputation · 07 Feb 2022, 21:54 UTC
A workspace contains an application plus several shared packages. The app and one package load the global core-js build (core-js 3), while another package imports from core-js-pure to avoid polluting globals. The goal is to decide which mode the shared library should declare as a dependency, given that both builds ship the same implementation but expose different APIs.
The uncertainty is the interoperability boundary: pure-mode code does not observe globals patched by the global build, so instanceof and prototype checks may behave differently across modules depending on which mode each component uses. Assume core-js 3 and Babel with useBuiltIns: 'usage' and a matching corejs: 3 option.
Array.prototype in the app, will pure-mode array methods in the library reflect those patches, or does each mode operate on its own wrapped implementations?35275 reputation · 08 Feb 2022, 05:46 UTC
For a shared library intended for distribution, declare core-js-pure as the dependency mode. This avoids adding polyfill code to the consumer's global namespace and lets each consumer decide which polyfills to apply (e.g., via useBuiltIns: 'usage' and corejs: 3 in Babel).
Both core-js (global build) and core-js-pure provide the same polyfill implementations and patch the same built-in prototypes (Array.prototype, String.prototype, etc.). If an application has already patched Array.prototype through the global core-js build, a library that imports core-js-pure will apply the same patch again; the operation is idempotent and results in identical prototype state. The two modes do not operate on separate wrapped implementations.
core-js and core-js-pure in the same bundle can duplicate module code if the bundler does not dedupe shared dependencies.core-js modules. A production build with proper dedup will show a single copy of the polyfill code.Choosing core-js-pure for a published library eliminates the risk of unexpectedly modifying the consumer's globals. The cost is a modest increase in the library's own bundle size, because the polyfill implementations are included directly rather than pulled from the host environment. This trade-off is generally favorable for library authors who want predictable, isolated behavior.
To refine the dedup guidance for your specific setup, which bundler and configuration are you using? This helps determine whether automatic module deduplication is active.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.