Executing Plugins and Injecting Arguments

Once the previous step is complete, the Gatsby bootstrap filters the flattenedPlugins namespace in Redux to yield only the plugins that implement the Gatsby API that needs to be executed. For each successive plugin it encounters, Gatsby will require its gatsby-node.js file and invoke its exported function that implements one of the Gatsby Node APIs. For instance, if the API invoked is sourceNodes, Gatsby will execute gatsbyNode['sourceNodes'](...apiCallArgs).

Once invoked, each API implementation is provided with a range of Gatsby actions and other functions and objects as arguments. Each of these arguments is created whenever a plugin is executed for a designated API, which permits Gatsby to rebind actions with default information for that plugin where necessary. Every action in Gatsby accepts three arguments, as follows:

  • The core piece of information required by the action; for instance, a Node object for the createNode API
  • The plugin invoking this action; for instance, my-plugin, which createNode uses to designate an owner for the new Node object
  • An object with several miscellaneous action options, such as traceId and parentSpan for build tracing

Passing along the full set of plugin options and action options on each and every action invocation would be unrelentingly slow for developers implementing sites or plugins. Because Gatsby is already aware of the plugin as well as traceId and parentSpan when referring to the API, the bootstrap rebinds injected Gatsby actions so that those arguments are already available. This is done by doubleBind in src/utils/api-runner-node.js.

Each plugin is executed within a map-series_Promise, thus permitting them to be run concurrently for performance. After all plugins have been executed, Gatsby removes them from apisRunningById and fires an API_RUNNING_QUEUE_EMPTY event, which results in the re-creation of any unfinished pages and the queries inside. Once this step is complete, the results are returned, allowing the bootstrap to proceed.

Now that we’ve covered how the Gatsby bootstrap handles each individual API and plugin it comes across, let’s zoom in on the build lifecycle, which is the process Gatsby undertakes for each build. To explore this, we’ll dive deeper into some of the APIs that we discussed in this section.


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *