Home / Function/ rehypeOptimizeStatic() — astro Function Reference

rehypeOptimizeStatic() — astro Function Reference

Architecture documentation for the rehypeOptimizeStatic() function in rehype-optimize-static.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384["rehypeOptimizeStatic()"]
  36422914_2d7c_adeb_c777_76a007e3cb87["rehype-optimize-static.ts"]
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 -->|defined in| 36422914_2d7c_adeb_c777_76a007e3cb87
  615fc232_626d_b894_3598_dd4e7bbb6051["getExportConstComponentObjectKeys()"]
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 -->|calls| 615fc232_626d_b894_3598_dd4e7bbb6051
  5c90d957_b7b9_18dc_7085_ecc7ecd641ba["simplifyPlainMdxComponentNode()"]
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 -->|calls| 5c90d957_b7b9_18dc_7085_ecc7ecd641ba
  893ecef5_14a8_82b0_1724_42530072391e["isMdxComponentNode()"]
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 -->|calls| 893ecef5_14a8_82b0_1724_42530072391e
  101ea6c0_f1fc_8678_855e_10f17e6047e5["findElementGroups()"]
  d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 -->|calls| 101ea6c0_f1fc_8678_855e_10f17e6047e5
  style d6e3dc2a_c11c_d2d3_2b99_f6aee60fd384 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/mdx/src/rehype-optimize-static.ts lines 33–175

export const rehypeOptimizeStatic: RehypePlugin<[OptimizeOptions?]> = (options) => {
	return (tree) => {
		// A set of non-static components to avoid collapsing when walking the tree
		// as they need to be preserved as JSX to be rendered dynamically.
		const ignoreElementNames = new Set<string>(options?.ignoreElementNames);

		// Find `export const components = { ... }` and get it's object's keys to be
		// populated into `ignoreElementNames`. This configuration is used to render
		// some HTML elements as custom components, and we also want to avoid collapsing them.
		for (const child of tree.children) {
			if (child.type === 'mdxjsEsm' && exportConstComponentsRe.test(child.value)) {
				const keys = getExportConstComponentObjectKeys(child);
				if (keys) {
					for (const key of keys) {
						ignoreElementNames.add(key);
					}
				}
				break;
			}
		}

		// All possible elements that could be the root of a subtree
		const allPossibleElements = new Set<OptimizableNode>();
		// The current collapsible element stack while traversing the tree
		const elementStack: Node[] = [];
		// Metadata used by `findElementGroups` later
		const elementMetadatas = new WeakMap<OptimizableNode, ElementMetadata>();

		/**
		 * A non-static node causes all its parents to be non-optimizable
		 */
		const isNodeNonStatic = (node: Node) => {
			return (
				node.type.startsWith('mdx') ||
				// @ts-expect-error `node` should never have `type: 'root'`, but in some cases plugins may inject it as children,
				// which MDX will render as a fragment instead (an MDX fragment is a `mdxJsxFlowElement` type).
				node.type === 'root' ||
				// @ts-expect-error Access `.tagName` naively for perf
				ignoreElementNames.has(node.tagName)
			);
		};

		visit(tree as any, {
			// @ts-expect-error Force coerce node as hast node
			enter(node: Node, key, index, parents: ParentNode[]) {
				// `estree-util-visit` may traverse in MDX `attributes`, we don't want that. Only continue
				// if it's traversing the root, or the `children` key.
				if (key != null && key !== 'children') return SKIP;

				// Mutate `node` as a normal hast element node if it's a plain MDX node, e.g. `<kbd>something</kbd>`
				simplifyPlainMdxComponentNode(node, ignoreElementNames);

				// For nodes that are not static, eliminate all elements in the `elementStack` from the
				// `allPossibleElements` set.
				if (isNodeNonStatic(node)) {
					for (const el of elementStack) {
						allPossibleElements.delete(el as OptimizableNode);
					}
					// Micro-optimization: While this destroys the meaning of an element
					// stack for this node, things will still work but we won't repeatedly
					// run the above for other nodes anymore. If this is confusing, you can
					// comment out the code below when reading.
					elementStack.length = 0;
				}
				// For possible subtree root nodes, record them in `elementStack` and
				// `allPossibleElements` to be used in the "leave" hook below.
				if (node.type === 'element' || isMdxComponentNode(node)) {
					elementStack.push(node);
					allPossibleElements.add(node);

					if (index != null && node.type === 'element') {
						// Record metadata for element node to be used for grouping analysis later
						elementMetadatas.set(node, { parent: parents[parents.length - 1], index });
					}
				}
			},
			// @ts-expect-error Force coerce node as hast node
			leave(node: Node, key, _, parents: ParentNode[]) {
				// `estree-util-visit` may traverse in MDX `attributes`, we don't want that. Only continue
				// if it's traversing the root, or the `children` key.
				if (key != null && key !== 'children') return SKIP;

Domain

Subdomains

Frequently Asked Questions

What does rehypeOptimizeStatic() do?
rehypeOptimizeStatic() is a function in the astro codebase, defined in packages/integrations/mdx/src/rehype-optimize-static.ts.
Where is rehypeOptimizeStatic() defined?
rehypeOptimizeStatic() is defined in packages/integrations/mdx/src/rehype-optimize-static.ts at line 33.
What does rehypeOptimizeStatic() call?
rehypeOptimizeStatic() calls 4 function(s): findElementGroups, getExportConstComponentObjectKeys, isMdxComponentNode, simplifyPlainMdxComponentNode.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free