Skip to content

Commit

Permalink
feat: skip work when gatsby version supports adapters (#639)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
pieh and kodiakhq[bot] authored Jul 19, 2023
1 parent 25ca2ca commit 1e9ba48
Show file tree
Hide file tree
Showing 45 changed files with 1,091 additions and 6 deletions.
46 changes: 45 additions & 1 deletion plugin/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import fs, {
import type { GatsbyConfig, PluginRef } from 'gatsby'
import { v4 as uuidv4 } from 'uuid'

import { checkPackageVersion } from './files'
import { checkPackageVersion, findModuleFromBase } from './files'
import type { FunctionList } from './functions'

/**
Expand Down Expand Up @@ -338,4 +338,48 @@ function isEnvSet(envVar: string) {
export function getGatsbyRoot(publish: string): string {
return resolve(dirname(publish))
}

export function shouldSkip(publishDir: string): boolean {
if (typeof process.env.NETLIFY_SKIP_GATSBY_BUILD_PLUGIN !== 'undefined') {
return isEnvSet('NETLIFY_SKIP_GATSBY_BUILD_PLUGIN')
}

const siteRoot = getGatsbyRoot(publishDir)

let shouldSkipResult = false

try {
const gatsbyPath = findModuleFromBase({
paths: [siteRoot],
candidates: ['gatsby/package.json'],
})
if (gatsbyPath) {
const gatsbyPluginUtilsPath = findModuleFromBase({
paths: [gatsbyPath, siteRoot],
candidates: ['gatsby-plugin-utils'],
})

// eslint-disable-next-line import/no-dynamic-require, n/global-require, @typescript-eslint/no-var-requires
const { hasFeature } = require(gatsbyPluginUtilsPath)

if (hasFeature(`adapters`)) {
shouldSkipResult = true
}
}
} catch {
// ignore
}

process.env.NETLIFY_SKIP_GATSBY_BUILD_PLUGIN = shouldSkipResult
? 'true'
: 'false'

if (shouldSkipResult) {
console.log(
'Skipping @netlify/plugin-gatsby work, because used Gatsby version supports adapters.',
)
}

return shouldSkipResult
}
/* eslint-enable max-lines */
21 changes: 20 additions & 1 deletion plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getNeededFunctions,
modifyConfig,
shouldSkipBundlingDatastore,
shouldSkip,
} from './helpers/config'
import { modifyFiles } from './helpers/files'
import { deleteFunctions, writeFunctions } from './helpers/functions'
Expand All @@ -33,6 +34,11 @@ export async function onPreBuild({
`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.`,
)
}

if (shouldSkip(PUBLISH_DIR)) {
return
}

await restoreCache({ utils, publish: PUBLISH_DIR })

await checkConfig({ utils, netlifyConfig })
Expand All @@ -47,6 +53,11 @@ export async function onBuild({
FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC,
INTERNAL_FUNCTIONS_SRC,
} = constants

if (shouldSkip(PUBLISH_DIR)) {
return
}

const cacheDir = normalizedCacheDir(PUBLISH_DIR)

if (
Expand Down Expand Up @@ -81,6 +92,10 @@ export async function onPostBuild({
constants: { PUBLISH_DIR, FUNCTIONS_DIST },
utils,
}): Promise<void> {
if (shouldSkip(PUBLISH_DIR)) {
return
}

await saveCache({ publish: PUBLISH_DIR, utils })

const cacheDir = normalizedCacheDir(PUBLISH_DIR)
Expand All @@ -92,7 +107,11 @@ export async function onPostBuild({
}
}

export async function onSuccess() {
export async function onSuccess({ constants: { PUBLISH_DIR } }) {
if (shouldSkip(PUBLISH_DIR)) {
return
}

// Pre-warm the lambdas as downloading the datastore file can take a while
if (shouldSkipBundlingDatastore()) {
const FETCH_TIMEOUT = 5000
Expand Down
1 change: 1 addition & 0 deletions plugin/test/fixtures/v5/with-adapters/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pickle=word
14 changes: 14 additions & 0 deletions plugin/test/fixtures/v5/with-adapters/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/
.cache/
public

# Local Netlify folder
.netlify
netlify/functions/gatsby/functions
deployment.json

# @netlify/plugin-gatsby ignores start
netlify/functions/gatsby
# @netlify/plugin-gatsby ignores end

package-lock.json
1 change: 1 addition & 0 deletions plugin/test/fixtures/v5/with-adapters/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v14.17.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Local functions can parse different ways of sending data file in multipart/form 1`] = `
Object {
"body": Object {
"something": "here",
},
"files": Array [
Object {
"buffer": Object {
"data": Array [
104,
105,
],
"type": "Buffer",
},
"encoding": "7bit",
"fieldname": "file",
"mimetype": "text/plain",
"originalname": "test.txt",
"size": 2,
},
],
}
`;

exports[`Local routing dynamic routes 1`] = `
Object {
"super": "additional",
"userId": "23",
}
`;

exports[`Local routing dynamic routes 2`] = `
Object {
"*": "super",
"0": "super",
}
`;

exports[`Local routing routes with special characters 1`] = `"I-Am-Capitalized.js"`;
33 changes: 33 additions & 0 deletions plugin/test/fixtures/v5/with-adapters/e2e-tests/build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// eslint-disable-next-line node/no-unpublished-require
const { buildSite } = require('../../../../helpers')
const { readFileSync } = require('fs')

jest.setTimeout(240_000)
describe('A site using gatsby version with adapters', () => {
it('successfully builds and disables @netlify/plugin-gatsby and gatsby-plugin-netlify', async () => {
const {
success,
logs: { stdout, stderr },
} = await buildSite()

// in CI warnings are outputted to stderr (yikes)
const fullOutput = stdout + stderr

expect(success).toBeTruthy()

expect(fullOutput).toContain(
'Skipping @netlify/plugin-gatsby work, because used Gatsby version supports adapters.',
)
expect(fullOutput).toContain('Disabling plugin "gatsby-plugin-netlify"')

const _redirectsContent = readFileSync('public/_redirects', 'utf8')

expect(_redirectsContent).not.toContain(
'# @netlify/plugin-gatsby redirects start',
)
expect(_redirectsContent).not.toContain(
'## Created with gatsby-plugin-netlify',
)
expect(_redirectsContent).toContain('# gatsby-adapter-netlify start')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { runTests } = require('./test-helpers')

if (process.env.TEST_ENV === 'netlify') {
const { deploy_url } = require('../deployment.json')
runTests('Netlify', deploy_url)
} else {
runTests('Local', 'http://localhost:8888')
}
Loading

0 comments on commit 1e9ba48

Please sign in to comment.