Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(glob): css imports injecting a ?used query to export the css string #6949

Merged
merged 9 commits into from Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -13,6 +13,14 @@ import {

// note: tests should retrieve the element at the beginning of test and reuse it
// in later assertions to ensure CSS HMR doesn't reload the page
test('imported css', async () => {
const css = await page.textContent('.imported-css')
expect(css).toContain('.imported {')
const glob = await page.textContent('.imported-css-glob')
expect(glob).toContain('.dir-import')
const globEager = await page.textContent('.imported-css-globEager')
expect(globEager).toContain('.dir-import')
})

test('linked css', async () => {
const linked = await page.$('.linked')
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/glob-import/bar.css
@@ -0,0 +1,3 @@
.dir-import-2 {
color: grey;
}
3 changes: 3 additions & 0 deletions packages/playground/css/glob-import/foo.css
@@ -0,0 +1,3 @@
.dir-import {
color: grey;
}
2 changes: 2 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -12,6 +12,8 @@ <h1>CSS</h1>
</p>
<p>Imported css string:</p>
<pre class="imported-css"></pre>
<pre class="imported-css-glob"></pre>
<pre class="imported-css-globEager"></pre>

<p class="postcss">
<span class="nesting">PostCSS nesting plugin: this should be pink</span>
Expand Down
10 changes: 10 additions & 0 deletions packages/playground/css/main.js
Expand Up @@ -68,3 +68,13 @@ if (import.meta.env.DEV) {
// inlined
import inlined from './inlined.css?inline'
text('.inlined-code', inlined)

// glob
const glob = import.meta.glob('./glob-import/*.css')
Promise.all(Object.keys(glob).map((key) => glob[key]())).then((res) => {
text('.imported-css-glob', JSON.stringify(res, null, 2))
})

// globEager
const globEager = import.meta.globEager('./glob-import/*.css')
text('.imported-css-globEager', JSON.stringify(globEager, null, 2))
31 changes: 18 additions & 13 deletions packages/vite/src/node/importGlob.ts
Expand Up @@ -7,6 +7,7 @@ import {
preloadMethod,
preloadMarker
} from './plugins/importAnalysisBuild'
import { isCSSRequest } from './plugins/css'
import { cleanUrl } from './utils'
import type { RollupError } from 'rollup'

Expand Down Expand Up @@ -92,21 +93,25 @@ export async function transformImportGlob(
entries += ` ${JSON.stringify(file)}: ${JSON.stringify(
await fsp.readFile(path.join(base, file), 'utf-8')
)},`
} else if (isEager) {
const identifier = `__glob_${importIndex}_${i}`
importsString += `import ${
isEagerDefault ? `` : `* as `
}${identifier} from ${JSON.stringify(importee)};`
entries += ` ${JSON.stringify(file)}: ${identifier},`
} else {
let imp = `import(${JSON.stringify(importee)})`
if (!normalizeUrl && preload) {
imp =
`(${isModernFlag}` +
`? ${preloadMethod}(()=>${imp},"${preloadMarker}")` +
`: ${imp})`
const importeeUrl = isCSSRequest(importee) ? `${importee}?used` : importee
if (isEager) {
const identifier = `__glob_${importIndex}_${i}`
// css imports injecting a ?used query to export the css string
importsString += `import ${
isEagerDefault ? `` : `* as `
}${identifier} from ${JSON.stringify(importeeUrl)};`
entries += ` ${JSON.stringify(file)}: ${identifier},`
} else {
let imp = `import(${JSON.stringify(importeeUrl)})`
if (!normalizeUrl && preload) {
imp =
`(${isModernFlag}` +
`? ${preloadMethod}(()=>${imp},"${preloadMarker}")` +
`: ${imp})`
}
entries += ` ${JSON.stringify(file)}: () => ${imp},`
}
entries += ` ${JSON.stringify(file)}: () => ${imp},`
}
}

Expand Down