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
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/gatsby-plugin-page-creator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

decls
dist

/*.js
34 changes: 34 additions & 0 deletions packages/gatsby-plugin-page-creator/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
36 changes: 36 additions & 0 deletions packages/gatsby-plugin-page-creator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# gatsby-plugin-page-creator

Gatsby plugin that automatically creates pages from React components in specified directories. Gatsby
includes this plugin automatically in all sites for creating pages from components in `src/pages`.

## Install

`npm install --save gatsby-plugin-page-creator`

## How to use

```javascript
// gatsby-config.js

module.exports = {
plugins: [
// You can have multiple instances of this plugin
// to create pages from components in different directories.
//
// The following sets up the pattern of having multiple
// "pages" directories in your project
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: `${__dirname}/src/account/pages`,
},
},
{
resolve: `gatsby-plugin-page-creator`,
options: {
path: `${__dirname}/src/settings/pages`,
},
},
],
};
```
33 changes: 33 additions & 0 deletions packages/gatsby-plugin-page-creator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "gatsby-plugin-page-creator",
"version": "1.0.0",
"description": "Gatsby plugin that automatically creates pages from React components in specified directories",
"main": "index.js",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"watch": "babel -w src --out-dir . --ignore __tests__",
"prepublish": "cross-env NODE_ENV=production npm run build"
},
"keywords": [
"gatsby",
"gatsby-plugin"
],
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"contributors": [
"Steven Natera <tektekpush@gmail.com> (https://twitter.com/stevennatera)"
],
"license": "MIT",
"dependencies": {
"babel-runtime": "^6.26.0",
"bluebird": "^3.5.0",
"chokidar": "^1.7.0",
"glob": "^7.1.1",
"lodash": "^4.17.4",
"parse-filepath": "^1.0.1",
"slash": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"cross-env": "^5.0.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Promise = require(`bluebird`)
const _ = require(`lodash`)
const chokidar = require(`chokidar`)
const systemPath = require(`path`)
const fs = require(`fs`)

const glob = Promise.promisify(globCB)

Expand All @@ -15,14 +16,38 @@ const validatePath = require(`./validate-path`)
// underscored. Then create url w/ our path algorithm *unless* user
// takes control of that page component in gatsby-node.
exports.createPagesStatefully = async (
{ store, boundActionCreators },
options,
{ store, boundActionCreators, reporter },
{ path: pagesPath, pathCheck = true },
doneCb
) => {
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, `/src/pages`)

if (!pagesPath) {
reporter.panic(
`
"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/

`
)
}

// 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.

👍

reporter.panic(
`
The path passed to gatsby-plugin-page-creator does not exist on your file system:

${pagesPath}

Please pick a path to an existing directory.
`
)
}

const pagesDirectory = systemPath.posix.join(pagesPath)
const pagesGlob = `${pagesDirectory}/**/*.{${exts}}`

// Get initial list of files.
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"gatsby-cli": "^1.1.58",
"gatsby-link": "^1.6.44",
"gatsby-module-loader": "^1.0.11",
"gatsby-plugin-page-creator": "^1.0.0",
"gatsby-react-router-scroll": "^1.0.17",
"glob": "^7.1.1",
"graphql": "^0.11.7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ Array [
"ssrAPIs": Array [],
"version": "1.0.0",
},
Object {
"browserAPIs": Array [],
"id": "Plugin component-page-creator",
"name": "component-page-creator",
"nodeAPIs": Array [
"createPagesStatefully",
],
"pluginOptions": Object {
"plugins": Array [],
},
"resolve": "",
"ssrAPIs": Array [],
"version": "1.0.0",
},
Object {
"browserAPIs": Array [],
"id": "Plugin component-layout-creator",
Expand Down Expand Up @@ -100,29 +86,31 @@ Array [
"ssrAPIs": Array [],
"version": "d41d8cd98f00b204e9800998ecf8427e",
},
]
`;

exports[`Load plugins Loads plugins defined with an object but without an option key 1`] = `
Array [
Object {
"browserAPIs": Array [],
"id": "Plugin dev-404-page",
"name": "dev-404-page",
"id": "Plugin gatsby-plugin-page-creator",
"name": "gatsby-plugin-page-creator",
"nodeAPIs": Array [
"createPagesStatefully",
],
"pluginOptions": Object {
"path": "/src/pages",
"pathCheck": false,
"plugins": Array [],
},
"resolve": "",
"ssrAPIs": Array [],
"version": "1.0.0",
},
]
`;

exports[`Load plugins Loads plugins defined with an object but without an option key 1`] = `
Array [
Object {
"browserAPIs": Array [],
"id": "Plugin component-page-creator",
"name": "component-page-creator",
"id": "Plugin dev-404-page",
"name": "dev-404-page",
"nodeAPIs": Array [
"createPagesStatefully",
],
Expand Down Expand Up @@ -213,5 +201,21 @@ Array [
"ssrAPIs": Array [],
"version": "d41d8cd98f00b204e9800998ecf8427e",
},
Object {
"browserAPIs": Array [],
"id": "Plugin gatsby-plugin-page-creator",
"name": "gatsby-plugin-page-creator",
"nodeAPIs": Array [
"createPagesStatefully",
],
"pluginOptions": Object {
"path": "/src/pages",
"pathCheck": false,
"plugins": Array [],
},
"resolve": "",
"ssrAPIs": Array [],
"version": "1.0.0",
},
]
`;
11 changes: 10 additions & 1 deletion packages/gatsby/src/bootstrap/load-plugins/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require(`fs`)
const path = require(`path`)
const crypto = require(`crypto`)
const glob = require(`glob`)
const { store } = require(`../../redux`)

function createFileContentHash(root, globPattern) {
const hash = crypto.createHash(`md5`)
Expand Down Expand Up @@ -130,7 +131,6 @@ module.exports = async (config = {}) => {
// Add internal plugins
const internalPlugins = [
`../../internal-plugins/dev-404-page`,
`../../internal-plugins/component-page-creator`,
`../../internal-plugins/component-layout-creator`,
`../../internal-plugins/internal-data-bridge`,
`../../internal-plugins/prod-404`,
Expand Down Expand Up @@ -159,5 +159,14 @@ module.exports = async (config = {}) => {
},
})

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

return plugins
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ emitter.on(`CREATE_PAGE`, () => {
// we can ignore them until CREATE_PAGE_END is called.
//
// After bootstrap, we need to listen for this as stateful page
// creators e.g. the internal plugin "component-page-creator"
// creators e.g. the plugin "gatsby-plugin-page-creator"
// calls createPage directly so CREATE_PAGE_END won't get fired.
if (bootstrapFinished) {
debouncedWritePages()
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/api-node-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ exports.createPages = true
* page information as Gatsby's data changes but those implementing
* `createPagesStatefully` will not.
*
* An example of a plugin that uses this extension point is the internal plugin
* [component-page-creator](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby/src/internal-plugins/component-page-creator)
* An example of a plugin that uses this extension point is the plugin
* [gatsby-plugin-page-creator](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-page-creator)
* which monitors the `src/pages` directory for the adding and removal of JS
* pages. As its source of truth, files in the pages directory, is not known by
* Gatsby, it needs to keep its own state about its world to know when to
Expand Down