fix: ?inline warning for .css.js file (#11347)

This commit is contained in:
patak 2022-12-12 20:12:53 +01:00 committed by GitHub
parent 34fec4158b
commit 729fb1a750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 29 deletions

View File

@ -438,35 +438,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
str().remove(end + 1, expEnd)
}
if (
!isDynamicImport &&
specifier &&
!specifier.includes('?') && // ignore custom queries
isCSSRequest(specifier) &&
!isModuleCSSRequest(specifier)
) {
const sourceExp = source.slice(expStart, start)
if (
sourceExp.includes('from') && // check default and named imports
!sourceExp.includes('__vite_glob_') // glob handles deprecation message itself
) {
const newImport =
sourceExp + specifier + `?inline` + source.slice(end, expEnd)
this.warn(
`\n` +
colors.cyan(importerModule.file) +
`\n` +
colors.reset(generateCodeFrame(source, start)) +
`\n` +
colors.yellow(
`Default and named imports from CSS files are deprecated. ` +
`Use the ?inline query instead. ` +
`For example: ${newImport}`,
),
)
}
}
// static import or valid string in dynamic import
// If resolvable, let's resolve it
if (specifier) {
@ -509,6 +480,35 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize
const [url, resolvedId] = await normalizeUrl(specifier, start)
if (
!isDynamicImport &&
specifier &&
!specifier.includes('?') && // ignore custom queries
isCSSRequest(resolvedId) &&
!isModuleCSSRequest(resolvedId)
) {
const sourceExp = source.slice(expStart, start)
if (
sourceExp.includes('from') && // check default and named imports
!sourceExp.includes('__vite_glob_') // glob handles deprecation message itself
) {
const newImport =
sourceExp + specifier + `?inline` + source.slice(end, expEnd)
this.warn(
`\n` +
colors.cyan(importerModule.file) +
`\n` +
colors.reset(generateCodeFrame(source, start)) +
`\n` +
colors.yellow(
`Default and named imports from CSS files are deprecated. ` +
`Use the ?inline query instead. ` +
`For example: ${newImport}`,
),
)
}
}
// record as safe modules
server?.moduleGraph.safeModulesPath.add(fsPathFromUrl(url))

View File

@ -451,6 +451,14 @@ test('PostCSS source.input.from includes query', async () => {
)
})
test('js file ending with .css.js', async () => {
const message = await page.textContent('.jsfile-css-js')
expect(message).toMatch('from jsfile.css.js')
serverLogs.forEach((log) => {
expect(log).not.toMatch(/Use the \?inline query instead.+jsfile\.css/)
})
})
test('aliased css has content', async () => {
expect(await getColor('.aliased')).toBe('blue')
// skipped: currently not supported see #8936

View File

@ -165,6 +165,9 @@
<p>PostCSS source.input.from. Should include query</p>
<pre class="postcss-source-input"></pre>
<p>Import from jsfile.css.js without the extension</p>
<pre class="jsfile-css-js"></pre>
<p>Aliased</p>
<p class="aliased">import '#alias': this should be blue</p>
<pre class="aliased-content"></pre>

View File

@ -0,0 +1,2 @@
const message = 'from jsfile.css.js'
export default message

View File

@ -100,6 +100,10 @@ text('.imported-css-globEager', JSON.stringify(globEager, null, 2))
import postcssSourceInput from './postcss-source-input.css?query=foo'
text('.postcss-source-input', postcssSourceInput)
// The file is jsfile.css.js, and we should be able to import it without extension
import jsFileMessage from './jsfile.css'
text('.jsfile-css-js', jsFileMessage)
import aliasContent from '#alias'
text('.aliased-content', aliasContent)
import aliasModule from '#alias-module'