diff --git a/.babelrc b/.babelrc deleted file mode 100644 index aaa36072..00000000 --- a/.babelrc +++ /dev/null @@ -1,6 +0,0 @@ -{ - "presets": [ - ["babel-preset-gatsby-package", { "browser": true }] - ] -} - diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index 825d82ab..00000000 --- a/jest.config.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -module.exports = { - roots: [ - '/test/', - '/src/' - ], - collectCoverageFrom: [ - 'src/**/*.js' - ] -} diff --git a/package.json b/package.json index 36fb9794..2073fc98 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,9 @@ "scripts": { "prepare": "cross-env NODE_ENV=production npm run build", "prebuild": "rimraf 'dist/*'", - "build": "babel src --out-dir dist --copy-files --ignore '**/__tests__,**/*.test.js'", + "build": "babel src --out-dir dist --copy-files --ignore '**/__tests__,**/*.test.js,**/*.fragment.js'", "postbuild": "rimraf 'dist/**/__tests__' 'dist/**/*.test.js'", - "watch": "babel -w src --out-dir dist --copy-files --ignore '**/__tests__,**/*.test.js'", + "watch": "babel -w src --out-dir dist --copy-files --ignore '**/__tests__,**/*.test.js,**/*.fragment.js'", "test": "jest --runInBand", "test:coverage": "jest --runInBand --coverage", "test:watch": "jest --watch --runInBand", @@ -74,6 +74,18 @@ "engines": { "node": ">=10.13.0" }, + "jest": { + "roots": [ + "/test/", + "/src/" + ], + "testPathIgnorePatterns": [ + "/__tests__/__fixtures__/.*$" + ], + "collectCoverageFrom": [ + "src/**/*.js" + ] + }, "standard": { "parser": "@babel/eslint-parser", "env": [ diff --git a/src/.babelrc b/src/.babelrc new file mode 100644 index 00000000..cd8d057e --- /dev/null +++ b/src/.babelrc @@ -0,0 +1,7 @@ +{ + "presets": [ + ["babel-preset-gatsby-package", { + "browser": true + }] + ] +} diff --git a/test/__fixtures__/routes.js b/src/__tests__/__fixtures__/routes.js similarity index 100% rename from test/__fixtures__/routes.js rename to src/__tests__/__fixtures__/routes.js diff --git a/src/__tests__/api.js b/src/__tests__/api.js index f48e65de..d832d492 100644 --- a/src/__tests__/api.js +++ b/src/__tests__/api.js @@ -1,5 +1,5 @@ import { withPrefix } from 'gatsby' -import routes from '../../test/__fixtures__/routes' +import routes from './__fixtures__/routes' import { getRoutes, routeExists, @@ -10,8 +10,11 @@ import { generatePath } from '../api' -// Create a virtual mock for routes.js -jest.doMock('../routes', () => routes, { virtual: true }) +// Create a virtual mock for routes.json +jest.doMock( + 'gatsby-plugin-advanced-pages-cache/routes.json', + () => routes, { virtual: true } +) jest.doMock('@reach/router', () => ({ globalHistory: { diff --git a/src/api.js b/src/api.js index ce0e441d..4f073898 100644 --- a/src/api.js +++ b/src/api.js @@ -3,7 +3,7 @@ import { pick, compile } from './lib/route-compiler' // Gets an array of all routes export function getRoutes () { - return require('./routes') + return require('gatsby-plugin-advanced-pages-cache/routes.json') } // Gets a specific route diff --git a/src/components/Link/__tests__/Link.js b/src/components/Link/__tests__/Link.js index 9557809a..cff7f9ae 100644 --- a/src/components/Link/__tests__/Link.js +++ b/src/components/Link/__tests__/Link.js @@ -1,10 +1,13 @@ import React from 'react' import TestRenderer from 'react-test-renderer' import Link from '../Link' -import routes from '../../../../test/__fixtures__/routes' +import routes from '../../../__tests__/__fixtures__/routes' -// Create a virtual mock for routes.js -jest.doMock('../../../routes', () => routes, { virtual: true }) +// Create a virtual mock for routes.json +jest.doMock( + 'gatsby-plugin-advanced-pages-cache/routes.json', + () => routes, { virtual: true } +) describe('', () => { const render = (props = {}) => { diff --git a/src/components/Pagination/Pagination.fragment b/src/components/Pagination/Pagination.fragment.js similarity index 66% rename from src/components/Pagination/Pagination.fragment rename to src/components/Pagination/Pagination.fragment.js index 54cf6b11..767d3734 100644 --- a/src/components/Pagination/Pagination.fragment +++ b/src/components/Pagination/Pagination.fragment.js @@ -1,6 +1,6 @@ -import { graphql } from "gatsby" +import { graphql } from 'gatsby' -export const pagination = graphql` +export const Pagination = graphql` fragment Pagination on PageInfo { perPage pageCount diff --git a/src/components/Pagination/__tests__/Pagination.js b/src/components/Pagination/__tests__/Pagination.js index d70874c1..d79b35f5 100644 --- a/src/components/Pagination/__tests__/Pagination.js +++ b/src/components/Pagination/__tests__/Pagination.js @@ -2,7 +2,7 @@ import React from 'react' import ShallowRenderer from 'react-test-renderer/shallow' import { mapValues, merge } from 'lodash' import Pagination from '../Pagination' -import testCases, { defaultTestCase } from '../../../../test/__fixtures__/pagination' +import testCases, { defaultTestCase } from './__fixtures__/pagination' describe('', () => { const render = (props = {}) => { diff --git a/test/__fixtures__/pagination.js b/src/components/Pagination/__tests__/__fixtures__/pagination.js similarity index 100% rename from test/__fixtures__/pagination.js rename to src/components/Pagination/__tests__/__fixtures__/pagination.js diff --git a/src/gatsby/.babelrc b/src/gatsby/.babelrc index eaeab35b..24a26e23 100644 --- a/src/gatsby/.babelrc +++ b/src/gatsby/.babelrc @@ -1,6 +1,8 @@ { "presets": [ - ["babel-preset-gatsby-package", { "nodeVersion": "8" }] + ["babel-preset-gatsby-package", { + "browser": false, + "nodeVersion": "10.13.0" + }] ] } - diff --git a/test/__fixtures__/create-pages.js b/src/gatsby/__tests__/__fixtures__/create-pages.js similarity index 87% rename from test/__fixtures__/create-pages.js rename to src/gatsby/__tests__/__fixtures__/create-pages.js index 7694d608..1676092c 100644 --- a/test/__fixtures__/create-pages.js +++ b/src/gatsby/__tests__/__fixtures__/create-pages.js @@ -69,9 +69,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { - throw new Error('some error') - } + helper: () => { throw new Error('some error') } }, { id: 'bad-helper-empty-call', @@ -85,9 +83,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { - createAdvancedPage() - } + helper: ({ createAdvancedPage }) => createAdvancedPage() }, { id: 'bad-helper-undefined-route', @@ -101,11 +97,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { - createAdvancedPage({ - route: 'blog' - }) - } + helper: ({ createAdvancedPage }) => createAdvancedPage({ route: 'blog' }) }, { id: 'bad-helper-invalid-route-params', @@ -119,11 +111,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { - createAdvancedPage({ - route: 'about' - }) - } + helper: ({ createAdvancedPage }) => createAdvancedPage({ route: 'about' }) }, { id: 'bad-helper-empty-pagination', @@ -137,7 +125,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'about', pagination: {} @@ -156,7 +144,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'about', pagination: { @@ -177,7 +165,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'about', pagination: { @@ -199,7 +187,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'about', pagination: { @@ -221,7 +209,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'blog', pagination: { @@ -243,7 +231,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'blog', pagination: { @@ -265,7 +253,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'blog', pagination: { @@ -287,7 +275,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { for (const slug of ['hello', 'world']) { createAdvancedPage({ route: 'page', @@ -310,7 +298,7 @@ export default [ ] } ], - helper: function ({ createAdvancedPage }) { + helper: ({ createAdvancedPage }) => { createAdvancedPage({ route: 'about', params: { diff --git a/test/__fixtures__/pagesSchema.js b/src/gatsby/__tests__/__fixtures__/pagesSchema.js similarity index 100% rename from test/__fixtures__/pagesSchema.js rename to src/gatsby/__tests__/__fixtures__/pagesSchema.js diff --git a/test/__fixtures__/pluginOptionsSchema.js b/src/gatsby/__tests__/__fixtures__/pluginOptionsSchema.js similarity index 100% rename from test/__fixtures__/pluginOptionsSchema.js rename to src/gatsby/__tests__/__fixtures__/pluginOptionsSchema.js diff --git a/test/__fixtures__/source-nodes.js b/src/gatsby/__tests__/__fixtures__/source-nodes.js similarity index 100% rename from test/__fixtures__/source-nodes.js rename to src/gatsby/__tests__/__fixtures__/source-nodes.js diff --git a/src/gatsby/__tests__/__snapshots__/create-pages.js.snap b/src/gatsby/__tests__/__snapshots__/create-pages.js.snap index ada30d11..6f56ae59 100644 --- a/src/gatsby/__tests__/__snapshots__/create-pages.js.snap +++ b/src/gatsby/__tests__/__snapshots__/create-pages.js.snap @@ -38,19 +38,7 @@ Array [ ] `; -exports[`createPages correctly creates pages on (blog-paginated-auto-route) 2`] = ` -"\\"use strict\\"; - -module.exports = [ - { - \\"name\\": \\"blog\\", - \\"path\\": \\"/blog\\", - \\"scopes\\": { - \\"pagination\\": \\"/blog/page/:page\\" - } - } -];" -`; +exports[`createPages correctly creates pages on (blog-paginated-auto-route) 2`] = `"[{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"scopes\\":{\\"pagination\\":\\"/blog/page/:page\\"}}]"`; exports[`createPages correctly creates pages on (blog-paginated-custom-limit) 1`] = ` Array [ @@ -101,19 +89,7 @@ Array [ ] `; -exports[`createPages correctly creates pages on (blog-paginated-custom-limit) 2`] = ` -"\\"use strict\\"; - -module.exports = [ - { - \\"name\\": \\"blog\\", - \\"path\\": \\"/blog\\", - \\"scopes\\": { - \\"pagination\\": \\"/blog/page/:page\\" - } - } -];" -`; +exports[`createPages correctly creates pages on (blog-paginated-custom-limit) 2`] = `"[{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"scopes\\":{\\"pagination\\":\\"/blog/page/:page\\"}}]"`; exports[`createPages correctly creates pages on (blog-paginated-manual-route) 1`] = ` Array [ @@ -153,24 +129,7 @@ Array [ ] `; -exports[`createPages correctly creates pages on (blog-paginated-manual-route) 2`] = ` -"\\"use strict\\"; - -module.exports = [ - { - \\"name\\": \\"blog\\", - \\"path\\": \\"/blog\\", - \\"scopes\\": { - \\"pagination\\": \\"/blog/what/:page\\" - } - }, - { - \\"name\\": \\"blog.paginated\\", - \\"path\\": \\"/blog/what/:page\\", - \\"scopes\\": {} - } -];" -`; +exports[`createPages correctly creates pages on (blog-paginated-manual-route) 2`] = `"[{\\"name\\":\\"blog\\",\\"path\\":\\"/blog\\",\\"scopes\\":{\\"pagination\\":\\"/blog/what/:page\\"}},{\\"name\\":\\"blog.paginated\\",\\"path\\":\\"/blog/what/:page\\",\\"scopes\\":{}}]"`; exports[`createPages correctly creates pages on (dynamic-page) 1`] = ` Array [ @@ -197,17 +156,7 @@ Array [ ] `; -exports[`createPages correctly creates pages on (dynamic-page) 2`] = ` -"\\"use strict\\"; - -module.exports = [ - { - \\"name\\": \\"page\\", - \\"path\\": \\"/pages/:page\\", - \\"scopes\\": {} - } -];" -`; +exports[`createPages correctly creates pages on (dynamic-page) 2`] = `"[{\\"name\\":\\"page\\",\\"path\\":\\"/pages/:page\\",\\"scopes\\":{}}]"`; exports[`createPages correctly creates pages on (home-about) 1`] = ` Array [ @@ -241,27 +190,7 @@ Array [ ] `; -exports[`createPages correctly creates pages on (home-about) 2`] = ` -"\\"use strict\\"; - -module.exports = [ - { - \\"name\\": \\"home\\", - \\"path\\": \\"/\\", - \\"scopes\\": {} - }, - { - \\"name\\": \\"about\\", - \\"path\\": \\"/about\\", - \\"scopes\\": {} - }, - { - \\"name\\": \\"aboutme\\", - \\"path\\": \\"/aboutme\\", - \\"scopes\\": {} - } -];" -`; +exports[`createPages correctly creates pages on (home-about) 2`] = `"[{\\"name\\":\\"home\\",\\"path\\":\\"/\\",\\"scopes\\":{}},{\\"name\\":\\"about\\",\\"path\\":\\"/about\\",\\"scopes\\":{}},{\\"name\\":\\"aboutme\\",\\"path\\":\\"/aboutme\\",\\"scopes\\":{}}]"`; exports[`createPages correctly creates pages on (page-context) 1`] = ` Array [ @@ -296,17 +225,7 @@ Array [ ] `; -exports[`createPages correctly creates pages on (page-context) 2`] = ` -"\\"use strict\\"; - -module.exports = [ - { - \\"name\\": \\"about\\", - \\"path\\": \\"/about/:name\\", - \\"scopes\\": {} - } -];" -`; +exports[`createPages correctly creates pages on (page-context) 2`] = `"[{\\"name\\":\\"about\\",\\"path\\":\\"/about/:name\\",\\"scopes\\":{}}]"`; exports[`createPages throws error on (bad-helper-empty-call) 1`] = `[Error: Route name passed to createAdvancedPage() at "/path/to/helper.js" must be a non-empty string]`; diff --git a/src/gatsby/__tests__/bootstrap.js b/src/gatsby/__tests__/bootstrap.js index 2ef8aae1..518e88cb 100644 --- a/src/gatsby/__tests__/bootstrap.js +++ b/src/gatsby/__tests__/bootstrap.js @@ -1,5 +1,5 @@ import { getOptions } from '../util' -import { mountOptions, mountFile } from '../../../test/helpers' +import { mountOptions, mountFile } from '../../../test/node-utils' // Use in-memory file system jest.mock('fs') diff --git a/src/gatsby/__tests__/create-pages.js b/src/gatsby/__tests__/create-pages.js index f2c46d9a..09521e6a 100644 --- a/src/gatsby/__tests__/create-pages.js +++ b/src/gatsby/__tests__/create-pages.js @@ -1,8 +1,8 @@ import fs from 'fs' import path from 'path' import { createPages } from '../' -import { mountOptions, mountFile, mountDir, mountModule } from '../../../test/helpers' -import testCases from '../../../test/__fixtures__/create-pages' +import { mountOptions, mountFile, mountDir, mountModule } from '../../../test/node-utils' +import testCases from './__fixtures__/create-pages' // Use in-memory file system jest.mock('fs') @@ -10,6 +10,7 @@ jest.mock('fs') describe('createPages', () => { const graphql = jest.fn() const createPage = jest.fn() + const cache = { directory: '/path/to/.cache' } const actions = { createPage } const getNodesByType = jest.fn().mockImplementation(() => 10) const helperFile = '/path/to/helper.js' @@ -34,9 +35,9 @@ describe('createPages', () => { mountModule(helperFile, helper) } - // Create a virtual directory for the 'src' folder - // so that routes.js file can be written virtually - mountDir(path.resolve(__dirname, '../../')) + // Create a virtual directory for cache.directory + // so that routes.json file can be written virtually + mountDir(cache.directory) graphql.mockReturnValue({ data: { @@ -49,6 +50,7 @@ describe('createPages', () => { await createPages({ graphql, actions, + cache, getNodesByType }) } catch (e) { @@ -64,7 +66,7 @@ describe('createPages', () => { expect(error).toBeNull() expect(createPage.mock.calls).toMatchSnapshot() expect( - fs.readFileSync(path.resolve(__dirname, '../../routes.js'), 'utf8') + fs.readFileSync(path.join(cache.directory, 'routes.json'), 'utf8') ).toMatchSnapshot() }) } diff --git a/src/gatsby/__tests__/pagesSchema.js b/src/gatsby/__tests__/pagesSchema.js index 2109cd91..c2881de0 100644 --- a/src/gatsby/__tests__/pagesSchema.js +++ b/src/gatsby/__tests__/pagesSchema.js @@ -1,6 +1,6 @@ import { testPluginOptionsSchema } from 'gatsby-plugin-utils' import { pagesSchema } from '../schema' -import testCases from '../../../test/__fixtures__/pagesSchema' +import testCases from './__fixtures__/pagesSchema' describe('pluginOptionsSchema', () => { for (const { title, pages } of testCases) { diff --git a/src/gatsby/__tests__/pluginOptionsSchema.js b/src/gatsby/__tests__/pluginOptionsSchema.js index 6b5518ea..a4a643bc 100644 --- a/src/gatsby/__tests__/pluginOptionsSchema.js +++ b/src/gatsby/__tests__/pluginOptionsSchema.js @@ -1,6 +1,6 @@ import { testPluginOptionsSchema } from 'gatsby-plugin-utils' import { pluginOptionsSchema } from '../' -import testCases from '../../../test/__fixtures__/pluginOptionsSchema' +import testCases from './__fixtures__/pluginOptionsSchema' describe('pluginOptionsSchema', () => { for (const { title, options } of testCases) { diff --git a/src/gatsby/__tests__/source-nodes.js b/src/gatsby/__tests__/source-nodes.js index fa8f1f18..40438402 100644 --- a/src/gatsby/__tests__/source-nodes.js +++ b/src/gatsby/__tests__/source-nodes.js @@ -1,7 +1,7 @@ import fs from 'fs' import { sourceNodes } from '../' -import { mountFile, mountModule, mountOptions } from '../../../test/helpers' -import testCases from '../../../test/__fixtures__/source-nodes' +import { mountFile, mountModule, mountOptions } from '../../../test/node-utils' +import testCases from './__fixtures__/source-nodes' // Use in-memory file system jest.mock('fs') diff --git a/src/gatsby/bootstrap.js b/src/gatsby/bootstrap.js deleted file mode 100644 index 39fdc39b..00000000 --- a/src/gatsby/bootstrap.js +++ /dev/null @@ -1,42 +0,0 @@ -import { initializeOptions, initializeReporter } from './util' -import { optionsSchema } from './schema' - -// Validates user-defined options against schema -// runs before onPreBootstrap -export function pluginOptionsSchema ({ Joi }) { - return optionsSchema(Joi) -} - -// Initializes plugin reporter -export function onPreInit ({ reporter }) { - initializeReporter(reporter) -} - -// Initializes plugin options -export function onPreBootstrap ({ store, reporter }, pluginOptions) { - // Default values for options - const defaultOptions = { - basePath: '/', - pages: [], - template: null, - directories: { - templates: './src/templates', - helpers: './gatsby/pages' - }, - pagination: { - limit: 10, - suffix: '/page/:page' - }, - typeNames: { - page: 'Page' - } - } - - // Initializes and validates options - // Only runs once at bootstrap - initializeOptions({ - store, - defaultOptions, - pluginOptions - }) -} diff --git a/src/gatsby/create-pages.js b/src/gatsby/create-pages.js index b54c16b1..3ee5673c 100644 --- a/src/gatsby/create-pages.js +++ b/src/gatsby/create-pages.js @@ -1,8 +1,9 @@ +import fs from 'fs' import path from 'path' import PagesCreator from './lib/pages-creator' import { getOption, reportError } from './util' -export default async function ({ graphql, actions, getNodesByType }) { +export default async function ({ graphql, actions, getNodesByType, cache }) { const { createPage } = actions const pageType = getOption('typeNames.page') @@ -32,13 +33,16 @@ export default async function ({ graphql, actions, getNodesByType }) { return reportError('Failed running "create-pages" GraphQL Query', result.errors.shift()) } - const pageCreator = new PagesCreator( - result.data[`all${pageType}`].nodes - ) - // Create the actual pages + const pageCreator = new PagesCreator(result.data[`all${pageType}`].nodes) await pageCreator.createPages({ graphql, createPage }) // Write routes export file - pageCreator.writeRoutesExport(path.resolve(__dirname, '../routes.js')) + const routesFile = path.join(cache.directory, 'routes.json') + try { + fs.writeFileSync(routesFile, JSON.stringify(pageCreator.getRoutesExport())) + return true + } catch (e) { + reportError('Error writing route map export file', e) + } } diff --git a/src/gatsby/index.js b/src/gatsby/index.js index 9bdae65c..24a17ea4 100644 --- a/src/gatsby/index.js +++ b/src/gatsby/index.js @@ -1,4 +1,57 @@ -export { onPreInit, onPreBootstrap, pluginOptionsSchema } from './bootstrap' +import { initializeOptions, initializeReporter } from './util' +import { optionsSchema } from './schema' + +// Validates user-defined options against schema +// runs before onPreBootstrap +export function pluginOptionsSchema ({ Joi }) { + return optionsSchema(Joi) +} + +// Initializes plugin reporter +export function onPreInit ({ reporter }) { + initializeReporter(reporter) +} + +// Initializes plugin options +export function onPreBootstrap ({ store, reporter }, pluginOptions) { + // Default values for options + const defaultOptions = { + basePath: '/', + pages: [], + template: null, + directories: { + templates: './src/templates', + helpers: './gatsby/pages' + }, + pagination: { + limit: 10, + suffix: '/page/:page' + }, + typeNames: { + page: 'Page' + } + } + + // Initializes and validates options + // Only runs once at bootstrap + initializeOptions({ + store, + defaultOptions, + pluginOptions + }) +} + +// Creatas a webpack alias for the plugin cache directory +// Usually located at /.cache/caches/ +export function onCreateWebpackConfig ({ actions, cache }) { + actions.setWebpackConfig({ + resolve: { + alias: { + 'gatsby-plugin-advanced-pages-cache': cache.directory + } + } + }) +} + export { default as sourceNodes } from './source-nodes' export { default as createPages } from './create-pages' -export { default as onPreExtractQueries } from './install-fragments' diff --git a/src/gatsby/install-fragments.js b/src/gatsby/install-fragments.js deleted file mode 100644 index b1c4572d..00000000 --- a/src/gatsby/install-fragments.js +++ /dev/null @@ -1,15 +0,0 @@ -import fs from 'fs' -import path from 'path' -import { reportError } from './util' - -export default async function ({ store }) { - const { program } = store.getState() - // Add pagination fragment to .cache/fragments. - await fs.copyFile( - path.resolve(__dirname, '../components/Pagination/Pagination.fragment'), - path.resolve(program.directory, '.cache/fragments/pagination-fragment.js'), - err => { - if (err) reportError('Failed copying pagination fragment', err) - } - ) -} diff --git a/src/gatsby/lib/pages-creator.js b/src/gatsby/lib/pages-creator.js index 1c542a71..9058e7a0 100644 --- a/src/gatsby/lib/pages-creator.js +++ b/src/gatsby/lib/pages-creator.js @@ -1,4 +1,3 @@ -import fs from 'fs' import path from 'path' import _ from 'lodash' import { compile as compileRoute } from '../../lib/route-compiler' @@ -173,21 +172,12 @@ export default class PagesCreator { this.createPageAction(gatsbyPage) } - writeRoutesExport (filename) { - const routes = _.map(this.routeMap, route => { + // Creates a serializable form of the route map + getRoutesExport () { + return _.map(this.routeMap, route => { route = _.pick(route, ['name', 'path', 'scopes']) route.scopes = _.mapValues(route.scopes, 'path') return route }) - - try { - fs.writeFileSync( - filename, - `"use strict";\n\nmodule.exports = ${JSON.stringify(routes, null, 2)};` - ) - return true - } catch (e) { - reportError('Error writing route map export file', e) - } } } diff --git a/test/.babelrc b/test/.babelrc new file mode 100644 index 00000000..24a26e23 --- /dev/null +++ b/test/.babelrc @@ -0,0 +1,8 @@ +{ + "presets": [ + ["babel-preset-gatsby-package", { + "browser": false, + "nodeVersion": "10.13.0" + }] + ] +} diff --git a/test/__mocks__/fs.js b/test/__mocks__/fs.js index 1a953fb9..016d839e 100644 --- a/test/__mocks__/fs.js +++ b/test/__mocks__/fs.js @@ -1,5 +1,5 @@ -'use strict' - -module.exports = new (require('metro-memory-fs'))({ +import MemoryFS from 'metro-memory-fs' +// Make sure we export this as a cjs module +module.exports = new MemoryFS({ cwd: () => '/virtual/project' }) diff --git a/test/__mocks__/gatsby.js b/test/__mocks__/gatsby.js index 5098b518..dd72855c 100644 --- a/test/__mocks__/gatsby.js +++ b/test/__mocks__/gatsby.js @@ -1,9 +1,7 @@ -'use strict' - -const path = require('path') -const React = require('react') +import path from 'path' +import React from 'react' const withPrefix = pathname => path.join('/site', pathname) - +// Make sure we export this as a cjs module module.exports = { graphql: jest.fn(), withPrefix: jest.fn().mockImplementation(withPrefix), diff --git a/test/helpers.js b/test/node-utils.js similarity index 100% rename from test/helpers.js rename to test/node-utils.js