Skip to content

Commit 3beb047

Browse files
feat(gatsby): Auto-Install Preview UI (#35587)
* initial commit * added check to install preview-ui * updated tests
1 parent c410214 commit 3beb047

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

packages/gatsby/src/bootstrap/load-plugins/__tests__/load-plugins.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,12 @@ describe(`Load plugins`, () => {
245245
})
246246

247247
it(`loads gatsby-plugin-gatsby-cloud if not provided and installed on gatsby-cloud`, async () => {
248-
resolveFrom.mockImplementation(
249-
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
250-
)
248+
resolveFrom.mockImplementation((rootDir, pkg) => {
249+
if (pkg !== `gatsby-plugin-gatsby-cloud`) {
250+
return undefined
251+
}
252+
return rootDir + `/node_modules/` + pkg
253+
})
251254
const config = {
252255
plugins: [],
253256
}
@@ -268,9 +271,12 @@ describe(`Load plugins`, () => {
268271
})
269272

270273
it(`uses the user provided plugin-gatsby-cloud if provided`, async () => {
271-
resolveFrom.mockImplementation(
272-
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
273-
)
274+
resolveFrom.mockImplementation((rootDir, pkg) => {
275+
if (pkg !== `gatsby-plugin-gatsby-cloud`) {
276+
return undefined
277+
}
278+
return rootDir + `/node_modules/` + pkg
279+
})
274280
const config = {
275281
plugins: [
276282
{
@@ -302,9 +308,12 @@ describe(`Load plugins`, () => {
302308
})
303309

304310
it(`does not add gatsby-plugin-gatsby-cloud if it exists in config.plugins`, async () => {
305-
resolveFrom.mockImplementation(
306-
(rootDir, pkg) => rootDir + `/node_modules/` + pkg
307-
)
311+
resolveFrom.mockImplementation((rootDir, pkg) => {
312+
if (pkg !== `gatsby-plugin-gatsby-cloud`) {
313+
return undefined
314+
}
315+
return rootDir + `/node_modules/` + pkg
316+
})
308317
const config = {
309318
plugins: [
310319
`gatsby-plugin-gatsby-cloud`,

packages/gatsby/src/bootstrap/load-plugins/load-internal-plugins.ts

+9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import { createPluginId } from "./utils/create-id"
1313
import { createFileContentHash } from "./utils/create-hash"
1414
import {
1515
addGatsbyPluginCloudPluginWhenInstalled,
16+
addGatsbyPluginPreviewWhenInstalled,
1617
incompatibleGatsbyCloudPlugin,
1718
GATSBY_CLOUD_PLUGIN_NAME,
19+
GATSBY_PLUGIN_PREVIEW_NAME,
1820
} from "./utils/handle-gatsby-cloud"
1921
import { getResolvedFieldsForPlugin } from "../../utils/parcel/compile-gatsby-files"
2022

@@ -90,6 +92,13 @@ export function loadInternalPlugins(
9092
addGatsbyPluginCloudPluginWhenInstalled(plugins, rootDir)
9193
}
9294

95+
if (
96+
!configuredPluginNames.has(GATSBY_PLUGIN_PREVIEW_NAME) &&
97+
(process.env.GATSBY_CLOUD === `true` || process.env.GATSBY_CLOUD === `1`)
98+
) {
99+
addGatsbyPluginPreviewWhenInstalled(plugins, rootDir)
100+
}
101+
93102
// Support Typescript by default but allow users to override it
94103
if (!configuredPluginNames.has(TYPESCRIPT_PLUGIN_NAME)) {
95104
const processedTypeScriptPlugin = processPlugin(

packages/gatsby/src/bootstrap/load-plugins/utils/handle-gatsby-cloud.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { IPluginInfo } from "../types"
44
import { processPlugin } from "../process-plugin"
55

66
export const GATSBY_CLOUD_PLUGIN_NAME = `gatsby-plugin-gatsby-cloud`
7+
export const GATSBY_PLUGIN_PREVIEW_NAME = `@gatsby-cloud-pkg/gatsby-plugin-preview`
78

8-
export function addGatsbyPluginCloudPluginWhenInstalled(
9+
function addCloudPluginWhenInstalled(
910
plugins: Array<IPluginInfo>,
10-
rootDir: string
11+
rootDir: string,
12+
name: string
1113
): void {
12-
const cloudPluginLocation = resolveFromSilent(
13-
rootDir,
14-
GATSBY_CLOUD_PLUGIN_NAME
15-
)
14+
const cloudPluginLocation = resolveFromSilent(rootDir, name)
1615

1716
if (cloudPluginLocation) {
1817
const processedGatsbyCloudPlugin = processPlugin(
@@ -26,6 +25,20 @@ export function addGatsbyPluginCloudPluginWhenInstalled(
2625
}
2726
}
2827

28+
export function addGatsbyPluginPreviewWhenInstalled(
29+
plugins: Array<IPluginInfo>,
30+
rootDir: string
31+
): void {
32+
addCloudPluginWhenInstalled(plugins, rootDir, GATSBY_PLUGIN_PREVIEW_NAME)
33+
}
34+
35+
export function addGatsbyPluginCloudPluginWhenInstalled(
36+
plugins: Array<IPluginInfo>,
37+
rootDir: string
38+
): void {
39+
addCloudPluginWhenInstalled(plugins, rootDir, GATSBY_CLOUD_PLUGIN_NAME)
40+
}
41+
2942
export function incompatibleGatsbyCloudPlugin(
3043
plugins: Array<IPluginInfo>
3144
): boolean {

0 commit comments

Comments
 (0)