fix: reverts #8471 · vitejs/vite@da77dee · GitHub
Skip to content

Commit da77dee

Browse files
sapphi-redpatak-cat
authored andcommitted
fix: reverts #8471
This reverts commit 8d7bac4.
1 parent 96c885a commit da77dee

4 files changed

Lines changed: 42 additions & 17 deletions

File tree

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

Lines changed: 5 additions & 1 deletion

packages/vite/src/node/importGlob.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
preloadMarker,
1010
preloadMethod
1111
} from './plugins/importAnalysisBuild'
12+
import { isCSSRequest } from './plugins/css'
1213
import {
1314
blankReplacer,
1415
cleanUrl,
@@ -149,14 +150,16 @@ export async function transformImportGlob(
149150
await fsp.readFile(path.join(base, files[i]), 'utf-8')
150151
)},`
151152
} else {
153+
const importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee
152154
if (isEager) {
153155
const identifier = `__glob_${importIndex}_${i}`
156+
// css imports injecting a ?used query to export the css string
154157
importsString += `import ${
155158
isEagerDefault ? `` : `* as `
156-
}${identifier} from ${JSON.stringify(importee)};`
159+
}${identifier} from ${JSON.stringify(importeeUrl)};`
157160
entries += ` ${JSON.stringify(file)}: ${identifier},`
158161
} else {
159-
let imp = `import(${JSON.stringify(importee)})`
162+
let imp = `import(${JSON.stringify(importeeUrl)})`
160163
if (!normalizeUrl && preload) {
161164
imp =
162165
`(${isModernFlag}` +

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ 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/
105106
const varRE = /^var\(/i
106107

107108
const enum PreprocessLang {
@@ -369,19 +370,18 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
369370
}
370371

371372
let code: string
372-
if (modulesCode) {
373-
code = modulesCode
374-
} else {
375-
let content = css
376-
if (config.build.minify) {
377-
content = await minifyCSS(content, config)
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)}`
378382
}
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-
)})()`
383+
} else {
384+
code = `export default ''`
385385
}
386386

387387
return {

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

Lines changed: 20 additions & 2 deletions

0 commit comments

Comments
 (0)