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

refactor: use webpack instead of webpack-sources #431

Merged
merged 7 commits into from
Nov 10, 2024

Conversation

yoannmoinet
Copy link
Contributor

@yoannmoinet yoannmoinet commented Oct 28, 2024

Coming from the discussion unjs/community#37

Having a dependency on webpack-sources forces plugin developers to break the compatibility webpack/webpack-sources by having them declare a dependency on webpack-sources instead of a peerDependency on webpack.

Instead, we could require webpack-sources as webpack using:

import { createRequire } from 'module'
[...]
const webpackRequire = createRequire(require.resolve('webpack'))
const RawSource = webpackRequire('webpack-sources').RawSource

And remove any dependency on webpack-sources or webpack.

The change "should" be mostly invisible.

  • Confirmed in both webpack 4 and 5 projects.

Copy link

pkg-pr-new bot commented Oct 28, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/unjs/unplugin@431

commit: dced339

@yoannmoinet yoannmoinet marked this pull request as ready for review October 29, 2024 09:26
@yoannmoinet
Copy link
Contributor Author

I will have a look at the failing test later today.

@sxzz sxzz changed the title chore(deps): use webpack instead of webpack-sources refactor: use webpack instead of webpack-sources Oct 29, 2024
@yoannmoinet
Copy link
Contributor Author

yoannmoinet commented Oct 29, 2024

🤔
I'm having difficulties to understand why the test started failing here after the main merge.
I can't reproduce it locally.
Anyone can assist here?

It doesn't seem to be super deterministic either.

@yoannmoinet
Copy link
Contributor Author

Do you know why the test is failing @sxzz?

@yoannmoinet
Copy link
Contributor Author

So sorry for hammering, but I'm stuck on this on a separate PR.
Any update @sxzz?
Also summoning @antfu, maybe you'll have an idea of what the issue may be.

@sxzz
Copy link
Member

sxzz commented Nov 1, 2024

It's OK. I'll check it out when I have time.

@sxzz
Copy link
Member

sxzz commented Nov 1, 2024

I believe PR is not compatible with Webpack 4.

image image

@yoannmoinet
Copy link
Contributor Author

yoannmoinet commented Nov 2, 2024

Thank you so much for looking into it.

I'll have a better look, I don't understand why it's not always failing though.
I thought the fallback here was specifically for this case where webpack doesn't use webpack-sources (aka in webpack 4).
Webpack 4 does use webpack-sources, but it doesn't re-export it 😞

sources
? new sources.RawSource(
// @ts-expect-error types mismatch
typeof emittedFile.source === 'string'
? emittedFile.source
: Buffer.from(emittedFile.source),
) as any
: {
source: () => emittedFile.source,
size: () => emittedFile.source!.length,
},

I'll double check how webpack 4 works there, and if it actually needs webpack-sources or not (it does use it).

@yoannmoinet
Copy link
Contributor Author

So, I couldn't find a good solution for webpack 4.

What I propose, to keep the best compatibility possible between webpack and webpack-sources is:

  • Add an optional peerDependency on webpack-sources@^1.4.1 (version used by latest webpack@4).
  • If require('webpack').sources exists, use it.
  • If require('webpack').sources doesn't exist, use require('webpack-sources').RawSource instead.
  • Add documentation about adding a dependency on webpack-sources:^1.4.1 for plugin developers so they don't break dependencies or side-load a potentially non-existing dependency.

This way we keep the compatibility most of the time, and do our best for webpack 4.

Cf: webpack/webpack#11345

Plugins should consume require("webpack").sources instead of require("webpack-sources") to avoid version conflicts for persistent caching

WDYT @sxzz ?

@yoannmoinet
Copy link
Contributor Author

yoannmoinet commented Nov 2, 2024

I went a different route after all.
It appears that you can actually require a package as a different dependent.

In our case we wanted to require the same webpack-sources as our peer webpack.

Doing:

import { createRequire } from 'module'
[...]
const webpackRequire = createRequire(require.resolve('webpack'))
const RawSource = webpackRequire('webpack-sources').RawSource

Makes it require the correct webpack-sources that our peer webpack expects.
And, we don't need to declare any of them as peerDependencies anymore.

(But, plugin developers should still declare their plugin's bundler as peerDependencies)

And it seems to pass the tests now 🙌

@sxzz
Copy link
Member

sxzz commented Nov 3, 2024

I need to test the PR more, please hold on.

@yoannmoinet
Copy link
Contributor Author

So sorry to ping you again @sxzz.
Did you have a chance to test the PR as you wanted? Can I help you in any way?

Copy link
Member

@sxzz sxzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sxzz sxzz merged commit 4013e15 into unjs:main Nov 10, 2024
12 checks passed
@yoannmoinet yoannmoinet deleted the yoann/remove-webpack-sources branch November 10, 2024 19:32
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.

2 participants