Skip to content

Commit

Permalink
fix(build-import-analysis): should not append ?used when css request …
Browse files Browse the repository at this point in the history
…has ?url or ?raw (#11910)
  • Loading branch information
sun0day committed Mar 1, 2023
1 parent 48cd43b commit e3f725f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -19,6 +19,7 @@ import type { ResolvedConfig } from '../config'
import { toOutputFilePathInJS } from '../build'
import { genSourceMapUrl } from '../server/sourcemap'
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
import { SPECIAL_QUERY_RE } from '../constants'
import { isCSSRequest, removedPureCssFilesCache } from './css'
import { interopNamedImports } from './importAnalysis'

Expand Down Expand Up @@ -362,6 +363,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
(source.slice(expStart, start).includes('from') || isDynamicImport) &&
// already has ?used query (by import.meta.glob)
!specifier.match(/\?used(&|$)/) &&
// don't append ?used when SPECIAL_QUERY_RE exists
!specifier.match(SPECIAL_QUERY_RE) &&
// edge case for package names ending with .css (e.g normalize.css)
!(bareImportRE.test(specifier) && !specifier.includes('/'))
) {
Expand Down
Expand Up @@ -9,6 +9,8 @@ import {
viteConfig,
} from '~utils'

const getBase = () => (viteConfig ? viteConfig?.testConfig?.baseRoute : '')

const absoluteAssetMatch = isBuild
? /http.*\/other-assets\/asset-\w{8}\.png/
: '/nested/asset.png'
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('css url() references', () => {
describe.runIf(isBuild)('index.css URLs', () => {
let css: string
beforeAll(() => {
const base = viteConfig ? viteConfig?.testConfig?.baseRoute : ''
const base = getBase()
css = findAssetFile(/index.*\.css$/, base, 'other-assets')
})

Expand Down Expand Up @@ -200,6 +202,10 @@ test('?url import on css', async () => {
expect(txt).toMatch(
isBuild ? /http.*\/other-assets\/icons-\w{8}\.css/ : '/css/icons.css',
)
isBuild &&
expect(findAssetFile(/index.*\.js$/, getBase(), 'entries')).toMatch(
/icons-.+\.css(?!\?used)/,
)
})

test('new URL(..., import.meta.url)', async () => {
Expand Down

0 comments on commit e3f725f

Please sign in to comment.