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

Add a gatsby cli api #14366

Closed
KnisterPeter opened this issue May 28, 2019 · 9 comments
Closed

Add a gatsby cli api #14366

KnisterPeter opened this issue May 28, 2019 · 9 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@KnisterPeter
Copy link

Summary

Integrate running gatsby in a current node application. This would be like some cli-api.

Motivation

Besides the possibility to use process_child and spawn a new process which executes gatsby then there might be a few cases where this is either not as easy to achieve or might seem suboptimal.

My concrete case is running gatsby in a lambda function. The process of creating a lambda package is creating a zip-file with all required dependencies and upload that to AWS.
The most optimal process here would be to use e.g. webpack or parcel to create a selfcontained bundled application with every dependency (including gatsby) in the bundle. As a side effect this will treeshake gatsby (and its dependencies) and reduce the application size a lot.

The problem here is that bundlers could not include all dependencies for a forked process. Hence the idea to itegrate it via API.

@wardpeet
Copy link
Contributor

All code to do a programmatic gatsby build or gatsby develop can be found inside the gatsby package.

The gatsby-cli is just a wrapper around these commands. You can write your own cli or just execute it from nodejs yourself fairly easy.

cli.command({
command: `build`,
desc: `Build a Gatsby project.`,
builder: _ =>
_.option(`prefix-paths`, {
type: `boolean`,
default: false,
describe: `Build site with link paths prefixed (set pathPrefix in your gatsby-config.js).`,
})
.option(`no-uglify`, {
type: `boolean`,
default: false,
describe: `Build site without uglifying JS bundles (for debugging).`,
})
.option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (OpenTracing compatible). See https://gatsby.dev/tracing`,
}),
handler: handlerP(
getCommandHandler(`build`, (args, cmd) => {
process.env.NODE_ENV = `production`
return cmd(args)
})
),
})

@wardpeet wardpeet added the type: question or discussion Issue discussing or asking a question about Gatsby label May 28, 2019
@KnisterPeter
Copy link
Author

@wardpeet Thank for point me into the right direction.
But I think I'm missing something here. getCommandHandler is an internal function, cmd is a parameter, handlerP is an internal function.

Is there a more simple aproach? Any documenation?

@wardpeet
Copy link
Contributor

This is pretty uncommon so we don't really have a guide for it but this is a small example that should get you going.

create a node script: (i'll go for custom-gatsby.js) so you can execute it by doing node custom-gatsby.js

const gatsbyBuild = require('gatsby/dist/commands/build.js');

gatsbyBuild({
  directory: process.cwd(),
  sitePackageJson:  require(path.join(process.cwd(), `package.json`)),
  prefixPaths: false,
  noUglify: false,
})

@KnisterPeter
Copy link
Author

Awesome. Thank you so much.

@wardpeet
Copy link
Contributor

feel free to reach out if you need more info.

@KnisterPeter
Copy link
Author

Hi @wardpeet ,

I've tried to call gatsby as you recommended above. But that will result in the following error.
Strangly running gatsby via gatsby build from the cli does work.

Any idea what is going wrong here?

Error: ContentfulAsset.fixed provided incorrect OutputType: 'ContentfulFixed'
    at TypeMapper.convertOutputFieldConfig (/var/task/node_modules/graphql-compose/lib/TypeMapper.js:294:15)
    at resolveOutputConfigAsThunk (/var/task/node_modules/graphql-compose/lib/utils/configAsThunk.js:19:41)
    at ObjectTypeComposer.getFieldConfig (/var/task/node_modules/graphql-compose/lib/ObjectTypeComposer.js:300:58)
    at fieldNames.forEach.fieldName (/var/task/node_modules/graphql-compose/lib/utils/toInputObjectType.js:44:19)
    at Array.forEach (<anonymous>)
    at toInputObjectType (/var/task/node_modules/graphql-compose/lib/utils/toInputObjectType.js:38:14)
    at convertInputObjectField (/var/task/node_modules/graphql-compose/lib/utils/toInputObjectType.js:78:19)
    at fieldNames.forEach.fieldName (/var/task/node_modules/graphql-compose/lib/utils/toInputObjectType.js:45:23)
    at Array.forEach (<anonymous>)
    at toInputObjectType (/var/task/node_modules/graphql-compose/lib/utils/toInputObjectType.js:38:14)
    at ObjectTypeComposer.getInputTypeComposer (/var/task/node_modules/graphql-compose/lib/ObjectTypeComposer.js:600:84)
    at getSortInput (/var/task/node_modules/gatsby/dist/schema/types/sort.js:38:42)
    at addResolvers (/var/task/node_modules/gatsby/dist/schema/schema.js:572:23)
    at /var/task/node_modules/gatsby/dist/schema/schema.js:196:15
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)```

@wardpeet
Copy link
Contributor

You don't get this error when running gatsby build from the cli?

@KnisterPeter
Copy link
Author

No, from cli this works as expected.

@david-j-davis
Copy link

david-j-davis commented Nov 1, 2019

@wardpeet I'm trying to do something similar, accomplish a build inside of a Docker nodejs image. How can I tell the build step where to find the plugins declared in gatsby-config.js? When using your example of gatsbyBuild method I get the following error:

Couldn't find the "gatsby-plugin-react-helmet" plugin declared in "/usr/app/tmp/my-new-app/gatsby-config.js".
Tried looking for a local plugin in /usr/app/tmp/my-new-app/plugins/gatsby-plugin-react-helmet.
Tried looking for an installed package in the following paths:
 - /usr/app/node_modules/gatsby/dist/bootstrap/load-themes/node_modules/gatsby-plugin-react-helmet
 - /usr/app/node_modules/gatsby/dist/bootstrap/node_modules/gatsby-plugin-react-helmet
 - /usr/app/node_modules/gatsby/dist/node_modules/gatsby-plugin-react-helmet
 - /usr/app/node_modules/gatsby/node_modules/gatsby-plugin-react-helmet
 - /usr/app/node_modules/gatsby-plugin-react-helmet
 - /usr/node_modules/gatsby-plugin-react-helmet
 - /node_modules/gatsby-plugin-react-helmet
not finished open and validate gatsby-configs - 0.202s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants