Skip to content

Commit

Permalink
fix(compiler-sfc): support asset paths containing spaces (vuejs#8752)
Browse files Browse the repository at this point in the history
By decoding them before generating them as JavaScript import paths

fix vuejs/vitepress#2596
fix vuejs/vitepress#573
  • Loading branch information
Jeff-Tian authored and lumozx committed Oct 21, 2023
1 parent 4c6b7a4 commit b7fb0ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,34 @@ describe('compiler sfc: transform asset url', () => {
expect(code).toMatch(`_createStaticVNode`)
expect(code).toMatchSnapshot()
})

test('transform with stringify with space in absolute filename', () => {
const { code } = compileWithAssetUrls(
`<div><img src="/foo bar.png"/></div>`,
{
includeAbsolute: true
},
{
hoistStatic: true,
transformHoist: stringifyStatic
}
)
expect(code).toMatch(`_createElementVNode`)
expect(code).toContain(`import _imports_0 from '/foo bar.png'`)
})

test('transform with stringify with space in relative filename', () => {
const { code } = compileWithAssetUrls(
`<div><img src="./foo bar.png"/></div>`,
{
includeAbsolute: true
},
{
hoistStatic: true,
transformHoist: stringifyStatic
}
)
expect(code).toMatch(`_createElementVNode`)
expect(code).toContain(`import _imports_0 from './foo bar.png'`)
})
})
8 changes: 7 additions & 1 deletion packages/compiler-sfc/src/template/transformAssetUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ function getImportsExpressionExp(
loc,
ConstantTypes.CAN_STRINGIFY
)
context.imports.push({ exp, path })

// We need to ensure the path is not encoded (to %2F),
// so we decode it back in case it is encoded
context.imports.push({
exp,
path: decodeURIComponent(path)
})
}

if (!hash) {
Expand Down

0 comments on commit b7fb0ce

Please sign in to comment.