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

fix: bundling compatibility with webpack@5 #2519

Merged
merged 2 commits into from
Jun 20, 2023
Merged

fix: bundling compatibility with webpack@5 #2519

merged 2 commits into from
Jun 20, 2023

Conversation

AviVahl
Copy link
Contributor

@AviVahl AviVahl commented Jun 4, 2023

Purpose (TL;DR)

Ensure sinon bundles nicely for browsers without any errors or warnings.
#2411 re-fixed (after "exports" were added I think).

Background

When using webpack v5 to bundle cjs code that calls require('sinon'), it would have defaulted to "exports->require" and fail with multiple node API errors/warnings (util, timers, etc.).

This patch ensures that anyone who bundles sinon with a bundler that respects "exports" gets the (browser-compatible) esm version.

Tested on both webpack v5 and v4. should be noted that v4 worked even without this patch, as it automatically injected polyfills. webpack@5 no longer does so.

Solution

The esm version bundles without any errors/warnings, so make sure bundlers pick that one up.

How to verify

Use the same steps to reproduce in #2411
Modify the node_modules with this patch and rerun npx webpack --mode development

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

when using webpack v5 to bundle code that calls `require('sinon')` (cjs) , it would have defaulted to "exports->require" and fail with multiple node-api requirements (util, timers, etc.)

this patch ensures that anyone who bundles sinon for browser gets the (browser-compatible) esm version.

tested on both webpack v5 and v4. should be noted that v4 worked even without this patch, as it automatically injected polyfills.  v5 no longer does so. with this PR, people using webpack@4 to bundle sinon at least see size improvement, as the polyfills are no longer required.
@fatso83
Copy link
Contributor

fatso83 commented Jun 4, 2023

Thanks, seems good! Needs a test run, of course :-)

@AviVahl
Copy link
Contributor Author

AviVahl commented Jun 4, 2023

Looks like browserify doesn't like the esm in package.json->browser. I'll revert that one and leave the one in the exports. All should be well then.

browserify doesn't seem to like esm. leave that entry point alone, and ensure "exports" -> "browser" (which webpack@5 uses) is esm.
@AviVahl
Copy link
Contributor Author

AviVahl commented Jun 4, 2023

Reverted. Tests should (hopefully) now pass.

On a related note, please consider dropping browserify support... it's pretty outdated in 2023.
Might be better to test widely-used bundlers, like: webpack, rollup, esbuild, etc.

@AviVahl
Copy link
Contributor Author

AviVahl commented Jun 12, 2023

@fatso83 heya, any chance we can rerun ci on the smaller change?

@codecov
Copy link

codecov bot commented Jun 19, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (05f05ac) 96.00% compared to head (c649a14) 96.00%.

❗ Current head c649a14 differs from pull request most recent head 6e3f36e. Consider uploading reports for the commit 6e3f36e to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2519   +/-   ##
=======================================
  Coverage   96.00%   96.00%           
=======================================
  Files          40       40           
  Lines        1900     1900           
=======================================
  Hits         1824     1824           
  Misses         76       76           
Flag Coverage Δ
unit 96.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@AviVahl
Copy link
Contributor Author

AviVahl commented Jun 19, 2023

Now passes on CI. :) Would appreciate if this could land main, as it would allow us to remove workarounds to the bundling issues in several repositories.

@fatso83 fatso83 merged commit d220c99 into sinonjs:main Jun 20, 2023
8 checks passed
@AviVahl AviVahl deleted the fix-webpack-compatibility branch June 20, 2023 15:27
@fatso83
Copy link
Contributor

fatso83 commented Jun 22, 2023

Ironically, this seems to have broken MUI's webpack setup: mui/material-ui#37430 (comment) 👀

@AviVahl
Copy link
Contributor Author

AviVahl commented Jun 22, 2023

Yeah, webpack now resolves the esm version, so this config: https://github.com/mui/material-ui/blob/master/test/regressions/webpack.config.js#LL26C1-L29C8 probably needs to now say process: 'process/browser.js'

They might even be able to remove the ProvidePlugin completely if sinon was the only place needing this polyfill.

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.

None yet

2 participants