Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/tests/transformers/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ describe('base URL', () => {
const result = run(html, { url: { base: 'https://cdn.example.com/' } })
expect(result).toBe(html)
})

it('prepends a relative base to url() exactly once (no infinite re-prepend)', () => {
// A relative base never makes the URL absolute; the PostCSS declaration
// must not be re-visited, or it re-prepends forever and hangs.
const html = '<style>.bg { background-image: url(bg.jpg) }</style>'
const result = run(html, { url: { base: '/images/' } })
expect(result).toContain('url(/images/bg.jpg)')
expect(result).not.toContain('/images//images/')
})

it('prepends a relative base in an inline style exactly once', () => {
const html = '<div style="background-image: url(bg.jpg)"></div>'
const result = run(html, { url: { base: '/images/' } })
expect(result).toContain('url(/images/bg.jpg)')
expect(result).not.toContain('/images//images/')
})
})

describe('inlineCss option', () => {
Expand Down
46 changes: 29 additions & 17 deletions src/transformers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,43 @@ const defaultTagConfig: Record<string, Record<string, string | boolean>> = Objec
)

const postcssBaseUrl: postcss.PluginCreator<{ url: string }> = (opts) => {
return {
postcssPlugin: 'postcss-base-url',
Declaration(decl) {
if (!decl.value.includes('url(')) return
const rewriteDecl = (decl: postcss.Declaration) => {
if (!decl.value.includes('url(')) return

const parsed = valueParser(decl.value)
let changed = false
const parsed = valueParser(decl.value)
let changed = false

parsed.walk(node => {
if (node.type !== 'function' || node.value !== 'url') return
parsed.walk(node => {
if (node.type !== 'function' || node.value !== 'url') return

const urlNode = node.nodes[0]
if (!urlNode) return
const urlNode = node.nodes[0]
if (!urlNode) return

if (isAbsoluteUrl(urlNode.value)) return
if (isAbsoluteUrl(urlNode.value)) return

urlNode.value = opts!.url + urlNode.value
changed = true
})
urlNode.value = opts!.url + urlNode.value
changed = true
})

if (changed) {
decl.value = parsed.toString()
}
if (changed) {
decl.value = parsed.toString()
}
}

return {
postcssPlugin: 'postcss-base-url',
/**
* Walk declarations once, in `OnceExit`, rather than via the visitor
* `Declaration` hook. The visitor re-runs on nodes it mutates, and a
* relative base (e.g. `/images/`) never makes the URL absolute — so the
* guard `isAbsoluteUrl` stays false and it re-prepends the base forever
* (`/images//images/…`), hanging the build. A single manual pass rewrites
* each declaration exactly once.
*/
OnceExit(root) {
root.walkDecls(rewriteDecl)
},
}
}
postcssBaseUrl.postcss = true

Expand Down
Loading