Skip to content

Commit 0d929f1

Browse files
ascorbicLekoArts
authored andcommitted
fix(gatsby-plugin-image): Use bare GATSBY___IMAGE global (#30713)
* fix(gatsby-plugin-image): Use bare GATSBY___IMAGE global * Fix check * Fix test * Use helper * Remove unused type (cherry picked from commit a5869e3)
1 parent 29fe049 commit 0d929f1

File tree

6 files changed

+32
-40
lines changed

6 files changed

+32
-40
lines changed

packages/gatsby-plugin-image/src/components/__tests__/gatsby-image.browser.tsx

+8-14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ import { GatsbyImage, IGatsbyImageData } from "../gatsby-image.browser"
33
import { render, waitFor } from "@testing-library/react"
44
import * as hooks from "../hooks"
55

6-
type GlobalOverride = NodeJS.Global &
7-
typeof global.globalThis & {
8-
// eslint-disable-next-line @typescript-eslint/naming-convention
9-
GATSBY___IMAGE: boolean
10-
SERVER: boolean
11-
}
12-
136
// Prevents terser for bailing because we're not in a babel plugin
147
jest.mock(`../../../macros/terser.macro`, () => (strs): string => strs.join(``))
158

@@ -19,8 +12,9 @@ describe(`GatsbyImage browser`, () => {
1912

2013
beforeEach(() => {
2114
console.warn = jest.fn()
22-
;(global as GlobalOverride).SERVER = true
23-
;(global as GlobalOverride).GATSBY___IMAGE = true
15+
console.error = jest.fn()
16+
global.SERVER = true
17+
global.GATSBY___IMAGE = true
2418
})
2519

2620
beforeEach(() => {
@@ -73,20 +67,20 @@ describe(`GatsbyImage browser`, () => {
7367

7468
afterEach(() => {
7569
jest.clearAllMocks()
76-
;(global as GlobalOverride).SERVER = undefined
77-
;(global as GlobalOverride).GATSBY___IMAGE = undefined
70+
global.SERVER = undefined
71+
global.GATSBY___IMAGE = undefined
7872
})
7973

8074
it(`shows a suggestion to switch to the new gatsby-image API when available`, async () => {
81-
;(global as GlobalOverride).GATSBY___IMAGE = false
75+
global.GATSBY___IMAGE = undefined
8276

8377
const { container } = render(
8478
<GatsbyImage image={image} alt="Alt content" />
8579
)
8680

8781
await waitFor(() => container.querySelector(`[data-placeholder-image=""]`))
8882

89-
expect(console.warn).toBeCalledWith(
83+
expect(console.error).toBeCalledWith(
9084
`[gatsby-plugin-image] You're missing out on some cool performance features. Please add "gatsby-plugin-image" to your gatsby-config.js`
9185
)
9286
})
@@ -164,7 +158,7 @@ describe(`GatsbyImage browser`, () => {
164158
container.querySelector(`[data-main-image=""]`)
165159
)
166160

167-
img.dispatchEvent(new Event(`load`))
161+
img?.dispatchEvent(new Event(`load`))
168162

169163
expect(onStartLoadSpy).toBeCalledWith({ wasCached: false })
170164
expect(onLoadSpy).toBeCalled()

packages/gatsby-plugin-image/src/components/__tests__/gatsby-image.server.tsx

+4-11
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@ import { GatsbyImage } from "../gatsby-image.server"
44
import { IGatsbyImageData } from "../gatsby-image.browser"
55
import { SourceProps } from "../picture"
66

7-
type GlobalOverride = NodeJS.Global &
8-
typeof global.globalThis & {
9-
SERVER: boolean | undefined
10-
// eslint-disable-next-line @typescript-eslint/naming-convention
11-
GATSBY___IMAGE: boolean | undefined
12-
}
13-
147
// Prevents terser for bailing because we're not in a babel plugin
158
jest.mock(`../../../macros/terser.macro`, () => (strs): string => strs.join(``))
169

1710
describe(`GatsbyImage server`, () => {
1811
beforeEach(() => {
1912
console.warn = jest.fn()
20-
;(global as GlobalOverride).SERVER = true
21-
;(global as GlobalOverride).GATSBY___IMAGE = true
13+
global.SERVER = true
14+
global.GATSBY___IMAGE = true
2215
})
2316

2417
afterEach(() => {
2518
jest.clearAllMocks()
26-
;(global as GlobalOverride).SERVER = false
27-
;(global as GlobalOverride).GATSBY___IMAGE = false
19+
global.SERVER = false
20+
global.GATSBY___IMAGE = false
2821
})
2922

3023
it(`shows nothing when the image props is not passed`, () => {

packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
hasNativeLazyLoadSupport,
1515
storeImageloaded,
1616
hasImageLoaded,
17+
gatsbyImageIsInstalled,
1718
} from "./hooks"
1819
import { PlaceholderProps } from "./placeholder"
1920
import { MainImageProps } from "./main-image"
@@ -182,7 +183,11 @@ class GatsbyImageHydrator extends Component<
182183
const cacheKey = JSON.stringify(this.props.image.images)
183184

184185
// when SSR and native lazyload is supported we'll do nothing ;)
185-
if (hasNativeLazyLoadSupport() && ssrElement && global.GATSBY___IMAGE) {
186+
if (
187+
hasNativeLazyLoadSupport() &&
188+
ssrElement &&
189+
gatsbyImageIsInstalled()
190+
) {
186191
this.props.onStartLoad?.({ wasCached: false })
187192

188193
// When the image is already loaded before we have hydrated, we trigger onLoad and cache the item
@@ -272,8 +277,8 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
272277
return null
273278
}
274279

275-
if (!global.GATSBY___IMAGE) {
276-
console.warn(
280+
if (!gatsbyImageIsInstalled()) {
281+
console.error(
277282
`[gatsby-plugin-image] You're missing out on some cool performance features. Please add "gatsby-plugin-image" to your gatsby-config.js`
278283
)
279284
}

packages/gatsby-plugin-image/src/components/hooks.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export const hasNativeLazyLoadSupport = (): boolean =>
2929
typeof HTMLImageElement !== `undefined` &&
3030
`loading` in HTMLImageElement.prototype
3131

32+
export function gatsbyImageIsInstalled(): boolean {
33+
return typeof GATSBY___IMAGE !== `undefined` && GATSBY___IMAGE
34+
}
35+
3236
export function storeImageloaded(cacheKey?: string): void {
3337
if (cacheKey) {
3438
imageCache.add(cacheKey)
@@ -86,7 +90,7 @@ export function getWrapperProps(
8690
let className = `gatsby-image-wrapper`
8791

8892
// If the plugin isn't installed we need to apply the styles inline
89-
if (!global.GATSBY___IMAGE) {
93+
if (!gatsbyImageIsInstalled()) {
9094
wrapperStyle.position = `relative`
9195
wrapperStyle.overflow = `hidden`
9296
}
@@ -95,7 +99,7 @@ export function getWrapperProps(
9599
wrapperStyle.width = width
96100
wrapperStyle.height = height
97101
} else if (layout === `constrained`) {
98-
if (!global.GATSBY___IMAGE) {
102+
if (!gatsbyImageIsInstalled()) {
99103
wrapperStyle.display = `inline-block`
100104
}
101105
className = `gatsby-image-wrapper gatsby-image-wrapper-constrained`
@@ -267,7 +271,7 @@ export function getMainProps(
267271
}
268272

269273
// fallback when it's not configured in gatsby-config.
270-
if (!global.GATSBY___IMAGE) {
274+
if (!gatsbyImageIsInstalled()) {
271275
style = {
272276
height: `100%`,
273277
left: 0,
@@ -347,7 +351,7 @@ export function getPlaceholderProps(
347351
}
348352

349353
// fallback when it's not configured in gatsby-config.
350-
if (!global.GATSBY___IMAGE) {
354+
if (!gatsbyImageIsInstalled()) {
351355
result.style = {
352356
height: `100%`,
353357
left: 0,

packages/gatsby-plugin-image/src/gatsby-node.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] = ({
3535
actions.setWebpackConfig({
3636
plugins: [
3737
plugins.define({
38-
[`global.GATSBY___IMAGE`]: true,
38+
// eslint-disable-next-line @typescript-eslint/naming-convention
39+
GATSBY___IMAGE: true,
3940
}),
4041
],
4142
})
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
export {}
22

33
declare global {
4-
declare var SERVER: boolean
5-
6-
namespace NodeJS {
7-
interface Global {
8-
GATSBY___IMAGE: boolean | undefined
9-
}
10-
}
4+
declare var SERVER: boolean | undefined
5+
declare var GATSBY___IMAGE: boolean | undefined
116
}

0 commit comments

Comments
 (0)