fix: reverts #8471 · vitejs/vite@da77dee

@@ -5,11 +5,11 @@ import { init, parse as parseImports } from 'es-module-lexer'

55

import type { OutputChunk, SourceMap } from 'rollup'

66

import type { RawSourceMap } from '@ampproject/remapping'

77

import { transformImportGlob } from '../importGlob'

8-

import { combineSourcemaps } from '../utils'

8+

import { bareImportRE, combineSourcemaps } from '../utils'

99

import type { Plugin } from '../plugin'

1010

import type { ResolvedConfig } from '../config'

1111

import { genSourceMapUrl } from '../server/sourcemap'

12-

import { removedPureCssFilesCache } from './css'

12+

import { isCSSRequest, removedPureCssFilesCache } from './css'

13131414

/**

1515

* A flag for injected helpers. This flag will be set to `false` if the output

@@ -148,6 +148,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

148148

e: end,

149149

ss: expStart,

150150

se: expEnd,

151+

n: specifier,

151152

d: dynamicIndex

152153

} = imports[index]

153154

@@ -194,6 +195,23 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {

194195

const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)`

195196

str().overwrite(expStart, expEnd, replacement, { contentOnly: true })

196197

}

198+199+

// Differentiate CSS imports that use the default export from those that

200+

// do not by injecting a ?used query - this allows us to avoid including

201+

// the CSS string when unnecessary (esbuild has trouble tree-shaking

202+

// them)

203+

if (

204+

specifier &&

205+

isCSSRequest(specifier) &&

206+

source.slice(expStart, start).includes('from') &&

207+

// edge case for package names ending with .css (e.g normalize.css)

208+

!(bareImportRE.test(specifier) && !specifier.includes('/'))

209+

) {

210+

const url = specifier.replace(/\?|$/, (m) => `?used${m ? '&' : ''}`)

211+

str().overwrite(start, end, dynamicIndex > -1 ? `'${url}'` : url, {

212+

contentOnly: true

213+

})

214+

}

197215

}

198216199217

if (