Skip to content

Commit 8d7bac4

Browse files
authored
fix(css): remove ?used hack (fixes #6421, #8245) (#8278) (#8471)
1 parent 7b972bc commit 8d7bac4

File tree

4 files changed

+17
-42
lines changed

4 files changed

+17
-42
lines changed

packages/playground/css/__tests__/css.spec.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,5 @@ test("relative path rewritten in Less's data-uri", async () => {
417417
test('PostCSS source.input.from includes query', async () => {
418418
const code = await page.textContent('.postcss-source-input')
419419
// should resolve assets
420-
expect(code).toContain(
421-
isBuild
422-
? '/postcss-source-input.css?used&query=foo'
423-
: '/postcss-source-input.css?query=foo'
424-
)
420+
expect(code).toContain('/postcss-source-input.css?query=foo')
425421
})

packages/vite/src/node/importGlob.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
preloadMarker,
1010
preloadMethod
1111
} from './plugins/importAnalysisBuild'
12-
import { isCSSRequest } from './plugins/css'
1312
import {
1413
blankReplacer,
1514
cleanUrl,
@@ -150,16 +149,14 @@ export async function transformImportGlob(
150149
await fsp.readFile(path.join(base, files[i]), 'utf-8')
151150
)},`
152151
} else {
153-
const importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee
154152
if (isEager) {
155153
const identifier = `__glob_${importIndex}_${i}`
156-
// css imports injecting a ?used query to export the css string
157154
importsString += `import ${
158155
isEagerDefault ? `` : `* as `
159-
}${identifier} from ${JSON.stringify(importeeUrl)};`
156+
}${identifier} from ${JSON.stringify(importee)};`
160157
entries += ` ${JSON.stringify(file)}: ${identifier},`
161158
} else {
162-
let imp = `import(${JSON.stringify(importeeUrl)})`
159+
let imp = `import(${JSON.stringify(importee)})`
163160
if (!normalizeUrl && preload) {
164161
imp =
165162
`(${isModernFlag}` +

packages/vite/src/node/plugins/css.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ const htmlProxyRE = /(\?|&)html-proxy\b/
102102
const commonjsProxyRE = /\?commonjs-proxy/
103103
const inlineRE = /(\?|&)inline\b/
104104
const inlineCSSRE = /(\?|&)inline-css\b/
105-
const usedRE = /(\?|&)used\b/
106105
const varRE = /^var\(/i
107106

108107
const enum PreprocessLang {
@@ -370,18 +369,19 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
370369
}
371370

372371
let code: string
373-
if (usedRE.test(id)) {
374-
if (modulesCode) {
375-
code = modulesCode
376-
} else {
377-
let content = css
378-
if (config.build.minify) {
379-
content = await minifyCSS(content, config)
380-
}
381-
code = `export default ${JSON.stringify(content)}`
382-
}
372+
if (modulesCode) {
373+
code = modulesCode
383374
} else {
384-
code = `export default ''`
375+
let content = css
376+
if (config.build.minify) {
377+
content = await minifyCSS(content, config)
378+
}
379+
// marking as pure to make it tree-shakable by minifier
380+
// but the module itself is still treated as a non tree-shakable module
381+
// because moduleSideEffects is 'no-treeshake'
382+
code = `export default /* #__PURE__ */ (() => ${JSON.stringify(
383+
content
384+
)})()`
385385
}
386386

387387
return {

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

+2-20
Original file line numberDiff line numberDiff line change
@@ -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 { bareImportRE, combineSourcemaps } from '../utils'
8+
import { combineSourcemaps } from '../utils'
99
import type { Plugin } from '../plugin'
1010
import type { ResolvedConfig } from '../config'
1111
import { genSourceMapUrl } from '../server/sourcemap'
12-
import { isCSSRequest, removedPureCssFilesCache } from './css'
12+
import { removedPureCssFilesCache } from './css'
1313

1414
/**
1515
* A flag for injected helpers. This flag will be set to `false` if the output
@@ -148,7 +148,6 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
148148
e: end,
149149
ss: expStart,
150150
se: expEnd,
151-
n: specifier,
152151
d: dynamicIndex
153152
} = imports[index]
154153

@@ -195,23 +194,6 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
195194
const replacement = `${preloadMethod}(() => ${original},${isModernFlag}?"${preloadMarker}":void 0)`
196195
str().overwrite(expStart, expEnd, replacement, { contentOnly: true })
197196
}
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-
}
215197
}
216198

217199
if (

0 commit comments

Comments
 (0)