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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(importGlob): warn on default import css #11103

Merged
merged 2 commits into from Nov 28, 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
@@ -0,0 +1,3 @@
.a {
color: red;
}
@@ -0,0 +1,3 @@
.b {
color: red;
}
@@ -0,0 +1,3 @@
.b {
color: red;
}
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { transformGlobImport } from '../../../plugins/importMetaGlob'
import { transformWithEsbuild } from '../../../plugins/esbuild'
import type { Logger } from '../../../logger'
import { createLogger } from '../../../logger'

const __dirname = resolve(fileURLToPath(import.meta.url), '..')
Expand Down Expand Up @@ -72,4 +73,43 @@ describe('fixture', async () => {
)?.s.toString()
).toMatchSnapshot()
})

it('warn when glob css without ?inline', async () => {
const logs: string[] = []
const logger = {
warn(msg: string) {
logs.push(msg)
}
} as Logger

await transformGlobImport(
"import.meta.glob('./fixture-c/*.css', { query: '?inline' })",
fileURLToPath(import.meta.url),
root,
resolveId,
logger
)
expect(logs).toHaveLength(0)

await transformGlobImport(
"import.meta.glob('./fixture-c/*.module.css')",
fileURLToPath(import.meta.url),
root,
resolveId,
logger
)
expect(logs).toHaveLength(0)

await transformGlobImport(
"import.meta.glob('./fixture-c/*.css')",
fileURLToPath(import.meta.url),
root,
resolveId,
logger
)
expect(logs).toHaveLength(1)
expect(logs[0]).to.include(
'Globbing CSS files without the ?inline query is deprecated'
)
})
})
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -439,9 +439,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
if (
!isDynamicImport &&
specifier &&
!specifier.includes('?') && // ignore custom queries
isCSSRequest(specifier) &&
!isModuleCSSRequest(specifier) &&
!specifier.includes('?') // ignore custom queries
!isModuleCSSRequest(specifier)
) {
const sourceExp = source.slice(expStart, start)
if (
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -29,7 +29,7 @@ import {
transformStableResult
} from '../utils'
import type { Logger } from '../logger'
import { isCSSRequest } from './css'
import { isCSSRequest, isModuleCSSRequest } from './css'

const { isMatch, scan } = micromatch

Expand Down Expand Up @@ -385,8 +385,10 @@ export async function transformGlobImport(
if (query && !query.startsWith('?')) query = `?${query}`

if (
!query.match(/(?:\?|&)inline\b/) &&
files.some((file) => isCSSRequest(file))
!query && // ignore custom queries
files.some(
(file) => isCSSRequest(file) && !isModuleCSSRequest(file)
)
) {
logger.warn(
`\n` +
Expand Down