Skip to content
Open
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
3 changes: 2 additions & 1 deletion docs/features/site-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ User drops files / folder / static .zip / CMS bundle .zip
buildAssetPlan(pagePlans, cssFileResults, fileMap, rawStylesheetSources)
│ normalizes url() in node props, HTML attributes, CSS values, raw @keyframes
│ CSS, and kept-stylesheet text to FileMap keys
│ CSS, and kept-stylesheet text to FileMap keys; a key that misses
│ exactly is retried under passthrough/ before being dropped
│ resolves @font-face → ImportFontFamily[]
│ flattens kept stylesheets (mode 'file') → ImportStylesheet[]
│ collects deduplicated asset list
Expand Down
43 changes: 43 additions & 0 deletions src/__tests__/siteImport/assetPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,49 @@ describe('buildAssetPlan — img src normalisation', () => {
expect(assets.some((a) => a.sourcePath === 'images/hero.png')).toBe(true)
})

it('falls back to a passthrough FileMap key for a root-relative img src', () => {
const sourcePath = 'passthrough/wp-content/uploads/x.png'
const fileMap = makeFileMap({
'index.html': { bytes: txt('<html><body><img src="/wp-content/uploads/x.png"></body></html>') },
[sourcePath]: { bytes: MINIMAL_PNG, mimeType: 'image/png' },
})
const { pagePlan } = makeHtmlPagePlan(
'index.html',
new TextDecoder().decode(fileMap.files['index.html']!.bytes),
fileMap,
)
const { normalizedPagePlans, assets } = buildAssetPlan([pagePlan], [], fileMap)

const imageNode = Object.values(normalizedPagePlans[0].nodeFragment.nodes).find(
(node) => node.moduleId === 'base.image',
)
expect(imageNode?.props['src']).toBe(sourcePath)
expect(assets.some((asset) => asset.sourcePath === sourcePath)).toBe(true)
})

it('prefers an exact FileMap key over its passthrough counterpart', () => {
const exactPath = 'wp-content/uploads/x.png'
const passthroughPath = `passthrough/${exactPath}`
const exactBytes = txt('exact')
const fileMap = makeFileMap({
'index.html': { bytes: txt('<html><body><img src="/wp-content/uploads/x.png"></body></html>') },
[exactPath]: { bytes: exactBytes, mimeType: 'image/png' },
[passthroughPath]: { bytes: txt('passthrough'), mimeType: 'image/png' },
})
const { pagePlan } = makeHtmlPagePlan(
'index.html',
new TextDecoder().decode(fileMap.files['index.html']!.bytes),
fileMap,
)
const { normalizedPagePlans, assets } = buildAssetPlan([pagePlan], [], fileMap)

const imageNode = Object.values(normalizedPagePlans[0].nodeFragment.nodes).find(
(node) => node.moduleId === 'base.image',
)
expect(imageNode?.props['src']).toBe(exactPath)
expect(assets.find((asset) => asset.sourcePath === exactPath)?.bytes).toBe(exactBytes)
})

it('leaves external URLs unchanged', () => {
const fileMap = makeFileMap({
'index.html': { bytes: txt('<html><body><img src="https://cdn.example.com/img.png"></body></html>') },
Expand Down
7 changes: 5 additions & 2 deletions src/core/siteImport/assetPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,12 @@ function resolveAndRecord(
): string | null {
if (!rawUrl || EXTERNAL_URL_RE.test(rawUrl)) return null

const fileMapKey = resolveRelativePath(rawUrl, basePath)
if (!fileMapKey) return null
const resolvedKey = resolveRelativePath(rawUrl, basePath)
if (!resolvedKey) return null

const fileMapKey = fileMap.files[resolvedKey]
? resolvedKey
: `passthrough/${resolvedKey}`
const entry = fileMap.files[fileMapKey]
if (!entry) return null

Expand Down