MCPcopy
hub / github.com/tailwindlabs/tailwindcss / rewriteUrls

Function rewriteUrls

packages/@tailwindcss-node/src/urls.ts:37–93  ·  view source on GitHub ↗
({
  css,
  base,
  root,
}: {
  css: string
  base: string
  root: string
})

Source from the content-addressed store, hash-verified

35}
36
37export async function rewriteUrls({
38 css,
39 base,
40 root,
41}: {
42 css: string
43 base: string
44 root: string
45}) {
46 if (!css.includes('url(') && !css.includes('image-set(')) {
47 return css
48 }
49
50 let ast = parse(css)
51
52 let promises: Promise<void>[] = []
53
54 function replacerForDeclaration(url: string) {
55 if (url[0] === '/') return url
56
57 let absoluteUrl = path.posix.join(normalizePath(base), url)
58 let relativeUrl = path.posix.relative(normalizePath(root), absoluteUrl)
59
60 // If the path points to a file in the same directory, `path.relative` will
61 // remove the leading `./` and we need to add it back in order to still
62 // consider the path relative
63 if (!relativeUrl.startsWith('.')) {
64 relativeUrl = './' + relativeUrl
65 }
66
67 return relativeUrl
68 }
69
70 walk(ast, (node) => {
71 if (node.kind !== 'declaration') return
72 if (!node.value) return
73
74 let isCssUrl = cssUrlRE.test(node.value)
75 let isCssImageSet = cssImageSetRE.test(node.value)
76
77 if (isCssUrl || isCssImageSet) {
78 let rewriterToUse = isCssImageSet ? rewriteCssImageSet : rewriteCssUrls
79
80 promises.push(
81 rewriterToUse(node.value, replacerForDeclaration).then((url) => {
82 node.value = url
83 }),
84 )
85 }
86 })
87
88 if (promises.length) {
89 await Promise.all(promises)
90 }
91
92 return toCss(ast)
93}
94

Callers 2

urls.test.tsFile · 0.90
loadStylesheetFunction · 0.90

Calls 3

parseFunction · 0.90
walkFunction · 0.90
toCssFunction · 0.90

Tested by

no test coverage detected