Skip to content

feat: auto-install gatsby plugin #261

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all 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
407 changes: 220 additions & 187 deletions plugin/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions plugin/package.json
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@
"download": "^8.0.0",
"etag": "^1.8.1",
"fs-extra": "^10.0.0",
"gatsby-plugin-netlify": "^4.1.0",
"linkfs": "^2.1.0",
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
44 changes: 41 additions & 3 deletions plugin/src/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* eslint-disable max-lines */
import { EOL } from 'os'
import path from 'path'
import process from 'process'

import { stripIndent } from 'common-tags'
import fs, { existsSync } from 'fs-extra'
import fs, { existsSync, move, writeFile } from 'fs-extra'
import type { GatsbyConfig, PluginRef } from 'gatsby'
import { relative } from 'pathe'

import { getGatsbyConfigWrapper } from '../templates/gatsby-config'

export async function spliceConfig({
startMarker,
@@ -62,14 +66,47 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
)
}

export function checkGatsbyConfig({ utils, netlifyConfig }): void {
function hasAutoInstall(
// eslint-disable-next-line camelcase
plugins: Array<PluginRef & { __nf_auto?: true }>,
): boolean {
// eslint-disable-next-line no-underscore-dangle
return plugins?.some((plugin) => plugin.__nf_auto)
}

/**
* Wrap the Gatsby config to inject the Netlify plugin
*/
export async function wrapGatsbyConfig() {
const configFile = path.resolve(process.cwd(), 'gatsby-config.js')
if (!existsSync(configFile)) {
console.error('Could not find Gatsby config')
return
}
await move(configFile, path.resolve(process.cwd(), 'gatsby-config-orig.js'))

const pluginRoot = require.resolve('gatsby-plugin-netlify')

const pluginPath = relative(process.cwd(), pluginRoot)
console.log({ pluginPath, pluginRoot })
await writeFile(configFile, getGatsbyConfigWrapper(pluginPath))
}

export async function checkGatsbyConfig({
utils,
netlifyConfig,
}): Promise<void> {
// warn if gatsby-plugin-netlify is missing
const gatsbyConfig = loadGatsbyConfig(utils)

if (!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
if (
!hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify') &&
!hasAutoInstall(gatsbyConfig.plugins)
) {
console.error(
'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/',
)
await wrapGatsbyConfig()
}

if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify-cache')) {
@@ -152,3 +189,4 @@ export function shouldSkipFunctions(cacheDir: string): boolean {

return false
}
/* eslint-enable max-lines */
2 changes: 1 addition & 1 deletion plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ export async function onPreBuild({
}
await restoreCache({ utils, publish: PUBLISH_DIR })

checkGatsbyConfig({ utils, netlifyConfig })
await checkGatsbyConfig({ utils, netlifyConfig })
}

export async function onBuild({
18 changes: 18 additions & 0 deletions plugin/src/templates/gatsby-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { stripIndent as javascript } from 'common-tags'

export const getGatsbyConfigWrapper = (pluginPath: string): string =>
// A string, but with the right editor plugin this will format as JS
javascript`

const originalConfig = require('./gatsby-config-orig')
module.exports = {
...originalConfig,
plugins: [
...originalConfig.plugins,
{
resolve: require.resolve("${pluginPath}"),
"__nf_auto": true
},
],
}
`
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
flags: {
FUNCTIONS: true,
},
siteMetadata: {
title: 'Function test',
},
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
"test:jest": "jest"
},
"dependencies": {
"gatsby": "^4.5.4",
"gatsby": "latest",
"react": "^17.0.1",
"react-dom": "^17.0.1"
},