@@ -21,12 +21,12 @@ import fg from 'fast-glob'
2121import { stringifyQuery } from 'ufo'
2222import type { GeneralImportGlobOptions } from 'types/importGlob'
2323import type { Plugin } from '../plugin'
24- import type { ViteDevServer } from '../server'
2524import type { EnvironmentModuleNode } from '../server/moduleGraph'
2625import type { ResolvedConfig } from '../config'
2726import { evalValue , normalizePath , transformStableResult } from '../utils'
2827import type { Logger } from '../logger'
2928import { slash } from '../../shared/utils'
29+ import type { Environment } from '../environment'
3030
3131const { isMatch, scan } = micromatch
3232
@@ -44,43 +44,16 @@ interface ParsedGeneralImportGlobOptions extends GeneralImportGlobOptions {
4444 query ?: string
4545}
4646
47- export function getAffectedGlobModules (
48- file : string ,
49- server : ViteDevServer ,
50- ) : EnvironmentModuleNode [ ] {
51- const modules : EnvironmentModuleNode [ ] = [ ]
52- // TODO: properly support other runtimes. Changing _importGlobMap breaks VitePress
53- // https://github.com/vuejs/vitepress/blob/28989df83446923a9e7c8ada345b0778119ed66f/src/node/plugins/staticDataPlugin.ts#L128
54- for ( const [ id , allGlobs ] of server . _importGlobMap ! ) {
55- // (glob1 || glob2) && !glob3 && !glob4...
56- if (
57- allGlobs . some (
58- ( { affirmed, negated } ) =>
59- ( ! affirmed . length || affirmed . some ( ( glob ) => isMatch ( file , glob ) ) ) &&
60- ( ! negated . length || negated . every ( ( glob ) => isMatch ( file , glob ) ) ) ,
61- )
62- ) {
63- const mod = server . environments . client . moduleGraph . getModuleById ( id )
64-
65- if ( mod ) {
66- if ( mod . file ) {
67- server . environments . client . moduleGraph . onFileChange ( mod . file )
68- }
69- modules . push ( mod )
70- }
71- }
72- }
73- return modules
74- }
75-
7647export function importGlobPlugin ( config : ResolvedConfig ) : Plugin {
77- let server : ViteDevServer | undefined
48+ const importGlobMaps = new Map <
49+ Environment ,
50+ Map < string , { affirmed : string [ ] ; negated : string [ ] } [ ] >
51+ > ( )
7852
7953 return {
8054 name : 'vite:import-glob' ,
81- configureServer ( _server ) {
82- server = _server
83- server . _importGlobMap . clear ( )
55+ configureServer ( ) {
56+ importGlobMaps . clear ( )
8457 } ,
8558 async transform ( code , id , options ) {
8659 if ( ! code . includes ( 'import.meta.glob' ) ) return
@@ -94,9 +67,12 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
9467 config . logger ,
9568 )
9669 if ( result ) {
97- if ( server ) {
70+ if ( this . environment ) {
9871 const allGlobs = result . matches . map ( ( i ) => i . globsResolved )
99- server . _importGlobMap . set (
72+ if ( ! importGlobMaps . has ( this . environment ) ) {
73+ importGlobMaps . set ( this . environment , new Map ( ) )
74+ }
75+ importGlobMaps . get ( this . environment ) ! . set (
10076 id ,
10177 allGlobs . map ( ( globs ) => {
10278 const affirmed : string [ ] = [ ]
@@ -112,6 +88,29 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
11288 return transformStableResult ( result . s , id , config )
11389 }
11490 } ,
91+ hotUpdate ( { type, file, modules : oldModules , environment } ) {
92+ if ( type === 'update' ) return
93+
94+ const importGlobMap = importGlobMaps . get ( environment )
95+ if ( ! importGlobMap ) return
96+
97+ const modules : EnvironmentModuleNode [ ] = [ ]
98+ for ( const [ id , allGlobs ] of importGlobMap ) {
99+ // (glob1 || glob2) && !glob3 && !glob4...
100+ if (
101+ allGlobs . some (
102+ ( { affirmed, negated } ) =>
103+ ( ! affirmed . length ||
104+ affirmed . some ( ( glob ) => isMatch ( file , glob ) ) ) &&
105+ ( ! negated . length || negated . every ( ( glob ) => isMatch ( file , glob ) ) ) ,
106+ )
107+ ) {
108+ const mod = environment . moduleGraph . getModuleById ( id )
109+ if ( mod ) modules . push ( mod )
110+ }
111+ }
112+ return modules . length > 0 ? [ ...oldModules , ...modules ] : undefined
113+ } ,
115114 }
116115}
117116
0 commit comments