Skip to content

Commit 729fb1a

Browse files
authoredDec 12, 2022
fix: ?inline warning for .css.js file (#11347)
1 parent 34fec41 commit 729fb1a

File tree

5 files changed

+46
-29
lines changed

5 files changed

+46
-29
lines changed
 

‎packages/vite/src/node/plugins/importAnalysis.ts

+29-29
Original file line numberDiff line numberDiff line change
@@ -438,35 +438,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
438438
str().remove(end + 1, expEnd)
439439
}
440440

441-
if (
442-
!isDynamicImport &&
443-
specifier &&
444-
!specifier.includes('?') && // ignore custom queries
445-
isCSSRequest(specifier) &&
446-
!isModuleCSSRequest(specifier)
447-
) {
448-
const sourceExp = source.slice(expStart, start)
449-
if (
450-
sourceExp.includes('from') && // check default and named imports
451-
!sourceExp.includes('__vite_glob_') // glob handles deprecation message itself
452-
) {
453-
const newImport =
454-
sourceExp + specifier + `?inline` + source.slice(end, expEnd)
455-
this.warn(
456-
`\n` +
457-
colors.cyan(importerModule.file) +
458-
`\n` +
459-
colors.reset(generateCodeFrame(source, start)) +
460-
`\n` +
461-
colors.yellow(
462-
`Default and named imports from CSS files are deprecated. ` +
463-
`Use the ?inline query instead. ` +
464-
`For example: ${newImport}`,
465-
),
466-
)
467-
}
468-
}
469-
470441
// static import or valid string in dynamic import
471442
// If resolvable, let's resolve it
472443
if (specifier) {
@@ -509,6 +480,35 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
509480
// normalize
510481
const [url, resolvedId] = await normalizeUrl(specifier, start)
511482

483+
if (
484+
!isDynamicImport &&
485+
specifier &&
486+
!specifier.includes('?') && // ignore custom queries
487+
isCSSRequest(resolvedId) &&
488+
!isModuleCSSRequest(resolvedId)
489+
) {
490+
const sourceExp = source.slice(expStart, start)
491+
if (
492+
sourceExp.includes('from') && // check default and named imports
493+
!sourceExp.includes('__vite_glob_') // glob handles deprecation message itself
494+
) {
495+
const newImport =
496+
sourceExp + specifier + `?inline` + source.slice(end, expEnd)
497+
this.warn(
498+
`\n` +
499+
colors.cyan(importerModule.file) +
500+
`\n` +
501+
colors.reset(generateCodeFrame(source, start)) +
502+
`\n` +
503+
colors.yellow(
504+
`Default and named imports from CSS files are deprecated. ` +
505+
`Use the ?inline query instead. ` +
506+
`For example: ${newImport}`,
507+
),
508+
)
509+
}
510+
}
511+
512512
// record as safe modules
513513
server?.moduleGraph.safeModulesPath.add(fsPathFromUrl(url))
514514

‎playground/css/__tests__/css.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,14 @@ test('PostCSS source.input.from includes query', async () => {
451451
)
452452
})
453453

454+
test('js file ending with .css.js', async () => {
455+
const message = await page.textContent('.jsfile-css-js')
456+
expect(message).toMatch('from jsfile.css.js')
457+
serverLogs.forEach((log) => {
458+
expect(log).not.toMatch(/Use the \?inline query instead.+jsfile\.css/)
459+
})
460+
})
461+
454462
test('aliased css has content', async () => {
455463
expect(await getColor('.aliased')).toBe('blue')
456464
// skipped: currently not supported see #8936

‎playground/css/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ <h1>CSS</h1>
165165
<p>PostCSS source.input.from. Should include query</p>
166166
<pre class="postcss-source-input"></pre>
167167

168+
<p>Import from jsfile.css.js without the extension</p>
169+
<pre class="jsfile-css-js"></pre>
170+
168171
<p>Aliased</p>
169172
<p class="aliased">import '#alias': this should be blue</p>
170173
<pre class="aliased-content"></pre>

‎playground/css/jsfile.css.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const message = 'from jsfile.css.js'
2+
export default message

‎playground/css/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ text('.imported-css-globEager', JSON.stringify(globEager, null, 2))
100100
import postcssSourceInput from './postcss-source-input.css?query=foo'
101101
text('.postcss-source-input', postcssSourceInput)
102102

103+
// The file is jsfile.css.js, and we should be able to import it without extension
104+
import jsFileMessage from './jsfile.css'
105+
text('.jsfile-css-js', jsFileMessage)
106+
103107
import aliasContent from '#alias'
104108
text('.aliased-content', aliasContent)
105109
import aliasModule from '#alias-module'

0 commit comments

Comments
 (0)
Please sign in to comment.