Skip to content

[2.x] Add a core entry point without pusher-js to the React, Vue and Svelte wrappers - #574

Closed
CaioBGalvao wants to merge 1 commit into
laravel:2.xfrom
CaioBGalvao:core-entry-without-pusher
Closed

CaioBGalvao wants to merge 1 commit into
laravel:2.xfrom
CaioBGalvao:core-entry-without-pusher

Conversation

@CaioBGalvao

Copy link
Copy Markdown

Fixes #570 without breaking anything, unlike #573.

The React, Vue and Svelte wrappers declare pusher-js as an optional peer, but import it unconditionally, so they fail to load without it (ERR_MODULE_NOT_FOUND). Apps that broadcast through Mercure, Socket.IO or the null broadcaster have to install pusher-js and ship it, although they never use it.

This adds a core entry point to each wrapper:

import { configureEcho, useEcho } from "@laravel/echo-react/core";

configureEcho({ broadcaster: "mercure" });

Why it doesn't break anything

  • The default entry point (@laravel/echo-react, -vue, -svelte) behaves exactly as today: it imports pusher-js and uses it as the Pusher client when the config doesn't pass one. configureEcho({ broadcaster: "reverb" }) keeps working with no change.
  • The IIFE builds are unchanged.
  • core exports the same API. It only skips the pusher-js import, so Pusher and Reverb users who prefer it pass the client themselves, as laravel-echo already expects.
  • Both entry points share one chunk, so importing from both in the same app still gives one Echo instance.

How

  • config/index.ts no longer imports pusher-js. It holds a default Pusher client that config/pusher.ts registers.
  • index.ts imports config/pusher.ts and re-exports core.ts.
  • The lib build has two entries, index and core, plus a ./core export in each package.json.
  • The shared chunk is named [name]-[hash].[format].js, to keep the .js extension the packages already ship.

Tests

tests/core.test.ts in each wrapper covers:

  • the core entry point loads when pusher-js cannot be imported;
  • the default entry point still uses pusher-js as the default client;
  • a Pusher passed to configureEcho wins over the default;
  • both entry points share the Echo instance.

I also packed the four packages and installed them in an empty project without pusher-js: @laravel/echo-{react,vue,svelte}/core loads, the default entry point fails exactly as it does today, and once pusher-js is installed the default entry point works and returns the same instance as core.

If you'd rather just make pusher-js a required peer, I'm happy to switch this PR to that.

🤖 Generated with Claude Code

The React, Vue and Svelte wrappers declare pusher-js as an optional
peer, but import it unconditionally, so they fail to load without it.

The default entry point keeps importing pusher-js and registering it as
the default Pusher client. The new `core` entry point exports the same
API without that import. Both entry points share one chunk, and with it
one Echo instance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@taylorotwell

Copy link
Copy Markdown
Member

I'm getting weary of AI generated attempts at this.

@CaioBGalvao

Copy link
Copy Markdown
Author

Sorry about that. I did use AI to help put this together, and I get why that's tiring to deal with. I'll leave it here. Thanks for everything you do for Laravel. @taylorotwell

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.

React, Vue and Svelte wrappers import pusher-js unconditionally, although it is an optional peer dependency

2 participants