Skip to content

Commit 36c99a9

Browse files
authored
fix(compiler-sfc): support asset paths containing spaces (#8752)
By decoding them before generating them as JavaScript import paths fix vuejs/vitepress#2596 fix vuejs/vitepress#573
1 parent 5976233 commit 36c99a9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,34 @@ describe('compiler sfc: transform asset url', () => {
166166
expect(code).toMatch(`_createStaticVNode`)
167167
expect(code).toMatchSnapshot()
168168
})
169+
170+
test('transform with stringify with space in absolute filename', () => {
171+
const { code } = compileWithAssetUrls(
172+
`<div><img src="/foo bar.png"/></div>`,
173+
{
174+
includeAbsolute: true
175+
},
176+
{
177+
hoistStatic: true,
178+
transformHoist: stringifyStatic
179+
}
180+
)
181+
expect(code).toMatch(`_createElementVNode`)
182+
expect(code).toContain(`import _imports_0 from '/foo bar.png'`)
183+
})
184+
185+
test('transform with stringify with space in relative filename', () => {
186+
const { code } = compileWithAssetUrls(
187+
`<div><img src="./foo bar.png"/></div>`,
188+
{
189+
includeAbsolute: true
190+
},
191+
{
192+
hoistStatic: true,
193+
transformHoist: stringifyStatic
194+
}
195+
)
196+
expect(code).toMatch(`_createElementVNode`)
197+
expect(code).toContain(`import _imports_0 from './foo bar.png'`)
198+
})
169199
})

packages/compiler-sfc/src/template/transformAssetUrl.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,13 @@ function getImportsExpressionExp(
168168
loc,
169169
ConstantTypes.CAN_STRINGIFY
170170
)
171-
context.imports.push({ exp, path })
171+
172+
// We need to ensure the path is not encoded (to %2F),
173+
// so we decode it back in case it is encoded
174+
context.imports.push({
175+
exp,
176+
path: decodeURIComponent(path)
177+
})
172178
}
173179

174180
if (!hash) {

0 commit comments

Comments
 (0)