algolia / docsearch

:blue_book: The easiest way to add search to your documentation.
https://docsearch.algolia.com
MIT License
3.87k stars 374 forks source link

Unbundle dependencies from `@docsearch/js` #2224

Open rschristian opened 3 months ago

rschristian commented 3 months ago

Description

preact and @docsearch/react are listed as dependencies of @docsearch/js, however, both are inlined into the bundles. This totally breaks any usage of Docsearch on a Preact site as Preact, like React, requires that it be a singleton in order for functionality like hooks to work. Preact being bundled will always result in an additional copy being loaded.

This also introduces an additional issue in that most CDNs will try to (helpfully) provide listed dependencies of modules. With this setup, that means each of these modules are downloaded twice: once inlined into the bundle, and another time as a separate module. Now this can be worked around in some CDNs, but still isn't ideal, as the default behavior causes wasted network trips.

Steps to reproduce

  1. Load up NPM's code viewer (which annoyingly doesn't allow for direct links to files): https://www.npmjs.com/package/@docsearch/js?activeTab=code
  2. Look in the ESM bundle and spot Preact.
    • If you don't know what you're looking for, ctrl + f for xlink and you'll spot this line

Live reproduction:

N/A

Expected behavior

They're either dependencies or they're bundled, not both.

Environment