UNPKG

gatsby-plugin-gatsby-cloud

Version:
125 lines (120 loc) 4.91 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _fsExtra = require("fs-extra"); var _webpackAssetsManifest = _interopRequireDefault(require("webpack-assets-manifest")); var _gatsbyCoreUtils = require("gatsby-core-utils"); var _pluginData = _interopRequireDefault(require("./plugin-data")); var _buildHeadersProgram = _interopRequireDefault(require("./build-headers-program")); var _copyFunctionsManifest = _interopRequireDefault(require("./copy-functions-manifest")); var _createRedirects = _interopRequireDefault(require("./create-redirects")); var _createSiteConfig = _interopRequireDefault(require("./create-site-config")); var _constants = require("./constants"); var _ipc = require("./ipc"); const assetsManifest = {}; process.env.GATSBY_PREVIEW_INDICATOR_ENABLED = process.env.GATSBY_PREVIEW_INDICATOR_ENABLED || `false`; // Inject a webpack plugin to get the file manifests so we can translate all link headers exports.onCreateWebpackConfig = ({ actions, stage }) => { if (stage === _constants.BUILD_HTML_STAGE) { return; } actions.setWebpackConfig({ plugins: [new _webpackAssetsManifest.default({ assets: assetsManifest, // mutates object with entries merge: true })] }); }; exports.onPostBuild = async ({ store }, userPluginOptions) => { const pluginOptions = { ..._constants.DEFAULT_OPTIONS, ...userPluginOptions }; const { redirects, pages } = store.getState(); const pluginData = (0, _pluginData.default)(store, assetsManifest); /** * Emit via IPC routes for which pages are non SSG */ let index = 0; let batch = {}; for (const [pathname, page] of pages) { if (page.mode && page.mode !== `SSG`) { index++; const fullPathName = page.matchPath ? page.matchPath : pathname; batch[(0, _gatsbyCoreUtils.generateHtmlPath)(``, fullPathName)] = page.mode; batch[(0, _gatsbyCoreUtils.generatePageDataPath)(``, fullPathName)] = page.mode; if (index % 1000 === 0) { await (0, _ipc.emitRoutes)(batch); batch = {}; } } } if (Object.keys(batch).length > 0) { await (0, _ipc.emitRoutes)(batch); } let rewrites = []; if (pluginOptions.generateMatchPathRewrites) { const matchPathsFile = (0, _gatsbyCoreUtils.joinPath)(pluginData.program.directory, `.cache`, `match-paths.json`); const matchPaths = await (0, _fsExtra.readJSON)(matchPathsFile); rewrites = matchPaths.map(({ matchPath, path }) => { return { fromPath: matchPath, toPath: path }; }); } await Promise.all([ensureEmittingFileNodesFinished, (0, _buildHeadersProgram.default)(pluginData, pluginOptions), (0, _createSiteConfig.default)(pluginData, pluginOptions), (0, _createRedirects.default)(pluginData, redirects, rewrites), (0, _copyFunctionsManifest.default)(pluginData), (0, _ipc.emitTotalRenderedPageCount)(pages.size)]); }; const MATCH_ALL_KEYS = /^/; const pluginOptionsSchema = function ({ Joi }) { const headersSchema = Joi.object().pattern(MATCH_ALL_KEYS, Joi.array().items(Joi.string())).description(`Add more headers to specific pages`); return Joi.object({ headers: headersSchema, allPageHeaders: Joi.array().items(Joi.string()).description(`Add more headers to all the pages`), mergeSecurityHeaders: Joi.boolean().description(`When set to false, turns off the default security headers`), mergeLinkHeaders: Joi.boolean().description(`When set to false, turns off the default gatsby js headers`), mergeCachingHeaders: Joi.boolean().description(`When set to false, turns off the default caching headers`), transformHeaders: Joi.function().maxArity(2).description(`Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array<string> }`), generateMatchPathRewrites: Joi.boolean().description(`When set to false, turns off automatic creation of redirect rules for client only paths`), disablePreviewUI: Joi.boolean().description(`When set to true, turns off Gatsby Preview if enabled`) }); }; exports.pluginOptionsSchema = pluginOptionsSchema; /** * We emit File Nodes via IPC and we need to make sure build doesn't finish before all of * messages were sent. */ let ensureEmittingFileNodesFinished; exports.onPreBootstrap = ({ emitter, getNodesByType }) => { emitter.on(`API_FINISHED`, action => { if (action.payload.apiName !== `sourceNodes`) { return; } async function doEmitFileNodes() { const fileNodes = getNodesByType(`File`); // TODO: This is missing the cacheLocations .cache/caches + .cache/caches-lmdb for (const file of fileNodes) { await (0, _ipc.emitFileNodes)({ path: file.absolutePath }); } } ensureEmittingFileNodesFinished = doEmitFileNodes(); }); };