在本文中,我們分析了stituteAtApply中拋出的錯誤。此錯誤與偵測到的循環依賴有關。
walk(rule.nodes, (child) => { if (child !== node) return throw new Error( `You cannot \`@apply\` the \`${candidate}\` utility here because it creates a circular dependency.`, ) })
這是圍繞此錯誤的程式碼的高級概述。
讓我們從步行開始:
export function walk( ast: AstNode[], visit: ( node: AstNode, utils: { parent: AstNode | null replaceWith(newNode: AstNode | AstNode[]): void context: Record<string, string> }, ) => void | WalkAction, parent: AstNode | null = null, context: Record<string, string> = {}, ) { for (let i = 0; i < ast.length; i++) { let node = ast[i] // We want context nodes to be transparent in walks. This means that // whenever we encounter one, we immediately walk through its children and // furthermore we also don't update the parent. if (node.kind === 'context') { walk(node.nodes, visit, parent, { …context, …node.context }) continue } let status = visit(node, { parent, replaceWith(newNode) { ast.splice(i, 1, …(Array.isArray(newNode) ? newNode : [newNode])) // We want to visit the newly replaced node(s), which start at the // current index (i). By decrementing the index here, the next loop // will process this position (containing the replaced node) again. i - }, context, }) ?? WalkAction.Continue // Stop the walk entirely if (status === WalkAction.Stop) return // Skip visiting the children of this node if (status === WalkAction.Skip) continue if (node.kind === 'rule') { walk(node.nodes, visit, node, context) } } }
walk 是位於 ast.ts 中的遞迴函數。
當node.kind === ‘context’或當node.kind === ‘rule’時,它會遞歸調用自身,破壞條件基於狀態
// Stop the walk entirely if (status === WalkAction.Stop) return // Skip visiting the children of this node if (status === WalkAction.Skip) continue
現在讓我們縮小一點,研究一下 apply.ts 中 walk 函數附近的程式碼
// Verify that we don't have any circular dependencies by verifying that // the current node does not appear in the new nodes. walk(newNodes, (child) => { if (child !== node) return // At this point we already know that we have a circular dependency. // // Figure out which candidate caused the circular dependency. This will // help to create a useful error message for the end user. for (let candidate of candidates) { let selector = `.${escape(candidate)}` for (let rule of candidateAst) { if (rule.kind !== 'rule') continue if (rule.selector !== selector) continue walk(rule.nodes, (child) => { if (child !== node) return throw new Error( `You cannot \`@apply\` the \`${candidate}\` utility here because it creates a circular dependency.`, ) }) } } })
TailwindCSS 作者在需要時在程式碼庫中添加了解釋性註釋,或提供額外的上下文是有意義的
有評論。
在 Think Throo,我們的使命是教授開源專案中使用的高階程式碼庫架構概念。
透過在 Next.js/React 中練習高階架構概念,將您的編碼技能提高 10 倍,學習最佳實踐並建立生產級專案。
我們是開源的 — https://github.com/thinkthroo/thinkthroo (請給我們一顆星!)
我們也提供網頁開發和技術寫作服務。請透過hello@thinkthroo.com聯絡我們以了解更多資訊!
https://github.com/tailwindlabs/tailwindcss/blob/next/packages/tailwindcss/src/ast.ts#L70
https://github.com/tailwindlabs/tailwindcss/blob/c01b8254e822d4f328674357347ca0532f1283a0/packages/tailwins/s/s>
以上是Tailwind CSS 如何偵測循環依賴。的詳細內容。更多資訊請關注PHP中文網其他相關文章!