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

plugin-webpack: reference webpack loaders with require.resolve for yarn pnp compat #1611

Merged
merged 1 commit into from
Nov 15, 2020
Merged

Conversation

mxmul
Copy link
Contributor

@mxmul mxmul commented Nov 15, 2020

Replaces implicit Webpack loader resolution (e.g. loader: 'babel-loader') with an explicit require.resolve call (e.g. loader: require.resolve('babel-loader')). This should be a noop for regular npm/yarn installs, but is required when using Yarn 2 PnP and pnp-webpack-plugin (because Yarn 2 overrides Node's module resolution, but not Webpack's enhanced-resolve).

Context: https://github.com/arcanis/pnp-webpack-plugin#usage

In my project, I'm using Yarn 2 and pnp-webpack-plugin like so:

    ['@snowpack/plugin-webpack', {
      extendConfig: (config) => {
        return merge(config, {
          resolve: {
            plugins: [
              PnpWebpackPlugin,
            ],
          },
          resolveLoader: {
            plugins: [
              PnpWebpackPlugin.moduleLoader(module),
            ],
          },
        });
      },
    }],

Before this patch, the build fails with Entry module not found: Error: Can't resolve 'babel-loader'. After applying my changes, it succeeds.

Changes

Loader paths are resolved with require.resolve instead of Webpack's enhanced-resolve.

Testing

Existing tests pass. A real regression test (that installs the required packages under pnp) would be complex, and doesn't seem like it would provide a ton of value.

Docs

No documentation, since this is a bug fix.

@vercel
Copy link

vercel bot commented Nov 15, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/e3jjdwd93
✅ Preview: https://snowpack-git-fix-webpack-pnp.pikapkg.vercel.app

@merceyz
Copy link
Contributor

merceyz commented Nov 15, 2020

This should be a noop for regular npm/yarn installs, but is required when using Yarn 2 PnP

It's technically always required, otherwise the project relies on hoisting to bring the dependencies to the top level, which is not guaranteed to happen
https://yarnpkg.com/advanced/rulebook#packages-should-only-ever-require-what-they-formally-list-in-their-dependencies

@FredKSchott
Copy link
Owner

nice! makes sense. For some reason, I thought that Webpack only supported a name here, and not fully resolved paths.

@FredKSchott FredKSchott merged commit df529e9 into FredKSchott:master Nov 15, 2020
@mxmul mxmul deleted the fix-webpack-pnp branch November 16, 2020 00:18
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

3 participants