Repository: tailwindlabs/tailwindcss
File: packages/tailwindcss/src/apply.ts (TypeScript)

Code before:
  for (let parent of sorted) {
    if (!('nodes' in parent)) continue

    for (let i = 0; i < parent.nodes.length; i++) {
      let node = parent.nodes[i]
      if (node.kind !== 'at-rule' || node.name !== '@apply') continue

      let candidates = node.params.split(/\s+/g)

      // Replace the `@apply` rule with the actual utility classes
      {
        // Parse the candidates to an AST that we can replace the `@apply` rule
        // with.
        let candidateAst
        try {
          candidateAst = compileCandidates(candidates, designSystem, {
            onInvalidCandidate: (candidate) => {
              throw new Error(`Cannot apply unknown utility class: ${candidate}`)
            },
          }).astNodes
        } catch (error: any) {
          // Error recovery: If we can't compile the candidates, just skip this @apply rule
          // This is a better user experience than getting stuck in an error state
          console.error(error)
          parent.nodes.splice(i, 1)
          i--
          continue
        }

        // Collect the nodes to insert in place of the `@apply` rule. When a rule
        // was used, we want to insert its children instead of the rule because we
        // don't want the wrapping selector.
        let newNodes: AstNode[] = []
        for (let candidateNode of candidateAst) {
          if (candidateNode.kind === 'rule') {
            for (let child of candidateNode.nodes) {
              newNodes.push(child)
            }
          } else {
            newNodes.push(candidateNode)
          }
        }

        parent.nodes.splice(i, 1, ...newNodes)
      }
    }
  }
  return features
}

function* resolveApplyDependencies(

Review comment (suggestion):
An invalid `@apply` will require a full rebuild I think right. So what does this particular code solve?

Is it in case you are importing some other CSS where you don't have control over what is being `@apply`'d and you just skip it?

I guess that's better indeed, so _some_ stuff still works. But the current rule will still be invalid and not what you expect I think 🤔 

Code after:
  for (let parent of sorted) {
    if (!('nodes' in parent)) continue

    for (let i = 0; i < parent.nodes.length; i++) {
      let node = parent.nodes[i]
      if (node.kind !== 'at-rule' || node.name !== '@apply') continue

      let candidates = node.params.split(/\s+/g)

      // Replace the `@apply` rule with the actual utility classes
      {
        // Parse the candidates to an AST that we can replace the `@apply` rule
        // with.
        let candidateAst = compileCandidates(candidates, designSystem, {
          onInvalidCandidate: (candidate) => {
            throw new Error(`Cannot apply unknown utility class: ${candidate}`)
          },
        }).astNodes

        // Collect the nodes to insert in place of the `@apply` rule. When a rule
        // was used, we want to insert its children instead of the rule because we
        // don't want the wrapping selector.
        let newNodes: AstNode[] = []
        for (let candidateNode of candidateAst) {
          if (candidateNode.kind === 'rule') {
            for (let child of candidateNode.nodes) {
              newNodes.push(child)
            }
          } else {
            newNodes.push(candidateNode)
          }
        }

        parent.nodes.splice(i, 1, ...newNodes)
      }
    }
  }
  return features
}

function* resolveApplyDependencies(
  node: Extract<AstNode, { kind: 'at-rule' }>,
  designSystem: DesignSystem,
) {
  for (let candidate of node.params.split(/\s+/g)) {
    for (let node of designSystem.parseCandidate(candidate)) {
      switch (node.kind) {
        case 'arbitrary':
          // Doesn't matter, because there is no lookup needed
          break