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

feat(gatsby): add pnp resolving by passing rootdir in pkg resolving #12163

Merged
merged 3 commits into from Mar 6, 2019

Conversation

arcanis
Copy link
Contributor

@arcanis arcanis commented Feb 28, 2019

Description

The current resolution algorithm takes loads the plugins using require.resolve via gatsby, meaning that PnP will block them unless they're listed in gatsby's dependencies. This diff ensures that the plugins are loaded from the package that contains the configuration.

Related Issues

#10245

@arcanis
Copy link
Contributor Author

arcanis commented Feb 28, 2019

The fail doesn't seem related, right?

@arcanis arcanis changed the title Adds a rootDir config option used to resolve plugins Pass the rootDir to the plugin resolver Feb 28, 2019
@wardpeet
Copy link
Contributor

Could you merge in master? I don't think you have the latest one.

@wardpeet wardpeet changed the title Pass the rootDir to the plugin resolver feat(gatsby): add pnp resolving by passing rootdir in pkg resolving Feb 28, 2019
@wardpeet wardpeet added the status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response label Feb 28, 2019
@arcanis
Copy link
Contributor Author

arcanis commented Feb 28, 2019

All good now 👍

@arcanis
Copy link
Contributor Author

arcanis commented Mar 1, 2019

Anything else needed?

@pieh pieh self-assigned this Mar 1, 2019
@pieh
Copy link
Contributor

pieh commented Mar 2, 2019

Small thing seems to be missing - in https://github.com/gatsbyjs/gatsby/blob/a67864b087b0966a14ea94df2833b3e6f7bc2b7f/packages/gatsby/src/bootstrap/load-plugins/index.js#L40-L42

we should have:

module.exports = async (config = {}, rootDir) => {
  // Collate internal plugins, site config plugins, site default plugins
  const plugins = loadPlugins(config, rootDir)

I also need to check how themes will behave -they will have their own gatsby-config with plugins and package.json, so plugins defined in themes would need to be resolved relative to theme directory and not root directory I think. Just not sure if we track that when we are merging multiple gatsby-config files.

@pieh
Copy link
Contributor

pieh commented Mar 4, 2019

I also need to check how themes will behave -they will have their own gatsby-config with plugins and package.json, so plugins defined in themes would need to be resolved relative to theme directory and not root directory I think. Just not sure if we track that when we are merging multiple gatsby-config files.

Ok, so as suspected, we currently break in master if theme plugin packages are not hoisted, but only way those wouldn't be hoisted is (I think - please correct me if I'm wrong) if site and theme (or one theme and another theme) depend on incompatible plugin versions (which would probably cause a lot more issues than jest resolving part).

I still think we will need to add tracking where plugin is being declared - because this alone will work with standalone Gatsby sites, but will break when themes are being used and PnP is being used.

@pieh
Copy link
Contributor

pieh commented Mar 4, 2019

So let's merge this one in as this doesn't introduce regressions and solves plugin resolution for most common usecase (right now).

@arcanis
Copy link
Contributor Author

arcanis commented Mar 4, 2019

but only way those wouldn't be hoisted is (I think - please correct me if I'm wrong) if site and theme (or one theme and another theme) depend on incompatible plugin versions (which would probably cause a lot more issues than jest resolving part).

Fwiw the exact hoisting algorithm isn't guaranteed by any package manager, so this assumption is unsafe. While it usually works kinda like you would expect, the case you mention (conflict between one theme and another) might happen. In fact, that's part of what ESLint is working to fix in their next release (eslint/eslint#11388).

@pieh pieh removed the status: awaiting reviewer response A pull request that is currently awaiting a reviewer's response label Mar 4, 2019
@pieh
Copy link
Contributor

pieh commented Mar 4, 2019

Fwiw the exact hoisting algorithm isn't guaranteed by any package manager, so this assumption is unsafe.

That's true. The main point was that we currently rely on plugin packages being installed in main site node_modules right now, and this PR doesn't change that.

There is one case I missed earlier:
We add one of plugins by default in

processPlugin({
resolve: `gatsby-plugin-page-creator`,
options: {
path: slash(path.join(program.directory, `src/pages`)),
pathCheck: false,
},
})

and this one is in gatsby dependencies -

"gatsby-plugin-page-creator": "^2.0.8",

If I understand correctly - we need to resolve that from gatsby package directory and not from site directory? I.e. change:

  plugins.push(
    processPlugin({
-      resolve: `gatsby-plugin-page-creator`,
+      resolve: require.resolve(`gatsby-plugin-page-creator`),
      options: {
        path: slash(path.join(program.directory, `src/pages`)),
        pathCheck: false,
      },
    })
  )

@arcanis
Copy link
Contributor Author

arcanis commented Mar 4, 2019

Good catch! I didn't spot it as I've added a Gatsby fallback in my PnP code - but the less reasons there is for such a mechanism the better it is 😃

@sidharthachatterjee
Copy link
Contributor

@arcanis Does #12315 remove the need for this?

@arcanis
Copy link
Contributor Author

arcanis commented Mar 6, 2019

@sidharthachatterjee no, both PRs are orthogonal (I'll leave more details on the other one)

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Thanks! We will probably need some adjustments for themes later on, but this doesn't break things when not using PnP, so let's ship this!

@pieh pieh merged commit 72e0d6f into gatsbyjs:master Mar 6, 2019
@arcanis arcanis deleted the rootdir branch March 6, 2019 21:22
@arcanis
Copy link
Contributor Author

arcanis commented Mar 6, 2019

Awesome, thanks!

DSchau pushed a commit that referenced this pull request Mar 15, 2019
## Description

This diff adds `pnp-webpack-plugin` in the configuration so that users don't have to manually extend the Webpack configuration to support PnP.

Note that the plugin [is a strict no-op](https://github.com/arcanis/pnp-webpack-plugin/blob/master/index.js#L110-L138) when not operating within a PnP environment, so it shouldn't break any existing workflow 🙂 

## Related Issues

Part of #10245 
Depends on #12163
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

4 participants