Skip to content

Commit

Permalink
feat(gatsby): enable gatsby-plugin-gatsby-cloud by default (gatsbyjs#…
Browse files Browse the repository at this point in the history
…30624)

* feat(gatsby): enable gatsby-plugin-gatsby-cloud by default when available

* replace another constant

* add tests
  • Loading branch information
wardpeet authored Apr 30, 2021
1 parent 1527e47 commit 4259940
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 60 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"normalize-path": "^3.0.0",
"null-loader": "^4.0.1",
"opentracing": "^0.14.4",
"resolve-from": "^5.0.0",
"p-defer": "^3.0.0",
"parseurl": "^1.3.3",
"path-to-regexp": "0.1.7",
Expand Down Expand Up @@ -258,4 +259,4 @@
"yargs": {
"boolean-negation": false
}
}
}
219 changes: 169 additions & 50 deletions packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { loadPlugins } from "../index"
import { slash } from "gatsby-core-utils"
import reporter from "gatsby-cli/lib/reporter"
import { IFlattenedPlugin } from "../types"
import { silent as resolveFrom } from "resolve-from"

jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
error: jest.fn(),
Expand All @@ -8,16 +14,15 @@ jest.mock(`gatsby-cli/lib/reporter`, () => {
info: jest.fn(),
}
})

jest.mock(`resolve-from`)
const mockProcessExit = jest.spyOn(process, `exit`).mockImplementation(() => {})
import { loadPlugins } from "../index"
import { slash } from "gatsby-core-utils"
import reporter from "gatsby-cli/lib/reporter"
import { IFlattenedPlugin } from "../types"

afterEach(() => {
Object.keys(reporter).forEach(method => {
reporter[method].mockClear()
})
resolveFrom.mockClear()
mockProcessExit.mockClear()
})

Expand Down Expand Up @@ -51,7 +56,7 @@ describe(`Load plugins`, () => {
})

it(`Load plugins for a site`, async () => {
let plugins = await loadPlugins({ plugins: [] })
let plugins = await loadPlugins({ plugins: [] }, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

Expand All @@ -67,7 +72,7 @@ describe(`Load plugins`, () => {
],
}

let plugins = await loadPlugins(config)
let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

Expand All @@ -88,7 +93,7 @@ describe(`Load plugins`, () => {
}

try {
await loadPlugins(config)
await loadPlugins(config, process.cwd())
} catch (err) {
expect(err.message).toMatchSnapshot()
}
Expand All @@ -107,7 +112,7 @@ describe(`Load plugins`, () => {
],
}

let plugins = await loadPlugins(config)
let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

Expand All @@ -120,7 +125,7 @@ describe(`Load plugins`, () => {
plugins: [],
}

let plugins = await loadPlugins(config)
let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

Expand All @@ -145,7 +150,7 @@ describe(`Load plugins`, () => {
],
}

let plugins = await loadPlugins(config)
let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

Expand Down Expand Up @@ -183,7 +188,7 @@ describe(`Load plugins`, () => {
],
}

let plugins = await loadPlugins(config)
let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

Expand All @@ -197,6 +202,105 @@ describe(`Load plugins`, () => {
})
})

describe(`Gatsby-plugin-gatsby-cloud support`, () => {
it(`doesn't gatsby-plugin-gatsby-cloud if not installed`, async () => {
resolveFrom.mockImplementation(() => undefined)
const config = {
plugins: [],
}

let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

expect(plugins).toEqual(
expect.arrayContaining([
expect.not.objectContaining({
name: `gatsby-plugin-gatsby-cloud`,
}),
])
)
})

it(`loads gatsby-plugin-gatsby-cloud if not provided and installed`, async () => {
resolveFrom.mockImplementation(
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
)
const config = {
plugins: [],
}

let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

expect(plugins).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: `gatsby-plugin-gatsby-cloud`,
}),
])
)
})

it(`uses the user provided plugin-gatsby-cloud if provided`, async () => {
resolveFrom.mockImplementation(
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
)
const config = {
plugins: [
{
resolve: `gatsby-plugin-gatsby-cloud`,
options: {
generateMatchPathRewrites: false,
},
},
],
}

let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

expect(plugins).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: `gatsby-plugin-gatsby-cloud`,
pluginOptions: {
generateMatchPathRewrites: false,
plugins: [],
},
}),
])
)
})

it(`does not add gatsby-plugin-gatsby-cloud if it exists in config.plugins`, async () => {
resolveFrom.mockImplementation(
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
)
const config = {
plugins: [
`gatsby-plugin-gatsby-cloud`,
{ resolve: `gatsby-plugin-gatsby-cloud` },
],
}

let plugins = await loadPlugins(config, process.cwd())

plugins = replaceFieldsThatCanVary(plugins)

const cloudPlugins = plugins.filter(
(plugin: { name: string }) =>
plugin.name === `gatsby-plugin-gatsby-cloud`
)

// TODO: I think we should probably be de-duping, so this should be 1.
// But this test is mostly here to ensure we don't add an _additional_ gatsby-plugin-typescript
expect(cloudPlugins.length).toEqual(2)
})
})

describe(`plugin options validation`, () => {
it(`throws a structured error with invalid plugin options`, async () => {
const invalidPlugins = [
Expand All @@ -214,9 +318,12 @@ describe(`Load plugins`, () => {
},
},
]
await loadPlugins({
plugins: invalidPlugins,
})
await loadPlugins(
{
plugins: invalidPlugins,
},
process.cwd()
)

expect(reporter.error as jest.Mock).toHaveBeenCalledTimes(
invalidPlugins.length
Expand Down Expand Up @@ -309,9 +416,12 @@ describe(`Load plugins`, () => {
},
},
]
await loadPlugins({
plugins,
})
await loadPlugins(
{
plugins,
},
process.cwd()
)

expect(reporter.error as jest.Mock).toHaveBeenCalledTimes(0)
expect(reporter.warn as jest.Mock).toHaveBeenCalledTimes(1)
Expand All @@ -325,16 +435,19 @@ describe(`Load plugins`, () => {
})

it(`defaults plugin options to the ones defined in the schema`, async () => {
let plugins = await loadPlugins({
plugins: [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `fake`,
let plugins = await loadPlugins(
{
plugins: [
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `fake`,
},
},
},
],
})
],
},
process.cwd()
)

plugins = replaceFieldsThatCanVary(plugins)

Expand All @@ -354,23 +467,26 @@ describe(`Load plugins`, () => {
})

it(`validates subplugin schemas`, async () => {
await loadPlugins({
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-autolink-headers`,
options: {
maintainCase: `should be boolean`,
await loadPlugins(
{
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-autolink-headers`,
options: {
maintainCase: `should be boolean`,
},
},
},
],
],
},
},
},
],
})
],
},
process.cwd()
)

expect(reporter.error as jest.Mock).toHaveBeenCalledTimes(1)
expect((reporter.error as jest.Mock).mock.calls[0])
Expand Down Expand Up @@ -403,16 +519,19 @@ describe(`Load plugins`, () => {
})

it(`validates local plugin schemas using require.resolve`, async () => {
await loadPlugins({
plugins: [
{
resolve: require.resolve(`./fixtures/local-plugin`),
options: {
optionalString: 1234,
await loadPlugins(
{
plugins: [
{
resolve: require.resolve(`./fixtures/local-plugin`),
options: {
optionalString: 1234,
},
},
},
],
})
],
},
process.cwd()
)

expect(reporter.error as jest.Mock).toHaveBeenCalledTimes(1)
expect((reporter.error as jest.Mock).mock.calls[0])
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/bootstrap/load-plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const normalizeConfig = (config: IRawSiteConfig = {}): ISiteConfig => {

export async function loadPlugins(
rawConfig: IRawSiteConfig = {},
rootDir: string | null = null
rootDir: string
): Promise<Array<IFlattenedPlugin>> {
// Turn all strings in plugins: [`...`] into the { resolve: ``, options: {} } form
const config = normalizeConfig(rawConfig)
Expand Down
Loading

0 comments on commit 4259940

Please sign in to comment.