From 98328370bbce333abfc583f6f3d1f5aaaae1c182 Mon Sep 17 00:00:00 2001 From: Martin Campbell Date: Wed, 21 Nov 2018 13:02:38 +0000 Subject: [PATCH] fix(gatsby-plugin-sharp): use actions provided, don't assume Gatsby module location. (#9986) The gatsby module resolve by require might not be the gatsby module that is executing. This can occur in mono-repo configurations. This will prevent the Redux store from receiving the job actions and the bootstrap process will complete without waiting for all images to be processed. This can cause the build to finish without all images being generated. Fixes #9984. --- packages/gatsby-plugin-sharp/src/gatsby-node.js | 6 ++++++ packages/gatsby-plugin-sharp/src/index.js | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-sharp/src/gatsby-node.js b/packages/gatsby-plugin-sharp/src/gatsby-node.js index aaf6b34ae98f7..98c258030c6e4 100644 --- a/packages/gatsby-plugin-sharp/src/gatsby-node.js +++ b/packages/gatsby-plugin-sharp/src/gatsby-node.js @@ -1,3 +1,9 @@ +const { setBoundActionCreators } = require(`./index`) + +exports.onPreInit = ({ actions }) => { + setBoundActionCreators(actions) +} + // TODO // exports.formatJobMessage = jobs => { // return { diff --git a/packages/gatsby-plugin-sharp/src/index.js b/packages/gatsby-plugin-sharp/src/index.js index cda7bf9b51548..baf2d06aed4d5 100644 --- a/packages/gatsby-plugin-sharp/src/index.js +++ b/packages/gatsby-plugin-sharp/src/index.js @@ -30,7 +30,16 @@ const getImageSize = file => { } const duotone = require(`./duotone`) -const { boundActionCreators } = require(`gatsby/dist/redux/actions`) + +// Bound action creators should be set when passed to onPreInit in gatsby-node. +// ** It is NOT safe to just directly require the gatsby module **. +// There is no guarantee that the module resolved is the module executing! +// This can occur in mono repos depending on how dependencies have been hoisted. +// The direct require has been left only to avoid breaking changes. +let { boundActionCreators } = require(`gatsby/dist/redux/actions`) +exports.setBoundActionCreators = actions => { + boundActionCreators = actions +} // Promisify the sharp prototype (methods) to promisify the alternative (for // raw) callback-accepting toBuffer(...) method