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] Extract internal plugin for automatically creating pages so we can reuse for other directories #4490

Merged
merged 19 commits into from
Jun 14, 2018

Conversation

nodox
Copy link
Contributor

@nodox nodox commented Mar 12, 2018

The current version of gatsby only allows auto component page generation from the /src/pages. We want the ability for the user to tell gatsby which folder to use for auto pages generation.

Here we create a new plugin that gatsby can use to supply a path option which denotes what directory can be used for auto page creation.

closes #2918
closes #2424
closes #2839
closes #2514

@gatsbybot
Copy link
Collaborator

gatsbybot commented Mar 12, 2018

Deploy preview for gatsbygram ready!

Built with commit 778e427

https://deploy-preview-4490--gatsbygram.netlify.com

@gatsbybot
Copy link
Collaborator

gatsbybot commented Mar 12, 2018

Deploy preview for using-drupal ready!

Built with commit 32edb33

https://deploy-preview-4490--using-drupal.netlify.com

Copy link
Contributor Author

@nodox nodox left a comment

Choose a reason for hiding this comment

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

@KyleAMathews left some comments on things I was unclear about in the description as well. Looking for guidance on how to proceed from here.

Commits will be squashed and formatted once everything is finalized.

@@ -130,7 +130,7 @@ module.exports = async (config = {}) => {
// Add internal plugins
const internalPlugins = [
`../../internal-plugins/dev-404-page`,
`../../internal-plugins/component-page-creator`,
`../../internal-plugins/component-page-creator`, // ???
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe we should remove this however I do not know how to require the new plugin and supply and options. My understanding is that this would be supplied by user in the gatsby-config.js file.

Copy link
Contributor

Choose a reason for hiding this comment

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

We'd keep src/pages as where components convert to pages automatically. That's one built-in constant we have.

What breaking this out to a plugin accomplishes is it's now easy to setup the same thing for other pages.

@@ -142,7 +142,7 @@ module.exports = async (config = {}) => {
})

// Add plugins from the site config.
if (config.plugins) {
if (config.plugins) { // ???
Copy link
Contributor Author

@nodox nodox Mar 12, 2018

Choose a reason for hiding this comment

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

Again not sure if its enough to just add the plugin at the user config level such as

{
    resolve: 'gatsby-plugin-page-creator',
    option: {
        path: './src/pages'
    }
}

const { createPage, deletePage } = boundActionCreators
const program = store.getState().program
const exts = program.extensions.map(e => `${e.slice(1)}`).join(`,`)
const pagesDirectory = systemPath.posix.join(program.directory, pluginOptions.path)
Copy link
Contributor Author

@nodox nodox Mar 12, 2018

Choose a reason for hiding this comment

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

The change occurred on this line.
Before:

const pagesDirectory = systemPath.posix.join(program.directory, 'src/pages')

After

const pagesDirectory = systemPath.posix.join(program.directory, pluginOptions.path)

Are there are places where I need to make a change in this file?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't see anywhere else where src/pages is hard-coded than no.

"keywords": [
"gatsby"
],
"author": "Steven Natera <tektekpush@gmail.com>",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an internal plugin therefore should Kyle be the author?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah — though you can add yourself as a contributor (or if authors can have multiple names, add yourself there).

@nodox
Copy link
Contributor Author

nodox commented Mar 12, 2018

Since this clones the official default starter, the would also need to be modified to include our new plugin. Would we create a breaking change with existing gatsby projects?

https://www.gatsbyjs.org/docs/gatsby-starters/

@KyleAMathews
Copy link
Contributor

Thanks for taking this on! This is a simple bit of functionality that'd be useful to be able to use in more ways.

Let's keep our setup as it is now and ship this as a new plugin people can install if they want additional "pages" directories.

@nodox nodox force-pushed the plugin-page-creator branch 4 times, most recently from 6b80f96 to 60e7de1 Compare April 1, 2018 18:35
A plugin to automatically create pages from a given path.

The current method only works when page components are present in src/pages.
Now a user can define custom paths to their page components and are not
limited to src/pages.
@nodox nodox changed the title [WIP][Refactor] auto create pages from a directory other than the default option [feat] auto create pages from a directory other than src/pages Apr 1, 2018
@nodox
Copy link
Contributor Author

nodox commented Apr 1, 2018

@KyleAMathews PR is all cleaned up.

Produced two blog posts in this work: How to contribute to Gatsby and How to auto create pages with gatsby-plugin-page-creator.

Copy link
Contributor

@KyleAMathews KyleAMathews left a comment

Choose a reason for hiding this comment

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

You still need to replace the internal plugin with this newly extracted plugin. You'll want to add gatsby-plugin-page-creator as a dependency of gatsby and then use it in the internal plugins stack instead of the existing implementation you copied out.

@@ -0,0 +1,40 @@
# gatsby-plugin-page-creator

Plugin for creating `File` nodes from the file system. The various "transformer"
Copy link
Contributor

Choose a reason for hiding this comment

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

Description needs updated

@@ -0,0 +1,21 @@
const systemPath = require(`path`)
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to keep sharing these internal utility functions with gatsby core

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually this isn't necessary now since we're removing layouts in v2 — so this is the only package using this.

@KyleAMathews
Copy link
Contributor

Getting close!

@nodox
Copy link
Contributor Author

nodox commented Apr 6, 2018

Please see last commit. I need a bit more guidance. I required the file in package.json but I don't know what to do to make this work. Step by step instructions or code sample would be helpful to get this going.

@KyleAMathews

@KyleAMathews
Copy link
Contributor

That looks correct! What happens when you test it?

@nodox
Copy link
Contributor Author

nodox commented Apr 9, 2018

Everything builds fine but the src/pages directory does not auto create pages for the components present.

@nodox
Copy link
Contributor Author

nodox commented Apr 10, 2018

How about we break this up into two smaller PRs?

  • one for the plugin (which is all done and tested)
  • and the other for replacing Gatsby internal plugins with the new solution

I'm happy to own both PRs in order to unblock the release of the page creator plugin. No need to keep the community waiting!

@KyleAMathews
Copy link
Contributor

You're really close to getting this working internally as well so let's just finish this up.

I noticed you're not setting the version. Double-check that you're adding all the fields that processPlugin is adding

Working version using the page creator plugin. Need a better way to
resolve. Need a better way to get the version number which is hardcoded
right now.
@nodox
Copy link
Contributor Author

nodox commented Apr 11, 2018

Now its working. See last two commits. The version number can be omitted which I kind of like because I'm not sure how to get the package version dynamically.

@KyleAMathews
Copy link
Contributor

Deploy preview for using-glamor failed.

Built with commit 601c8f1

https://app.netlify.com/sites/using-glamor/deploys/5acd82213813f049f847a458

@KyleAMathews
Copy link
Contributor

Deploy preview for using-glamor failed.

Built with commit 87e5a16

https://app.netlify.com/sites/using-glamor/deploys/5acd838473f2cf077392e746

@@ -160,11 +160,13 @@ module.exports = async (config = {}) => {

// Add the auto page creator plugin
plugins.push({
resolve: `gatsby-plugin-page-creator`,
resolve: slash(`${process.cwd()}/node_modules/gatsby-plugin-page-creator`),
Copy link
Contributor

Choose a reason for hiding this comment

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

You'd want to use require.resolve here as otherwise this wouldn't work in certain situations e.g. monorepos where the package is hoisted to a higher directory.

pluginOptions: {
plugins: [],
path: `src/pages`,
path: `${process.cwd()}/src/pages`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Use program.directory here + path.join

resolve: slash(`${process.cwd()}/node_modules/gatsby-plugin-page-creator`),
id: `Plugin gatsby-plugin-page-creator`,
name: `gatsby-plugin-page-creator`,
// version: `1.0.0`,
Copy link
Contributor

Choose a reason for hiding this comment

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

To get the version, just do require(path.join(require.resolve(gatsby-plugin-page-creator), package.json`)).version

version: pluginPageCreator.version,
pluginOptions: {
plugins: [],
path: `${process.cwd()}/src/pages`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of process.cwd use program.directory. Also use path.join to ensure cross-os compatability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do I get program.directory?

Copy link
Contributor

Choose a reason for hiding this comment

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

Check here -

const generateComponentChunkName = componentPath => {
const program = store.getState().program
let directory = `/`
if (program && program.directory) {
directory = program.directory
}

}

// Validate that the path exists.
if (!fs.existsSync(pluginOptions.path)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

One downside to this is that if someone doesn't want to have a src directory or src/pages directory then this will error. Perhaps the core instance of this plugin could say it is optional so if src/pages doesn't exist, then it just does nothing.

Copy link
Contributor Author

@nodox nodox May 4, 2018

Choose a reason for hiding this comment

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

How should this be implemented? I can't see I understand fully? @KyleAMathews @m-allanson

@nodox
Copy link
Contributor Author

nodox commented May 22, 2018

Stale?

pieh
pieh previously requested changes May 28, 2018
version: pluginPageCreator.version,
pluginOptions: {
plugins: [],
path: `${process.cwd()}/src/pages`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Check here -

const generateComponentChunkName = componentPath => {
const program = store.getState().program
let directory = `/`
if (program && program.directory) {
directory = program.directory
}

path.dirname(
require.resolve(`gatsby-plugin-page-creator`)), `package.json`
)
).version,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think instead of manually creating that you could reuse processPlugin function - something like that:

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

(check comment below for program.directory)

@pieh pieh self-assigned this Jun 7, 2018
@pieh pieh dismissed their stale review June 7, 2018 18:44

changes added in #87001d7

}

// Validate that the path exists.
if (pathCheck && !fs.existsSync(pagesPath)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

adding pathCheck and setting to false in default gatsby config is hacky way to address #4490 (review) :

One downside to this is that if someone doesn't want to have a src directory or src/pages directory then this will error. Perhaps the core instance of this plugin could say it is optional so if src/pages doesn't exist, then it just does nothing.

Is this fine?

Copy link
Contributor

Choose a reason for hiding this comment

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

this seems Ok to me? it could be given an awkward name to discourage people from using it - __dangerouslyIgnoreMissingPathsWithoutErroringAreYouSureYouWantToDoThis? (or similar)

Copy link
Contributor

Choose a reason for hiding this comment

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

This option is not documented in README, so they would have to dive into code to discover it and even then I don't consider it harmless if they use that option

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

`
"path" is a required option for gatsby-plugin-page-creator

See docs here - https://www.gatsbyjs.org/packages/gatsby-plugin-page-creator/
Copy link
Contributor

Choose a reason for hiding this comment

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

link should go to /plugins/ instead of /packages/

Copy link
Contributor

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

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

LGTM 👍 thanks @nodox and @pieh

@nodox
Copy link
Contributor Author

nodox commented Jun 8, 2018

@pieh thank you for moving this along. Thank you @m-allanson for keeping the issue from going stale! @calcsam @KyleAMathews ready for a merge?

@m-allanson m-allanson merged commit d43ae23 into gatsbyjs:master Jun 14, 2018
@Undistraction
Copy link
Contributor

Undistraction commented Jun 14, 2018

This is great. Thank you @nodox (and everyone else)

m-allanson added a commit that referenced this pull request Jun 14, 2018
* master: (26 commits)
  Publish
  [feat] Extract internal plugin for automatically creating pages so we can reuse for other directories (#4490)
  fix(contentful): properly delete deleted entries and assets (#5756)
  Fix typo
  add Linda's post (#5817)
  [gatsby-source-contentful] Fix prepareJSONNode for array normalization (#5107)
  Process Contentful images inlined in markdown (#5542)
  feat(contentful): add traced SVGs to Contentful images (#5659)
  [SQIP] Add documentation (#5287)
  Add site to showcase (#5819)
  [gatsby-remark-copy-linked-files] Support reference-style images (#5818)
  Update docs for gatsby-plugin-catch-links (#5843)
  Added ⋅ representing a space to sub-list examples to reflect actual code (#5829)
  Adding tigerfacilityservices.com to showcase (#5855)
  fix schema type conflict reporter regressions (#5805)
  [gatsby-plugin-feed] Support nested output directory (#5820)
  Make cache available to the plugins (#5840)
  [gatsby-source-contentful] update data and jest snapshots (#5802)
  [gatsby-plugin-manifest] Replace all manifest.json with manifest.webmanifest in comments/docs (#5157)
  Update deploy-gatsby.md (#5800)
  ...

# Conflicts:
#	docs/tutorial/part-one/index.md
#	examples/using-sqip/src/components/polaroid.js
#	packages/gatsby-image/README.md
#	packages/gatsby-image/package.json
#	packages/gatsby-plugin-catch-links/package.json
#	packages/gatsby-plugin-feed/package.json
#	packages/gatsby-plugin-manifest/package.json
#	packages/gatsby-plugin-page-creator/src/gatsby-node.js
#	packages/gatsby-remark-copy-linked-files/package.json
#	packages/gatsby-source-contentful/package.json
#	packages/gatsby-source-contentful/src/__tests__/__snapshots__/normalize.js.snap
#	packages/gatsby-source-contentful/src/extend-node-type.js
#	packages/gatsby-source-contentful/src/gatsby-node.js
#	packages/gatsby-source-contentful/src/normalize.js
#	packages/gatsby-transformer-remark/package.json
#	packages/gatsby-transformer-remark/src/__tests__/__snapshots__/gatsby-node.js.snap
#	packages/gatsby-transformer-sqip/package.json
#	packages/gatsby-transformer-sqip/src/extend-node-type.js
#	packages/gatsby/package.json
#	packages/gatsby/src/bootstrap/load-plugins/__tests__/__snapshots__/load-plugins.js.snap
#	packages/gatsby/src/bootstrap/load-plugins/load.js
#	www/src/data/sites.yml
@nodox nodox deleted the plugin-page-creator branch June 14, 2018 13:43
@nodox
Copy link
Contributor Author

nodox commented Jun 14, 2018

So happy this has been merged. I will write a blog post about the feature soon!

@avigoldman
Copy link
Contributor

This is wonderful! Thanks for tackling this @nodox @m-allanson @pieh 🎉

@bluetidepro
Copy link

This is awesome. Thanks so much for adding this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants