|
| 1 | +import { uniqBy, List } from "lodash" |
| 2 | +import { IGatsbyState } from "../redux/types" |
| 3 | +import { Stats } from "webpack" |
| 4 | + |
| 5 | +interface ICompilation { |
| 6 | + modules: IModule[] |
| 7 | +} |
| 8 | + |
| 9 | +interface IReason extends Omit<Stats.Reason, "module"> { |
| 10 | + module: IModule |
| 11 | +} |
| 12 | + |
| 13 | +interface IModule extends Omit<Stats.FnModules, "identifier" | "reasons"> { |
| 14 | + hasReasons: () => boolean |
| 15 | + resource?: string |
| 16 | + identifier: () => string |
| 17 | + reasons: IReason[] |
| 18 | +} |
| 19 | + |
| 20 | +const mapComponentsToStaticQueryHashes = ( |
| 21 | + staticQueryComponents: IGatsbyState["staticQueryComponents"] |
| 22 | +): Map<string, string> => { |
| 23 | + const map = new Map() |
| 24 | + |
| 25 | + staticQueryComponents.forEach(({ componentPath, hash }) => { |
| 26 | + map.set(componentPath, hash) |
| 27 | + }) |
| 28 | + |
| 29 | + return map |
| 30 | +} |
| 31 | + |
| 32 | +/* This function takes the current Redux state and a compilation |
| 33 | + * object from webpack and returns a map of unique templates |
| 34 | + * to static queries included in each (as hashes). |
| 35 | + * |
| 36 | + * This isn't super straightforward because templates may include |
| 37 | + * deep component trees with static queries present at any depth. |
| 38 | + * This is why it is necessary to map templates to all their (user land and node_modules) |
| 39 | + * dependencies first and then map those dependencies to known static queries. |
| 40 | + * |
| 41 | + * Also, Gatsby makes it possible to wrap an entire site or page with a layout |
| 42 | + * or other component(s) via the wrapRootElement and wrapPageElement APIs. These must |
| 43 | + * also be handled when computing static queries for a page. |
| 44 | + * |
| 45 | + * Let's go through the implementation step by step. |
| 46 | + */ |
| 47 | +export function mapTemplatesToStaticQueryHashes( |
| 48 | + reduxState: IGatsbyState, |
| 49 | + compilation: ICompilation |
| 50 | +): Map<string, Array<number>> { |
| 51 | + /* The `staticQueryComponents` slice of state is useful because |
| 52 | + * it is a pre extracted collection of all static queries found in a Gatsby site. |
| 53 | + * This lets us traverse upwards from those to templates that |
| 54 | + * may contain components that contain them. |
| 55 | + * Note that this upward traversal is much shallower (and hence more performant) |
| 56 | + * than an equivalent downward one from an entry point. |
| 57 | + */ |
| 58 | + const { components, staticQueryComponents } = reduxState |
| 59 | + const { modules } = compilation |
| 60 | + |
| 61 | + /* When we traverse upwards, we need to know where to stop. We'll call these terminal nodes. |
| 62 | + * `async-requires.js` is the entry point for every page, while `api-runner-browser-plugins.js` |
| 63 | + * is the one for `gatsby-browser` (where one would use wrapRootElement or wrapPageElement APIs) |
| 64 | + */ |
| 65 | + const terminalNodes = [ |
| 66 | + `.cache/api-runner-browser-plugins.js`, |
| 67 | + `.cache/async-requires.js`, |
| 68 | + ] |
| 69 | + |
| 70 | + /* We call the queries included above a page (via wrapRootElement or wrapPageElement APIs) |
| 71 | + * global queries. For now, we include these in every single page for simplicity. Overhead |
| 72 | + * here is not much since we are storing hashes (that reference separate result files) |
| 73 | + * as opposed to inlining results. We may move these to app-data perhaps in the future. |
| 74 | + */ |
| 75 | + const globalStaticQueries = new Set<string>() |
| 76 | + |
| 77 | + /* This function takes a webpack module corresponding |
| 78 | + * to the file containing a static query and returns |
| 79 | + * a Set of strings, each an absolute path of a dependent |
| 80 | + * of this module |
| 81 | + */ |
| 82 | + const getDeps = (mod: IModule): Set<string> => { |
| 83 | + const staticQueryModuleComponentPath = mod.resource |
| 84 | + const result = new Set<string>() |
| 85 | + |
| 86 | + // This is the body of the recursively called function |
| 87 | + const getDepsFn = (m: IModule): Set<string> => { |
| 88 | + // Reasons in webpack are literally reasons of why this module was included in the tree |
| 89 | + const hasReasons = m.hasReasons() |
| 90 | + |
| 91 | + // Is this node one of our known terminal nodes? See explanation above |
| 92 | + const isTerminalNode = terminalNodes.some(terminalNode => |
| 93 | + m?.resource?.includes(terminalNode) |
| 94 | + ) |
| 95 | + |
| 96 | + // Exit if we don't have any reasons or we have reached a possible terminal node |
| 97 | + if (!hasReasons || isTerminalNode) { |
| 98 | + return result |
| 99 | + } |
| 100 | + |
| 101 | + // These are non terminal dependents and hence modules that need |
| 102 | + // further upward traversal |
| 103 | + const nonTerminalDependents: List<IModule> = m.reasons |
| 104 | + .filter(r => { |
| 105 | + const dependentModule = r.module |
| 106 | + const isTerminal = terminalNodes.some(terminalNode => |
| 107 | + dependentModule?.resource?.includes(terminalNode) |
| 108 | + ) |
| 109 | + return !isTerminal |
| 110 | + }) |
| 111 | + .map(r => r.module) |
| 112 | + .filter(Boolean) |
| 113 | + |
| 114 | + const uniqDependents = uniqBy(nonTerminalDependents, d => d?.identifier()) |
| 115 | + |
| 116 | + for (const uniqDependent of uniqDependents) { |
| 117 | + if (uniqDependent.resource) { |
| 118 | + result.add(uniqDependent.resource) |
| 119 | + } |
| 120 | + |
| 121 | + if ( |
| 122 | + uniqDependent?.resource?.includes(`gatsby-browser.js`) && |
| 123 | + staticQueryModuleComponentPath |
| 124 | + ) { |
| 125 | + globalStaticQueries.add(staticQueryModuleComponentPath) |
| 126 | + } |
| 127 | + getDepsFn(uniqDependent) |
| 128 | + } |
| 129 | + |
| 130 | + return result |
| 131 | + } |
| 132 | + |
| 133 | + return getDepsFn(mod) |
| 134 | + } |
| 135 | + |
| 136 | + const mapOfStaticQueryComponentsToDependants = new Map() |
| 137 | + |
| 138 | + // For every known static query, we get its dependents. |
| 139 | + staticQueryComponents.forEach(({ componentPath }) => { |
| 140 | + const staticQueryComponentModule = modules.find( |
| 141 | + m => m.resource === componentPath |
| 142 | + ) |
| 143 | + |
| 144 | + const dependants = staticQueryComponentModule |
| 145 | + ? getDeps(staticQueryComponentModule) |
| 146 | + : new Set() |
| 147 | + |
| 148 | + mapOfStaticQueryComponentsToDependants.set(componentPath, dependants) |
| 149 | + }) |
| 150 | + |
| 151 | + const mapOfComponentsToStaticQueryHashes = mapComponentsToStaticQueryHashes( |
| 152 | + staticQueryComponents |
| 153 | + ) |
| 154 | + |
| 155 | + const globalStaticQueryHashes: string[] = [] |
| 156 | + |
| 157 | + globalStaticQueries.forEach(q => { |
| 158 | + const hash = mapOfComponentsToStaticQueryHashes.get(q) |
| 159 | + if (hash) { |
| 160 | + globalStaticQueryHashes.push(hash) |
| 161 | + } |
| 162 | + }) |
| 163 | + |
| 164 | + // For every known page, we get queries |
| 165 | + const mapOfTemplatesToStaticQueryHashes = new Map() |
| 166 | + |
| 167 | + components.forEach(page => { |
| 168 | + const staticQueryHashes = [...globalStaticQueryHashes] |
| 169 | + |
| 170 | + // Does this page contain an inline static query? |
| 171 | + if (mapOfComponentsToStaticQueryHashes.has(page.componentPath)) { |
| 172 | + const hash = mapOfComponentsToStaticQueryHashes.get(page.componentPath) |
| 173 | + if (hash) { |
| 174 | + staticQueryHashes.push(hash) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Check dependencies |
| 179 | + mapOfStaticQueryComponentsToDependants.forEach( |
| 180 | + (setOfDependants, staticQueryComponentPath) => { |
| 181 | + if (setOfDependants.has(page.componentPath)) { |
| 182 | + const hash = mapOfComponentsToStaticQueryHashes.get( |
| 183 | + staticQueryComponentPath |
| 184 | + ) |
| 185 | + if (hash) { |
| 186 | + staticQueryHashes.push(hash) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + ) |
| 191 | + |
| 192 | + mapOfTemplatesToStaticQueryHashes.set( |
| 193 | + page.componentPath, |
| 194 | + staticQueryHashes.sort() |
| 195 | + ) |
| 196 | + }) |
| 197 | + |
| 198 | + return mapOfTemplatesToStaticQueryHashes |
| 199 | +} |
0 commit comments