-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Conversation
Deploy preview for gatsbygram ready! Built with commit 778e427 |
Deploy preview for using-drupal ready! Built with commit 32edb33 |
There was a problem hiding this 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`, // ??? |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { // ??? |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>", |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
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? |
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. |
7b62c8b
to
381e678
Compare
6b80f96
to
60e7de1
Compare
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.
60e7de1
to
3d1b166
Compare
@KyleAMathews PR is all cleaned up. Produced two blog posts in this work: |
There was a problem hiding this 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" |
There was a problem hiding this comment.
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`) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Getting close! |
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. |
That looks correct! What happens when you test it? |
a54ec72
to
e116257
Compare
Everything builds fine but the |
How about we break this up into two smaller PRs?
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! |
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 |
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.
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. |
Deploy preview for using-glamor failed. Built with commit 601c8f1 https://app.netlify.com/sites/using-glamor/deploys/5acd82213813f049f847a458 |
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`), |
There was a problem hiding this comment.
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`, |
There was a problem hiding this comment.
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`, |
There was a problem hiding this comment.
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`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check here -
gatsby/packages/gatsby/src/utils/js-chunk-names.js
Lines 10 to 15 in a38f49e
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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Stale? |
version: pluginPageCreator.version, | ||
pluginOptions: { | ||
plugins: [], | ||
path: `${process.cwd()}/src/pages`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check here -
gatsby/packages/gatsby/src/utils/js-chunk-names.js
Lines 10 to 15 in a38f49e
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, |
There was a problem hiding this comment.
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)
…isting functions
} | ||
|
||
// Validate that the path exists. | ||
if (pathCheck && !fs.existsSync(pagesPath)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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/ |
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pieh thank you for moving this along. Thank you @m-allanson for keeping the issue from going stale! @calcsam @KyleAMathews ready for a merge? |
This is great. Thank you @nodox (and everyone else) |
* 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
So happy this has been merged. I will write a blog post about the feature soon! |
This is wonderful! Thanks for tackling this @nodox @m-allanson @pieh 🎉 |
This is awesome. Thanks so much for adding this! |
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