mirror of
https://github.com/vitejs/vite.git
synced 2024-11-22 07:09:05 +00:00
fix: non-relative base public paths in CSS files (#8682)
This commit is contained in:
parent
4061ee02bf
commit
d11d6eaeea
@ -458,7 +458,8 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
|
||||
// resolve asset URL placeholders to their built file URLs
|
||||
function resolveAssetUrlsInCss(chunkCSS: string, cssAssetName: string) {
|
||||
const encodedPublicUrls = encodePublicUrlsInCSS(config)
|
||||
const assetsBase = config.experimental.buildAdvancedBaseOptions.assets
|
||||
const { assets: assetsBase, public: publicBase } =
|
||||
config.experimental.buildAdvancedBaseOptions
|
||||
const cssAssetDirname =
|
||||
encodedPublicUrls || assetsBase.relative
|
||||
? getCssAssetDirname(cssAssetName)
|
||||
@ -475,23 +476,25 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
|
||||
? relativePath
|
||||
: './' + relativePath
|
||||
} else {
|
||||
if (assetsBase.runtime) {
|
||||
// config.logger.error('Error TODO:base')... absolute + runtime
|
||||
}
|
||||
// assetsBase.runtime has no effect for assets in CSS
|
||||
return (assetsBase.url ?? config.base) + filename
|
||||
}
|
||||
})
|
||||
// resolve public URL from CSS paths, TODO:base
|
||||
// resolve public URL from CSS paths
|
||||
if (encodedPublicUrls) {
|
||||
const relativePathToPublicFromCSS = path.posix.relative(
|
||||
cssAssetDirname!,
|
||||
''
|
||||
)
|
||||
chunkCSS = chunkCSS.replace(
|
||||
publicAssetUrlRE,
|
||||
(_, hash) =>
|
||||
relativePathToPublicFromCSS + publicAssetUrlMap.get(hash)!
|
||||
)
|
||||
chunkCSS = chunkCSS.replace(publicAssetUrlRE, (_, hash) => {
|
||||
const publicUrl = publicAssetUrlMap.get(hash)!
|
||||
if (publicBase.relative) {
|
||||
return relativePathToPublicFromCSS + publicUrl
|
||||
} else {
|
||||
// publicBase.runtime has no effect for assets in CSS
|
||||
return (publicBase.url ?? config.base) + publicUrl.slice(1)
|
||||
}
|
||||
})
|
||||
}
|
||||
return chunkCSS
|
||||
}
|
||||
|
@ -179,8 +179,14 @@ describe('css url() references', () => {
|
||||
expect(bg).toMatch(assetMatch)
|
||||
})
|
||||
|
||||
test.runIf(isBuild)('preserve postfix query/hash', () => {
|
||||
expect(findAssetFile(/\.css$/, 'foo')).toMatch(`woff2?#iefix`)
|
||||
test.runIf(isBuild)('generated paths in CSS', () => {
|
||||
const css = findAssetFile(/\.css$/, 'foo')
|
||||
|
||||
// preserve postfix query/hash
|
||||
expect(css).toMatch(`woff2?#iefix`)
|
||||
|
||||
// generate non-relative base for public path in CSS
|
||||
expect(css).not.toMatch(`../icon.png`)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -6,7 +6,10 @@
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
|
||||
"preview": "vite preview"
|
||||
"preview": "vite preview",
|
||||
"dev:relative-base": "vite --config ./vite.config-relative-base.js dev",
|
||||
"build:relative-base": "vite --config ./vite.config-relative-base.js build",
|
||||
"preview:relative-base": "vite --config ./vite.config-relative-base.js preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
"css-dep": "link:./css-dep",
|
||||
|
26
playground/css/vite.config-relative-base.js
Normal file
26
playground/css/vite.config-relative-base.js
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @type {import('vite').UserConfig}
|
||||
*/
|
||||
|
||||
const baseConfig = require('./vite.config.js')
|
||||
module.exports = {
|
||||
...baseConfig,
|
||||
base: './', // relative base to make dist portable
|
||||
build: {
|
||||
...baseConfig.build,
|
||||
outDir: 'dist/relative-base',
|
||||
watch: false,
|
||||
minify: false,
|
||||
assetsInlineLimit: 0,
|
||||
rollupOptions: {
|
||||
output: {
|
||||
entryFileNames: 'entries/[name].js',
|
||||
chunkFileNames: 'chunks/[name].[hash].js',
|
||||
assetFileNames: 'other-assets/[name].[hash][extname]'
|
||||
}
|
||||
}
|
||||
},
|
||||
testConfig: {
|
||||
baseRoute: '/relative-base/'
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ const path = require('path')
|
||||
* @type {import('vite').UserConfig}
|
||||
*/
|
||||
module.exports = {
|
||||
base: './',
|
||||
build: {
|
||||
cssTarget: 'chrome61'
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user