Files
30-seconds-of-code/node_modules/gatsby/dist/bootstrap/load-plugins/index.js
2019-08-20 15:52:05 +02:00

83 lines
2.2 KiB
JavaScript

"use strict";
const _ = require(`lodash`);
const {
store
} = require(`../../redux`);
const nodeAPIs = require(`../../utils/api-node-docs`);
const browserAPIs = require(`../../utils/api-browser-docs`);
const ssrAPIs = require(`../../../cache-dir/api-ssr-docs`);
const loadPlugins = require(`./load`);
const {
collatePluginAPIs,
handleBadExports,
handleMultipleReplaceRenderers
} = require(`./validate`);
const getAPI = api => _.keys(api).reduce((merged, key) => {
merged[key] = _.keys(api[key]);
return merged;
}, {}); // Create a "flattened" array of plugins with all subplugins
// brought to the top-level. This simplifies running gatsby-* files
// for subplugins.
const flattenPlugins = plugins => {
const flattened = [];
const extractPlugins = plugin => {
plugin.pluginOptions.plugins.forEach(subPlugin => {
flattened.push(subPlugin);
extractPlugins(subPlugin);
});
};
plugins.forEach(plugin => {
flattened.push(plugin);
extractPlugins(plugin);
});
return flattened;
};
module.exports = async (config = {}, rootDir = null, latestAPIs = {}) => {
const apis = {
currentAPIs: getAPI({
browser: browserAPIs,
node: nodeAPIs,
ssr: ssrAPIs
}),
latestAPIs // Collate internal plugins, site config plugins, site default plugins
};
const plugins = loadPlugins(config, rootDir); // Create a flattened array of the plugins
let flattenedPlugins = flattenPlugins(plugins); // Work out which plugins use which APIs, including those which are not
// valid Gatsby APIs, aka 'badExports'
const x = collatePluginAPIs(Object.assign({}, apis, {
flattenedPlugins
}));
flattenedPlugins = x.flattenedPlugins;
const badExports = x.badExports; // Show errors for any non-Gatsby APIs exported from plugins
handleBadExports(Object.assign({}, apis, {
badExports
})); // Show errors when ReplaceRenderer has been implemented multiple times
flattenedPlugins = handleMultipleReplaceRenderers({
flattenedPlugins
}); // If we get this far, everything looks good. Update the store
store.dispatch({
type: `SET_SITE_FLATTENED_PLUGINS`,
payload: flattenedPlugins
});
return flattenedPlugins;
};
//# sourceMappingURL=index.js.map