Skip to content

Commit 1e9ba48

Browse files
piehkodiakhq[bot]
andauthored
feat: skip work when gatsby version supports adapters (#639)
* feat: skip work when gatsby version supports adapters * fix: unit tests * feat: log that work is skipped when adapters are used * test: add e2e test * test: add unit test for shouldSkip * test: log version * test: check both stdout and stderr for logs * test: bump timeout * test: bump canary version --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 25ca2ca commit 1e9ba48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1091
-6
lines changed

Diff for: plugin/src/helpers/config.ts

+45-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import fs, {
1515
import type { GatsbyConfig, PluginRef } from 'gatsby'
1616
import { v4 as uuidv4 } from 'uuid'
1717

18-
import { checkPackageVersion } from './files'
18+
import { checkPackageVersion, findModuleFromBase } from './files'
1919
import type { FunctionList } from './functions'
2020

2121
/**
@@ -338,4 +338,48 @@ function isEnvSet(envVar: string) {
338338
export function getGatsbyRoot(publish: string): string {
339339
return resolve(dirname(publish))
340340
}
341+
342+
export function shouldSkip(publishDir: string): boolean {
343+
if (typeof process.env.NETLIFY_SKIP_GATSBY_BUILD_PLUGIN !== 'undefined') {
344+
return isEnvSet('NETLIFY_SKIP_GATSBY_BUILD_PLUGIN')
345+
}
346+
347+
const siteRoot = getGatsbyRoot(publishDir)
348+
349+
let shouldSkipResult = false
350+
351+
try {
352+
const gatsbyPath = findModuleFromBase({
353+
paths: [siteRoot],
354+
candidates: ['gatsby/package.json'],
355+
})
356+
if (gatsbyPath) {
357+
const gatsbyPluginUtilsPath = findModuleFromBase({
358+
paths: [gatsbyPath, siteRoot],
359+
candidates: ['gatsby-plugin-utils'],
360+
})
361+
362+
// eslint-disable-next-line import/no-dynamic-require, n/global-require, @typescript-eslint/no-var-requires
363+
const { hasFeature } = require(gatsbyPluginUtilsPath)
364+
365+
if (hasFeature(`adapters`)) {
366+
shouldSkipResult = true
367+
}
368+
}
369+
} catch {
370+
// ignore
371+
}
372+
373+
process.env.NETLIFY_SKIP_GATSBY_BUILD_PLUGIN = shouldSkipResult
374+
? 'true'
375+
: 'false'
376+
377+
if (shouldSkipResult) {
378+
console.log(
379+
'Skipping @netlify/plugin-gatsby work, because used Gatsby version supports adapters.',
380+
)
381+
}
382+
383+
return shouldSkipResult
384+
}
341385
/* eslint-enable max-lines */

Diff for: plugin/src/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getNeededFunctions,
1515
modifyConfig,
1616
shouldSkipBundlingDatastore,
17+
shouldSkip,
1718
} from './helpers/config'
1819
import { modifyFiles } from './helpers/files'
1920
import { deleteFunctions, writeFunctions } from './helpers/functions'
@@ -33,6 +34,11 @@ export async function onPreBuild({
3334
`Gatsby sites must publish the "public" directory, but your site’s publish directory is set to “${PUBLISH_DIR}”. Please set your publish directory to your Gatsby site’s "public" directory.`,
3435
)
3536
}
37+
38+
if (shouldSkip(PUBLISH_DIR)) {
39+
return
40+
}
41+
3642
await restoreCache({ utils, publish: PUBLISH_DIR })
3743

3844
await checkConfig({ utils, netlifyConfig })
@@ -47,6 +53,11 @@ export async function onBuild({
4753
FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC,
4854
INTERNAL_FUNCTIONS_SRC,
4955
} = constants
56+
57+
if (shouldSkip(PUBLISH_DIR)) {
58+
return
59+
}
60+
5061
const cacheDir = normalizedCacheDir(PUBLISH_DIR)
5162

5263
if (
@@ -81,6 +92,10 @@ export async function onPostBuild({
8192
constants: { PUBLISH_DIR, FUNCTIONS_DIST },
8293
utils,
8394
}): Promise<void> {
95+
if (shouldSkip(PUBLISH_DIR)) {
96+
return
97+
}
98+
8499
await saveCache({ publish: PUBLISH_DIR, utils })
85100

86101
const cacheDir = normalizedCacheDir(PUBLISH_DIR)
@@ -92,7 +107,11 @@ export async function onPostBuild({
92107
}
93108
}
94109

95-
export async function onSuccess() {
110+
export async function onSuccess({ constants: { PUBLISH_DIR } }) {
111+
if (shouldSkip(PUBLISH_DIR)) {
112+
return
113+
}
114+
96115
// Pre-warm the lambdas as downloading the datastore file can take a while
97116
if (shouldSkipBundlingDatastore()) {
98117
const FETCH_TIMEOUT = 5000
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pickle=word

Diff for: plugin/test/fixtures/v5/with-adapters/.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node_modules/
2+
.cache/
3+
public
4+
5+
# Local Netlify folder
6+
.netlify
7+
netlify/functions/gatsby/functions
8+
deployment.json
9+
10+
# @netlify/plugin-gatsby ignores start
11+
netlify/functions/gatsby
12+
# @netlify/plugin-gatsby ignores end
13+
14+
package-lock.json

Diff for: plugin/test/fixtures/v5/with-adapters/.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.17.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Local functions can parse different ways of sending data file in multipart/form 1`] = `
4+
Object {
5+
"body": Object {
6+
"something": "here",
7+
},
8+
"files": Array [
9+
Object {
10+
"buffer": Object {
11+
"data": Array [
12+
104,
13+
105,
14+
],
15+
"type": "Buffer",
16+
},
17+
"encoding": "7bit",
18+
"fieldname": "file",
19+
"mimetype": "text/plain",
20+
"originalname": "test.txt",
21+
"size": 2,
22+
},
23+
],
24+
}
25+
`;
26+
27+
exports[`Local routing dynamic routes 1`] = `
28+
Object {
29+
"super": "additional",
30+
"userId": "23",
31+
}
32+
`;
33+
34+
exports[`Local routing dynamic routes 2`] = `
35+
Object {
36+
"*": "super",
37+
"0": "super",
38+
}
39+
`;
40+
41+
exports[`Local routing routes with special characters 1`] = `"I-Am-Capitalized.js"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// eslint-disable-next-line node/no-unpublished-require
2+
const { buildSite } = require('../../../../helpers')
3+
const { readFileSync } = require('fs')
4+
5+
jest.setTimeout(240_000)
6+
describe('A site using gatsby version with adapters', () => {
7+
it('successfully builds and disables @netlify/plugin-gatsby and gatsby-plugin-netlify', async () => {
8+
const {
9+
success,
10+
logs: { stdout, stderr },
11+
} = await buildSite()
12+
13+
// in CI warnings are outputted to stderr (yikes)
14+
const fullOutput = stdout + stderr
15+
16+
expect(success).toBeTruthy()
17+
18+
expect(fullOutput).toContain(
19+
'Skipping @netlify/plugin-gatsby work, because used Gatsby version supports adapters.',
20+
)
21+
expect(fullOutput).toContain('Disabling plugin "gatsby-plugin-netlify"')
22+
23+
const _redirectsContent = readFileSync('public/_redirects', 'utf8')
24+
25+
expect(_redirectsContent).not.toContain(
26+
'# @netlify/plugin-gatsby redirects start',
27+
)
28+
expect(_redirectsContent).not.toContain(
29+
'## Created with gatsby-plugin-netlify',
30+
)
31+
expect(_redirectsContent).toContain('# gatsby-adapter-netlify start')
32+
})
33+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { runTests } = require('./test-helpers')
2+
3+
if (process.env.TEST_ENV === 'netlify') {
4+
const { deploy_url } = require('../deployment.json')
5+
runTests('Netlify', deploy_url)
6+
} else {
7+
runTests('Local', 'http://localhost:8888')
8+
}

0 commit comments

Comments
 (0)