Home / Function/ rewriteUrls() — tailwindcss Function Reference

rewriteUrls() — tailwindcss Function Reference

Architecture documentation for the rewriteUrls() function in urls.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9["rewriteUrls()"]
  52f67804_5329_acb6_2ee6_ce1634fdb996["createCompileOptions()"]
  52f67804_5329_acb6_2ee6_ce1634fdb996 -->|calls| fe420c64_fc5d_8de5_0c9f_c3e614f856a9
  253418a1_4f08_cf0e_5b8e_c6392b9959eb["parse()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9 -->|calls| 253418a1_4f08_cf0e_5b8e_c6392b9959eb
  d00d78c4_d3a9_d8f8_31c1_110fc660f105["normalizePath()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9 -->|calls| d00d78c4_d3a9_d8f8_31c1_110fc660f105
  a32bba76_f60d_883f_1ff1_276a0bb9db9f["walk()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9 -->|calls| a32bba76_f60d_883f_1ff1_276a0bb9db9f
  e7a34553_0273_6202_4792_07409e33d8f0["toCss()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  style fe420c64_fc5d_8de5_0c9f_c3e614f856a9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-node/src/urls.ts lines 37–93

export async function rewriteUrls({
  css,
  base,
  root,
}: {
  css: string
  base: string
  root: string
}) {
  if (!css.includes('url(') && !css.includes('image-set(')) {
    return css
  }

  let ast = parse(css)

  let promises: Promise<void>[] = []

  function replacerForDeclaration(url: string) {
    if (url[0] === '/') return url

    let absoluteUrl = path.posix.join(normalizePath(base), url)
    let relativeUrl = path.posix.relative(normalizePath(root), absoluteUrl)

    // If the path points to a file in the same directory, `path.relative` will
    // remove the leading `./` and we need to add it back in order to still
    // consider the path relative
    if (!relativeUrl.startsWith('.')) {
      relativeUrl = './' + relativeUrl
    }

    return relativeUrl
  }

  walk(ast, (node) => {
    if (node.kind !== 'declaration') return
    if (!node.value) return

    let isCssUrl = cssUrlRE.test(node.value)
    let isCssImageSet = cssImageSetRE.test(node.value)

    if (isCssUrl || isCssImageSet) {
      let rewriterToUse = isCssImageSet ? rewriteCssImageSet : rewriteCssUrls

      promises.push(
        rewriterToUse(node.value, replacerForDeclaration).then((url) => {
          node.value = url
        }),
      )
    }
  })

  if (promises.length) {
    await Promise.all(promises)
  }

  return toCss(ast)
}

Domain

Subdomains

Frequently Asked Questions

What does rewriteUrls() do?
rewriteUrls() is a function in the tailwindcss codebase.
What does rewriteUrls() call?
rewriteUrls() calls 4 function(s): normalizePath, parse, toCss, walk.
What calls rewriteUrls()?
rewriteUrls() is called by 1 function(s): createCompileOptions.

Analyze Your Own Codebase

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

Try Supermodel Free