Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SignalR npm package not able to be bundled using esbuild #47674

Open
1 task done
hrueger opened this issue Apr 12, 2023 · 7 comments · Fixed by #48154
Open
1 task done

SignalR npm package not able to be bundled using esbuild #47674

hrueger opened this issue Apr 12, 2023 · 7 comments · Fixed by #48154
Labels
area-signalr Includes: SignalR clients and servers

Comments

@hrueger
Copy link

hrueger commented Apr 12, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hi,
I'm using the npm package @microsoft/signalr with version 7.0.4. I'm using esbuild for bundeling, however, it does not correctly resolve some dependencies, like ws.

This is due those hacks:

// In order to ignore the dynamic require in webpack builds we need to do this magic
// @ts-ignore: TS doesn't know about these names
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
webSocketModule = requireFunc("ws");
eventSourceModule = requireFunc("eventsource");

I don't think this is an issue of esbuild, since it believes that this is a dynamic require. Can we somehow fix this?

I confirmed that this is the issue, by patching the lib to use plain require instead of the requireFunc, then everything works correctly.

Expected Behavior

ws is resolved correctly when using the bundle option of esbuild.

Steps To Reproduce

https://github.com/hrueger/signalr-esbuild-issue

  1. yarn install
  2. yarn build
  3. See __webpack_require__ is still in dist/bundle.js instead of the ws module embedded in the bundle.

Exceptions (if any)

No response

.NET Version

n/a

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-signalr Includes: SignalR clients and servers label Apr 12, 2023
@BrennanConroy
Copy link
Member

Looks like an esbuild issue
evanw/esbuild#1921

@hrueger
Copy link
Author

hrueger commented Apr 12, 2023

Are you sure? I don't think so, to be honest. This is not about esm / cjs, but instead the workaround which is implemented for webpack here, breaks other bundlers (at least esbuild). To those, the call looks like a dynamic require, although it is actually not dynamic. Do you know, what I mean?

@shsuman
Copy link

shsuman commented Apr 18, 2023

We are facing the same issue with 5.0.17. The workaround was to replace the __non_webpack_require__ with require using webpack plugin.

{
      test: /@microsoft.signalr.dist.cjs.(HttpConnection|FetchHttpClient)\.js/,
      loader: 'string-replace-loader',
      options: {
             search: /__non_webpack_require__/g,
             replace: 'require',
             strict: true
       }
}

The question I want to ask is why is __non_webpack_require__ is being used because after searching online, it seems like it is not meant to be used in production code and no other package uses it.

@hrueger
Copy link
Author

hrueger commented Jul 12, 2023

Thanks!

@BrennanConroy
Copy link
Member

Unfortunately this issue will need to be reopened. The fix we made broke other important scenarios so we'll need to find a different way to fix this. Starting with 8.0.7 this esbuild issue will start happening again.

@BrennanConroy BrennanConroy reopened this Jul 9, 2024
@dotnet dotnet unlocked this conversation Jul 9, 2024
@BrennanConroy BrennanConroy removed this from the 8.0-preview7 milestone Jul 9, 2024
@rahul-sharma-uipath
Copy link

@BrennanConroy did it break multiple scenarios or just the esbuild thing?

@sschultze
Copy link

For bundling with rollup (for Electron Main), I also have to use an ugly workaround involving the replace plugin. Needless to say that this is very unsatisfactory.

replace({
    preventAssignment: true,
    include: [
        './node_modules/@microsoft/signalr/dist/**/*'
    ],
    values: {
        // @microsoft/signalr 8.0.7
        '/*#__PURE__*/': '',
        'new (requireFunc("tough-cookie")).CookieJar()': 'undefined',
        'this._fetchType = requireFunc("fetch-cookie")(this._fetchType, this._jar);': JSON.stringify(''),
        'requireFunc("ws")': 'undefined',
        'requireFunc("eventsource")': 'undefined'
    },
    delimiters: ['', '']
}),

In addition, the private WebSocket and EventSource properties have to be set in IHttpConnectionOptions manually. And this workaround wouldn't work with cookies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-signalr Includes: SignalR clients and servers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants
@BrennanConroy @hrueger @sschultze @shsuman @rahul-sharma-uipath and others