Integrate running gatsby in a current node application. This would be like some cli-api.
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.
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.
https://github.com/gatsbyjs/gatsby/blob/a795c102bee9795218eeb63034062b5175f0ba11/packages/gatsby-cli/src/create-cli.js#L176-L200
@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?
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,
})
Awesome. Thank you so much.
feel free to reach out if you need more info.
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?
js
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)
You don't get this error when running gatsby build from the cli?
No, from cli this works as expected.
@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
Most helpful comment
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