@@ -4,7 +4,7 @@ import {replace, replaceImport, replaceInStringLiteral, replaceInTemplateElement
44import { StringAsBytes , collectMatchingStrings , parseCode } from "./ast" ;
55
66export async function transformChunk ( codeStr : string , options : TransformOptions ) : Promise < string > {
7- const { base, publicPath } = options
7+ const { base, publicPath, removeStartingSlash } = options
88 const [ spanOffset , ast ] = await parseCode ( codeStr ) ;
99
1010 const strings = collectMatchingStrings ( base , ast ) ;
@@ -23,9 +23,9 @@ export async function transformChunk(codeStr: string, options: TransformOptions)
2323
2424 let transformed : string ;
2525 if ( str . type === 'TemplateElement' ) {
26- transformed = replaceInTemplateElement ( str , base , publicPath ) ;
26+ transformed = replaceInTemplateElement ( str , base , publicPath , removeStartingSlash ) ;
2727 } else if ( str . type === 'StringLiteral' ) {
28- transformed = replaceInStringLiteral ( str , base , publicPath ) ;
28+ transformed = replaceInStringLiteral ( str , base , publicPath , removeStartingSlash ) ;
2929 }
3030
3131 lastIdx = str . span . end - spanOffset ;
@@ -44,16 +44,20 @@ export function transformAsset(code: string, options: TransformOptions) {
4444}
4545
4646export function transformLegacyHtml ( code : string , options : TransformOptions ) {
47- const { base, publicPath } = options
47+ const { base, publicPath, removeStartingSlash } = options
4848 let content = replaceSrc ( publicPath , code )
4949 content = replace ( base , '/' , content )
5050 content = replaceImport ( publicPath , content )
5151 const document = parse ( content , { comment : true } )
5252 const legacyPolyfill = document . getElementById ( 'vite-legacy-polyfill' )
53- const legacyPolyfillSrc = legacyPolyfill ?. getAttribute ( 'src' )
53+ let legacyPolyfillSrc = legacyPolyfill ?. getAttribute ( 'src' )
5454
5555 const legacyEntry = document . getElementById ( 'vite-legacy-entry' )
56- const legacyEntrySrc = legacyEntry ?. getAttribute ( 'data-src' )
56+ let legacyEntrySrc = legacyEntry ?. getAttribute ( 'data-src' )
57+ if ( removeStartingSlash ) {
58+ legacyPolyfillSrc = legacyPolyfillSrc ?. replace ( / ^ \/ / , '' )
59+ legacyEntrySrc = legacyEntrySrc ?. replace ( / ^ \/ / , '' )
60+ }
5761
5862 if ( legacyPolyfill ) {
5963 legacyPolyfill . setAttribute ( 'data-src' , legacyPolyfillSrc )
0 commit comments