1
1
import { isAbsolute , posix } from 'node:path'
2
2
import micromatch from 'micromatch'
3
- import colors from 'picocolors'
4
3
import { stripLiteral } from 'strip-literal'
5
4
import type {
6
5
ArrayExpression ,
@@ -26,12 +25,10 @@ import type { ModuleNode } from '../server/moduleGraph'
26
25
import type { ResolvedConfig } from '../config'
27
26
import {
28
27
evalValue ,
29
- generateCodeFrame ,
30
28
normalizePath ,
31
29
slash ,
32
30
transformStableResult
33
31
} from '../utils'
34
- import type { Logger } from '../logger'
35
32
import { isCSSRequest , isModuleCSSRequest } from './css'
36
33
37
34
const { isMatch, scan } = micromatch
@@ -79,7 +76,7 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
79
76
id ,
80
77
config . root ,
81
78
( im ) => this . resolve ( im , id ) . then ( ( i ) => i ?. id || im ) ,
82
- config . logger ,
79
+ config . isProduction ,
83
80
config . experimental . importGlobRestoreExtension
84
81
)
85
82
if ( result ) {
@@ -320,6 +317,24 @@ const importPrefix = '__vite_glob_'
320
317
321
318
const { basename, dirname, relative, join } = posix
322
319
320
+ const warnedCSSDefaultImportVarName = '__vite_warned_css_default_import'
321
+ const jsonStringifyInOneline = ( input : any ) =>
322
+ JSON . stringify ( input ) . replace ( / [ { , : ] / g, '$& ' ) . replace ( / \} / g, ' }' )
323
+ const createCssDefaultImportWarning = (
324
+ globs : string [ ] ,
325
+ options : GeneralImportGlobOptions
326
+ ) =>
327
+ `if (!${ warnedCSSDefaultImportVarName } ) {` +
328
+ `${ warnedCSSDefaultImportVarName } = true;` +
329
+ `console.warn(${ JSON . stringify (
330
+ 'Default import of CSS without `?inline` is deprecated. ' +
331
+ "Add the `{ query: '?inline' }` glob option to fix this.\n" +
332
+ `For example: \`import.meta.glob(${ jsonStringifyInOneline (
333
+ globs . length === 1 ? globs [ 0 ] : globs
334
+ ) } , ${ jsonStringifyInOneline ( { ...options , query : '?inline' } ) } )\``
335
+ ) } );` +
336
+ `}`
337
+
323
338
export interface TransformGlobImportResult {
324
339
s : MagicString
325
340
matches : ParsedImportGlob [ ]
@@ -334,7 +349,7 @@ export async function transformGlobImport(
334
349
id : string ,
335
350
root : string ,
336
351
resolveId : IdResolver ,
337
- logger : Logger ,
352
+ isProduction : boolean ,
338
353
restoreQueryExtension = false
339
354
) : Promise < TransformGlobImportResult | null > {
340
355
id = slash ( id )
@@ -365,7 +380,15 @@ export async function transformGlobImport(
365
380
const staticImports = (
366
381
await Promise . all (
367
382
matches . map (
368
- async ( { globsResolved, isRelative, options, index, start, end } ) => {
383
+ async ( {
384
+ globs,
385
+ globsResolved,
386
+ isRelative,
387
+ options,
388
+ index,
389
+ start,
390
+ end
391
+ } ) => {
369
392
const cwd = getCommonBase ( globsResolved ) ?? root
370
393
const files = (
371
394
await fg ( globsResolved , {
@@ -391,25 +414,6 @@ export async function transformGlobImport(
391
414
392
415
if ( query && ! query . startsWith ( '?' ) ) query = `?${ query } `
393
416
394
- if (
395
- ! query && // ignore custom queries
396
- files . some (
397
- ( file ) => isCSSRequest ( file ) && ! isModuleCSSRequest ( file )
398
- )
399
- ) {
400
- logger . warn (
401
- `\n` +
402
- colors . cyan ( id ) +
403
- `\n` +
404
- colors . reset ( generateCodeFrame ( code , start ) ) +
405
- `\n` +
406
- colors . yellow (
407
- `Globbing CSS files without the ?inline query is deprecated. ` +
408
- `Add the \`{ query: '?inline' }\` glob option to fix this.`
409
- )
410
- )
411
- }
412
-
413
417
const resolvePaths = ( file : string ) => {
414
418
if ( ! dir ) {
415
419
if ( isRelative )
@@ -434,6 +438,7 @@ export async function transformGlobImport(
434
438
return { filePath, importPath }
435
439
}
436
440
441
+ let includesCSS = false
437
442
files . forEach ( ( file , i ) => {
438
443
const paths = resolvePaths ( file )
439
444
const filePath = paths . filePath
@@ -448,6 +453,10 @@ export async function transformGlobImport(
448
453
449
454
importPath = `${ importPath } ${ importQuery } `
450
455
456
+ const isCSS =
457
+ ! query && isCSSRequest ( file ) && ! isModuleCSSRequest ( file )
458
+ includesCSS ||= isCSS
459
+
451
460
const importKey =
452
461
options . import && options . import !== '*'
453
462
? options . import
@@ -461,14 +470,36 @@ export async function transformGlobImport(
461
470
staticImports . push (
462
471
`import ${ expression } from ${ JSON . stringify ( importPath ) } `
463
472
)
464
- objectProps . push ( `${ JSON . stringify ( filePath ) } : ${ variableName } ` )
473
+ if ( ! isProduction && isCSS ) {
474
+ objectProps . push (
475
+ `get ${ JSON . stringify (
476
+ filePath
477
+ ) } () { ${ createCssDefaultImportWarning (
478
+ globs ,
479
+ options
480
+ ) } return ${ variableName } }`
481
+ )
482
+ } else {
483
+ objectProps . push ( `${ JSON . stringify ( filePath ) } : ${ variableName } ` )
484
+ }
465
485
} else {
466
486
let importStatement = `import(${ JSON . stringify ( importPath ) } )`
467
487
if ( importKey )
468
488
importStatement += `.then(m => m[${ JSON . stringify ( importKey ) } ])`
469
- objectProps . push (
470
- `${ JSON . stringify ( filePath ) } : () => ${ importStatement } `
471
- )
489
+ if ( ! isProduction && isCSS ) {
490
+ objectProps . push (
491
+ `${ JSON . stringify (
492
+ filePath
493
+ ) } : () => { ${ createCssDefaultImportWarning (
494
+ globs ,
495
+ options
496
+ ) } return ${ importStatement } }`
497
+ )
498
+ } else {
499
+ objectProps . push (
500
+ `${ JSON . stringify ( filePath ) } : () => ${ importStatement } `
501
+ )
502
+ }
472
503
}
473
504
} )
474
505
@@ -480,9 +511,21 @@ export async function transformGlobImport(
480
511
originalLineBreakCount > 0
481
512
? '\n' . repeat ( originalLineBreakCount )
482
513
: ''
483
- const replacement = `/* #__PURE__ */ Object.assign({${ objectProps . join (
484
- ','
485
- ) } ${ lineBreaks } })`
514
+
515
+ let replacement : string
516
+ if ( ! isProduction && includesCSS ) {
517
+ replacement =
518
+ '/* #__PURE__ */ Object.assign(' +
519
+ '(() => {' +
520
+ `let ${ warnedCSSDefaultImportVarName } = false;` +
521
+ `return {${ objectProps . join ( ',' ) } ${ lineBreaks } };` +
522
+ '})()' +
523
+ ')'
524
+ } else {
525
+ replacement = `/* #__PURE__ */ Object.assign({${ objectProps . join (
526
+ ','
527
+ ) } ${ lineBreaks } })`
528
+ }
486
529
s . overwrite ( start , end , replacement )
487
530
488
531
return staticImports
0 commit comments