Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip work when gatsby version supports adapters #639

Merged
merged 10 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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