Skip to content

Commit e3f725f

Browse files
authoredMar 1, 2023
fix(build-import-analysis): should not append ?used when css request has ?url or ?raw (#11910)
1 parent 48cd43b commit e3f725f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed
 

‎packages/vite/src/node/plugins/importAnalysisBuild.ts

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { ResolvedConfig } from '../config'
1919
import { toOutputFilePathInJS } from '../build'
2020
import { genSourceMapUrl } from '../server/sourcemap'
2121
import { getDepsOptimizer, optimizedDepNeedsInterop } from '../optimizer'
22+
import { SPECIAL_QUERY_RE } from '../constants'
2223
import { isCSSRequest, removedPureCssFilesCache } from './css'
2324
import { interopNamedImports } from './importAnalysis'
2425

@@ -362,6 +363,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
362363
(source.slice(expStart, start).includes('from') || isDynamicImport) &&
363364
// already has ?used query (by import.meta.glob)
364365
!specifier.match(/\?used(&|$)/) &&
366+
// don't append ?used when SPECIAL_QUERY_RE exists
367+
!specifier.match(SPECIAL_QUERY_RE) &&
365368
// edge case for package names ending with .css (e.g normalize.css)
366369
!(bareImportRE.test(specifier) && !specifier.includes('/'))
367370
) {

‎playground/assets/__tests__/relative-base/relative-base-assets.spec.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
viteConfig,
1010
} from '~utils'
1111

12+
const getBase = () => (viteConfig ? viteConfig?.testConfig?.baseRoute : '')
13+
1214
const absoluteAssetMatch = isBuild
1315
? /http.*\/other-assets\/asset-\w{8}\.png/
1416
: '/nested/asset.png'
@@ -138,7 +140,7 @@ describe('css url() references', () => {
138140
describe.runIf(isBuild)('index.css URLs', () => {
139141
let css: string
140142
beforeAll(() => {
141-
const base = viteConfig ? viteConfig?.testConfig?.baseRoute : ''
143+
const base = getBase()
142144
css = findAssetFile(/index.*\.css$/, base, 'other-assets')
143145
})
144146

@@ -200,6 +202,10 @@ test('?url import on css', async () => {
200202
expect(txt).toMatch(
201203
isBuild ? /http.*\/other-assets\/icons-\w{8}\.css/ : '/css/icons.css',
202204
)
205+
isBuild &&
206+
expect(findAssetFile(/index.*\.js$/, getBase(), 'entries')).toMatch(
207+
/icons-.+\.css(?!\?used)/,
208+
)
203209
})
204210

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

0 commit comments

Comments
 (0)
Please sign in to comment.