-
Notifications
You must be signed in to change notification settings - Fork 194
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
webpack version detection should use feature detection #308
Comments
Or |
The issue with feature detection is that we need to import stuff from Webpack that is only on a certain version. If |
would it be possible to provide the path to webpack to the constructor? |
Hmm, that would be possible, but is it possible to describe the use cases and current limitations to me so I could understand the situation more? |
I'm trying to add webpack 5 support for storybook whilst also still allowing webpack 4 support. |
@ndelangen I am right there at the moment; trying to make the new storybook alpha work with react refresh. It seems that the culprit is @pmmmwh Is there way to force using webpack 5 or 4 and override the automatic detection? |
Not currently. I would be open to add that, but it would also be quite clear that you're (mostly) on your own if you opt into that.
Can I ask more about why not opt for resolutions or aliases (in a monorepo), or for peer dependencies on Webpack? How is the dependency chain actually configured and why would Node not resolve properly? |
@pmmmwh I tried using yarn resolutions and I encountered a number of errors — not recall what those were, to be honest. My guess is that since Storybook uses a hybrid model in order to support both Webpack 4 and 5, somehow confusion comes into play. I will give it another try today and I come up with more context I will follow up here. |
We can't enforce a single version of webpack in storybook, because we use 1 (fixed) version of webpack to build the manager, and then the user can control what version of webpack to build the preview with. |
I'm also facing this using pnpm package manager in a monorepo. I have webpack@5 installed in the workspace root that is used for backend projects, but for a CRA app (that also uses Storybook) it's still on webpack v4 as CRA hasn't landed support for it yet. The react-refresh plugin is trying to import the wrong version of webpack (5) and failing with:
at
I work around it by forcing pnpm to add webpack@4 as a dependency of react-refresh: function readPackage(pkg) {
// TODO: webpack5 https://github.com/facebook/create-react-app/issues/9994
if (
pkg.name === '@pmmmwh/react-refresh-webpack-plugin' &&
pkg.peerDependencies?.webpack?.startsWith('>=4')
) {
pkg.dependencies = {
...pkg.dependencies,
webpack: '4.46.0',
};
}
return pkg;
}
module.exports = {
hooks: {
readPackage,
},
}; See also: pnpm/pnpm#3437 |
I think the main problem that I'm unsure here is how should we pull in files when It is quite easy to make the plugin to detect current version on the compiler, but the difference between Webpack 4 and Webpack 5 is larger that we would need to require Webpack 5 specific files, which means we are again dependent on |
Webpack should dependency inject webpack into the Plugin.apply call, maybe it already does? |
What are the drawbacks of doing dynamic requires as needed? Like this (pseudocode): let moduleInfo;
if (compiler.version === 5) {
moduleInfo = require('webpack/lib/moduleInfo');
} else if (compiler.version === 4) {
const compilation = require('webpack/lib/compilation');
moduleInfo = compilation.stats.modules.info;
}
moduleInfo.forEach(...) I see that you're already doing this in some places, and if you say that it's easy to detect webpack's version from the compiler object, I'm not sure if I got what is blocking us here then. |
Consider this package getting hoised to the top of a monorepo, Now a sub-package in this monorepo depends on webpack 4 and runs it, and uses this plugin. Webpack 4 is adding this addon, which will require webpack (will require webpack 5, because it's hoisted). You can simply not assume that the webpack version you require is the same as the webpack version your plugin is invoked with. |
The issue is the |
Now I understand, thank you all for the explanations. I assume this is a quite common scenario for loaders and plugins, we could see what others are doing. I'm not sure if they rely on @alexander-akait @sokra by any chance could you give any tips for us here? TLDR: we have multiple versions of webpack in the |
For webpack v5 please use |
TIL I can do I guess this solves the issue of having WP4 hoisted and wanted to use WP5, but if the hoisted version is WP5 and the local version is WP4 this would sill break as there is a require call ... I'll try to test this out. |
|
Yep I think this would solve most if not all of our problems. Thanks for chiming in! Is there any lower-bound version for WP5 to use |
|
For a while, there will always be new versions in the future, and the hoisting problem will be there always. |
Also if you use |
Is there a way to access Edit: Hmm, never mind, tapping through |
Hi @ndelangen @rdsedmundo - I've implemented proper version detection and pass-through imports on the compiler instance for Webpack 5 in #415. Would you mind testing it out in your projects? npm install -D -S git://github.com/pmmmwh/react-refresh-webpack-plugin.git#feat/feature-detection
yarn add -D git://github.com/pmmmwh/react-refresh-webpack-plugin.git#feat/feature-detection
pnpm add -D git://github.com/pmmmwh/react-refresh-webpack-plugin.git#feat/feature-detection I'll merge that once I got around to fill out tests for the changes and we can confirm that the PR works in reality. |
It worked for my project. Thanks for working on that! 🎉 |
the webpack version is determined using a require, but this is problematic in monorepos where the location of this plugin and the location of webpack can be unexpected.
Is there some way to infer the version we're dealing with from the compiler passed into
apply
?The text was updated successfully, but these errors were encountered: