UNPKG

gatsby-plugin-cloudflare-pages

Version:
105 lines (104 loc) 5.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.onPostBuild = exports.onCreateWebpackConfig = exports.pluginOptionsSchema = void 0; const path_1 = require("path"); const fs_extra_1 = require("fs-extra"); // import { generatePageDataPath } from 'gatsby-core-utils' const webpack_assets_manifest_1 = __importDefault(require("webpack-assets-manifest")); const build_headers_program_1 = __importDefault(require("./build-headers-program")); const constants_1 = require("./constants"); const create_redirects_1 = __importDefault(require("./create-redirects")); const plugin_data_1 = __importDefault(require("./plugin-data")); const assetsManifest = {}; const pluginOptionsSchema = ({ Joi }) => { const MATCH_ALL_KEYS = /^/; const headersSchema = Joi.object() .pattern(MATCH_ALL_KEYS, Joi.array().items(Joi.string())) .description(`Add more HTTP 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`), 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`), }); }; exports.pluginOptionsSchema = pluginOptionsSchema; // Inject a webpack plugin to get the file manifests so we can translate all link headers const onCreateWebpackConfig = ({ actions, stage }) => { // We only need to get manifest for production browser bundle if (stage !== constants_1.BUILD_BROWSER_BUNDLE_STAGE) { return; } actions.setWebpackConfig({ plugins: [ new webpack_assets_manifest_1.default({ // mutates object with entries assets: assetsManifest, merge: true, }), ], }); }; exports.onCreateWebpackConfig = onCreateWebpackConfig; const onPostBuild = async ({ store, pathPrefix, reporter }, userPluginOptions) => { const pluginData = (0, plugin_data_1.default)(store, assetsManifest, pathPrefix); const pluginOptions = { ...constants_1.DEFAULT_OPTIONS, ...userPluginOptions }; const { redirects, pages, functions = [], program } = store.getState(); if (pages.size > constants_1.PAGE_COUNT_WARN && pluginOptions.mergeCachingHeaders) { reporter.warn(`[gatsby-plugin-cloudflare-pages] Your site has ${pages.size} pages, which means that the generated headers file could become very large. Consider disabling "mergeCachingHeaders" in your plugin config`); } reporter.info(`[gatsby-plugin-cloudflare-pages] Creating SSR/DSG redirects...`); let count = 0; const rewrites = []; const neededFunctions = { API: functions.length !== 0, SSR: false, DSG: false, }; [...pages.values()].forEach((page) => { const { mode, matchPath, path } = page; const matchPathClean = matchPath && matchPath.replace(/\*.*/, '*'); const matchPathIsNotPath = matchPath && matchPath !== path; if (mode === `SSR` || mode === `DSG`) { throw new Error(`[gatsby-plugin-cloudflare-pages] Found ${mode} page at ${path}. This is not supported by Cloudflare Pages.`); // neededFunctions[mode] = true // const fromPath = matchPathClean ?? path // const toPath = mode === `SSR` ? `/.netlify/functions/__ssr` : `/.netlify/functions/__dsg` // count++ // rewrites.push( // { // fromPath, // toPath, // }, // { // fromPath: generatePageDataPath(`/`, fromPath), // toPath, // }, // ) } else if (pluginOptions.generateMatchPathRewrites && matchPathIsNotPath) { rewrites.push({ fromPath: matchPathClean, toPath: path, }); } }); reporter.info(`[gatsby-plugin-cloudflare-pages] Created ${count} SSR/DSG redirect${count === 1 ? `` : `s`}...`); const skipFilePath = (0, path_1.join)(program.directory, `.cache`, `.nf-skip-gatsby-functions`); const generateSkipFile = Object.values(neededFunctions).includes(false) ? (0, fs_extra_1.writeJson)(skipFilePath, neededFunctions) : (0, fs_extra_1.remove)(skipFilePath); await Promise.all([ generateSkipFile, (0, build_headers_program_1.default)(pluginData, pluginOptions, reporter), (0, create_redirects_1.default)(pluginData, redirects, rewrites, reporter), ]); }; exports.onPostBuild = onPostBuild;