fix(vite): only apply relative url fix to css assets (#2930)

This commit is contained in:
Daniel Roe 2022-01-26 12:59:48 +00:00 committed by GitHub
parent 4b1ea5741e
commit d0bcc3fc82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,12 +18,13 @@ export const RelativeAssetPlugin = function (): Plugin {
for (const file in bundle) {
const asset = bundle[file]
if (asset.type === 'asset') {
if (asset.type === 'asset' && typeof asset.source === 'string' && asset.fileName.endsWith('.css')) {
const depth = file.split('/').length - 1
const assetBase = depth === 0 ? '.' : Array.from({ length: depth }).map(() => '..').join('/')
asset.source = asset.source.toString().replace(assetRE, r => r.replace(/\/__NUXT_BASE__/g, assetBase))
const publicBase = Array.from({ length: depth + 1 }).map(() => '..').join('/')
asset.source = asset.source.toString().replace(/\/__NUXT_BASE__/g, publicBase)
asset.source = asset.source
.replace(assetRE, r => r.replace(/\/__NUXT_BASE__/g, assetBase))
.replace(/\/__NUXT_BASE__/g, publicBase)
}
}
}