Skip to content

Commit

Permalink
feat(gatsby): Auto-Install Preview UI (#35587)
Browse files Browse the repository at this point in the history
* initial commit

* added check to install preview-ui

* updated tests
  • Loading branch information
leithonenglish authored May 6, 2022
1 parent c410214 commit 3beb047
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,12 @@ describe(`Load plugins`, () => {
})

it(`loads gatsby-plugin-gatsby-cloud if not provided and installed on gatsby-cloud`, async () => {
resolveFrom.mockImplementation(
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
)
resolveFrom.mockImplementation((rootDir, pkg) => {
if (pkg !== `gatsby-plugin-gatsby-cloud`) {
return undefined
}
return rootDir + `/node_modules/` + pkg
})
const config = {
plugins: [],
}
Expand All @@ -268,9 +271,12 @@ describe(`Load plugins`, () => {
})

it(`uses the user provided plugin-gatsby-cloud if provided`, async () => {
resolveFrom.mockImplementation(
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
)
resolveFrom.mockImplementation((rootDir, pkg) => {
if (pkg !== `gatsby-plugin-gatsby-cloud`) {
return undefined
}
return rootDir + `/node_modules/` + pkg
})
const config = {
plugins: [
{
Expand Down Expand Up @@ -302,9 +308,12 @@ describe(`Load plugins`, () => {
})

it(`does not add gatsby-plugin-gatsby-cloud if it exists in config.plugins`, async () => {
resolveFrom.mockImplementation(
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
)
resolveFrom.mockImplementation((rootDir, pkg) => {
if (pkg !== `gatsby-plugin-gatsby-cloud`) {
return undefined
}
return rootDir + `/node_modules/` + pkg
})
const config = {
plugins: [
`gatsby-plugin-gatsby-cloud`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import { createPluginId } from "./utils/create-id"
import { createFileContentHash } from "./utils/create-hash"
import {
addGatsbyPluginCloudPluginWhenInstalled,
addGatsbyPluginPreviewWhenInstalled,
incompatibleGatsbyCloudPlugin,
GATSBY_CLOUD_PLUGIN_NAME,
GATSBY_PLUGIN_PREVIEW_NAME,
} from "./utils/handle-gatsby-cloud"
import { getResolvedFieldsForPlugin } from "../../utils/parcel/compile-gatsby-files"

Expand Down Expand Up @@ -90,6 +92,13 @@ export function loadInternalPlugins(
addGatsbyPluginCloudPluginWhenInstalled(plugins, rootDir)
}

if (
!configuredPluginNames.has(GATSBY_PLUGIN_PREVIEW_NAME) &&
(process.env.GATSBY_CLOUD === `true` || process.env.GATSBY_CLOUD === `1`)
) {
addGatsbyPluginPreviewWhenInstalled(plugins, rootDir)
}

// Support Typescript by default but allow users to override it
if (!configuredPluginNames.has(TYPESCRIPT_PLUGIN_NAME)) {
const processedTypeScriptPlugin = processPlugin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { IPluginInfo } from "../types"
import { processPlugin } from "../process-plugin"

export const GATSBY_CLOUD_PLUGIN_NAME = `gatsby-plugin-gatsby-cloud`
export const GATSBY_PLUGIN_PREVIEW_NAME = `@gatsby-cloud-pkg/gatsby-plugin-preview`

export function addGatsbyPluginCloudPluginWhenInstalled(
function addCloudPluginWhenInstalled(
plugins: Array<IPluginInfo>,
rootDir: string
rootDir: string,
name: string
): void {
const cloudPluginLocation = resolveFromSilent(
rootDir,
GATSBY_CLOUD_PLUGIN_NAME
)
const cloudPluginLocation = resolveFromSilent(rootDir, name)

if (cloudPluginLocation) {
const processedGatsbyCloudPlugin = processPlugin(
Expand All @@ -26,6 +25,20 @@ export function addGatsbyPluginCloudPluginWhenInstalled(
}
}

export function addGatsbyPluginPreviewWhenInstalled(
plugins: Array<IPluginInfo>,
rootDir: string
): void {
addCloudPluginWhenInstalled(plugins, rootDir, GATSBY_PLUGIN_PREVIEW_NAME)
}

export function addGatsbyPluginCloudPluginWhenInstalled(
plugins: Array<IPluginInfo>,
rootDir: string
): void {
addCloudPluginWhenInstalled(plugins, rootDir, GATSBY_CLOUD_PLUGIN_NAME)
}

export function incompatibleGatsbyCloudPlugin(
plugins: Array<IPluginInfo>
): boolean {
Expand Down

0 comments on commit 3beb047

Please sign in to comment.