diff --git a/packages/gatsby-plugin-page-creator/.gitignore b/packages/gatsby-plugin-page-creator/.gitignore new file mode 100644 index 0000000000000..8f49cac92c0ac --- /dev/null +++ b/packages/gatsby-plugin-page-creator/.gitignore @@ -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 diff --git a/packages/gatsby-plugin-page-creator/.npmignore b/packages/gatsby-plugin-page-creator/.npmignore new file mode 100644 index 0000000000000..e771d2c9fa299 --- /dev/null +++ b/packages/gatsby-plugin-page-creator/.npmignore @@ -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 diff --git a/packages/gatsby-plugin-page-creator/README.md b/packages/gatsby-plugin-page-creator/README.md new file mode 100644 index 0000000000000..c5cc223761c21 --- /dev/null +++ b/packages/gatsby-plugin-page-creator/README.md @@ -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 React 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`, + }, + }, + ], +}; +``` diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/index.js b/packages/gatsby-plugin-page-creator/index.js similarity index 100% rename from packages/gatsby/src/internal-plugins/component-page-creator/index.js rename to packages/gatsby-plugin-page-creator/index.js diff --git a/packages/gatsby-plugin-page-creator/package.json b/packages/gatsby-plugin-page-creator/package.json new file mode 100644 index 0000000000000..d64d61fdc265f --- /dev/null +++ b/packages/gatsby-plugin-page-creator/package.json @@ -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 ", + "contributors": [ + "Steven Natera (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" + } +} diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-plugin-page-creator/src/__tests__/__snapshots__/gatsby-node.js.snap similarity index 100% rename from packages/gatsby/src/internal-plugins/component-page-creator/__tests__/__snapshots__/gatsby-node.js.snap rename to packages/gatsby-plugin-page-creator/src/__tests__/__snapshots__/gatsby-node.js.snap diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js b/packages/gatsby-plugin-page-creator/src/__tests__/gatsby-node.js similarity index 100% rename from packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js rename to packages/gatsby-plugin-page-creator/src/__tests__/gatsby-node.js diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js b/packages/gatsby-plugin-page-creator/src/create-path.js similarity index 100% rename from packages/gatsby/src/internal-plugins/component-page-creator/create-path.js rename to packages/gatsby-plugin-page-creator/src/create-path.js diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/gatsby-node.js b/packages/gatsby-plugin-page-creator/src/gatsby-node.js similarity index 74% rename from packages/gatsby/src/internal-plugins/component-page-creator/gatsby-node.js rename to packages/gatsby-plugin-page-creator/src/gatsby-node.js index 9517a2a8de508..6fb663df1112e 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/gatsby-node.js +++ b/packages/gatsby-plugin-page-creator/src/gatsby-node.js @@ -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) @@ -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/plugins/gatsby-plugin-page-creator/ + ` + ) + } + + // Validate that the path exists. + if (pathCheck && !fs.existsSync(pagesPath)) { + 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. diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js b/packages/gatsby-plugin-page-creator/src/validate-path.js similarity index 100% rename from packages/gatsby/src/internal-plugins/component-page-creator/validate-path.js rename to packages/gatsby-plugin-page-creator/src/validate-path.js diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index 9c3fda9c1de78..f829edb1304ef 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -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", diff --git a/packages/gatsby/src/bootstrap/load-plugins/__tests__/__snapshots__/load-plugins.js.snap b/packages/gatsby/src/bootstrap/load-plugins/__tests__/__snapshots__/load-plugins.js.snap index 7148fa293ff8e..c5a61ebe0e3fe 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/__tests__/__snapshots__/load-plugins.js.snap +++ b/packages/gatsby/src/bootstrap/load-plugins/__tests__/__snapshots__/load-plugins.js.snap @@ -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", @@ -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", ], @@ -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", + }, ] `; diff --git a/packages/gatsby/src/bootstrap/load-plugins/load.js b/packages/gatsby/src/bootstrap/load-plugins/load.js index ae8c2b4619ac1..5ecb976f2e1b2 100644 --- a/packages/gatsby/src/bootstrap/load-plugins/load.js +++ b/packages/gatsby/src/bootstrap/load-plugins/load.js @@ -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`) @@ -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`, @@ -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 } diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/package.json b/packages/gatsby/src/internal-plugins/component-page-creator/package.json deleted file mode 100644 index c30586a276119..0000000000000 --- a/packages/gatsby/src/internal-plugins/component-page-creator/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "component-page-creator", - "version": "1.0.0", - "description": "An internal Gatsby plugin that creates pages from component files in src/pages", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": ["gatsby", "gatsby-plugin"], - "author": "Kyle Mathews ", - "license": "MIT" -} diff --git a/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js b/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js index 2cee771048961..1fcf1c20f90f6 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js +++ b/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js @@ -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() diff --git a/packages/gatsby/src/utils/api-node-docs.js b/packages/gatsby/src/utils/api-node-docs.js index ec5c7174e643f..09fb55f360b8e 100644 --- a/packages/gatsby/src/utils/api-node-docs.js +++ b/packages/gatsby/src/utils/api-node-docs.js @@ -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